Alignment & Centering
Control alignment and centering in css. Learn multiple techniques to position content exactly where you want it, both horizontally and vertically.
How do I center an element in CSS?
The simplest way to centre anything, both horizontally and vertically, is to make the parent a grid with display: grid; place-items: center;, or a flex container with display: flex; justify-content: center; align-items: center;. To centre a fixed-width block horizontally, use margin-inline: auto; to centre text, use text-align: center.
/* Center anything, both axes */
.parent { display: grid; place-items: center; min-height: 100vh; }
/* Flexbox equivalent */
.parent { display: flex; justify-content: center; align-items: center; }
/* Horizontally center a block */
.box { width: 600px; margin-inline: auto; }
/* Center text */
h1 { text-align: center; }Interactive Alignment Demo
Code Editor
<div class="center-container">
<div class="centered-content">
<h3>Perfectly Centered</h3>
<p>This content is centered both horizontally and vertically.</p>
</div>
</div>Preview
Flexbox Centering
Flexbox provides the most straightforward and reliable way to center content both horizontally and vertically. It works in all modern browsers and handles dynamic content well.
Perfect Center
.center {
display: flex;
justify-content: center; /* Horizontal */
align-items: center; /* Vertical */
min-height: 100vh; /* Full viewport height */
}The most reliable method for centering any content, regardless of size.
Horizontal Only
.horizontal-center {
display: flex;
justify-content: center;
}Center content horizontally only
Vertical Only
.vertical-center {
display: flex;
align-items: center;
min-height: 400px;
}Center content vertically only
CSS Grid Centering
Grid Place Items
.grid-center {
display: grid;
place-items: center; /* Shorthand for align-items + justify-items */
min-height: 100vh;
}
/* Or separately */
.grid-center {
display: grid;
align-items: center;
justify-items: center;
}Grid's place-items provides concise centering with excellent browser support.
Grid Template Areas
.area-center {
display: grid;
grid-template-areas: ". content .";
grid-template-columns: 1fr auto 1fr;
min-height: 100vh;
align-items: center;
}
.content {
grid-area: content;
}Use grid areas for semantic centering
Grid Auto Margins
.grid-container {
display: grid;
min-height: 100vh;
}
.grid-item {
margin: auto;
}Auto margins work in grid containers
Classic Centering Methods
Transform Method
.transform-center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
/* Parent container needs relative positioning */
.parent {
position: relative;
min-height: 400px;
}Works regardless of content size, but requires absolute positioning.
Margin Auto (Horizontal)
.margin-center {
width: 300px; /* Fixed width required */
margin: 0 auto;
}
/* Or */
.margin-center {
max-width: 300px;
margin-left: auto;
margin-right: auto;
}Classic horizontal centering for block elements
Line Height (Vertical)
.line-height-center {
height: 200px;
line-height: 200px;
text-align: center;
}
/* Only works for single line text */Simple method for single-line text
Text and Content Alignment
Text Alignment
.text-center {
text-align: center;
}
.text-left {
text-align: left;
}
.text-right {
text-align: right;
}
.text-justify {
text-align: justify;
}Basic text alignment properties
Vertical Align
/* For inline/table-cell elements */
.vertical-align {
vertical-align: top;
vertical-align: middle;
vertical-align: bottom;
vertical-align: baseline;
}
/* Table cell centering */
.table-cell {
display: table-cell;
vertical-align: middle;
text-align: center;
}Vertical alignment for inline content
Image Centering
/* Center image horizontally */
.center-image {
display: block;
margin: 0 auto;
}
/* Center image in container */
.image-container {
display: flex;
justify-content: center;
align-items: center;
min-height: 300px;
}
/* Object fit for responsive images */
.responsive-image {
width: 100%;
height: 200px;
object-fit: cover;
object-position: center;
}Multiple approaches for centering images and media content.
When to Use Each Method
Recommended Methods
- • Flexbox: Best for most centering needs
- • Grid: Great for 2D layouts and components
- • Margin auto: Simple horizontal centering
- • Text-align: For text and inline content
- • Transform: When absolute positioning is needed
Best Practices
- • Use flexbox as your default centering method
- • Consider content type and container context
- • Test with dynamic content of varying sizes
- • Ensure accessibility with proper focus management
- • Use semantic HTML alongside centering techniques
CSS Alignment — step by step
Flexbox Centering
The simplest centering: display: flex; align-items: center; justify-content: center on the parent centers the child both horizontally and vertically.
🎯 Quick Check
Which property is the shorthand for both align-items and justify-items in Grid?
CSS Alignment: summary
Key points
text-aligncentres inline content (text, inline-block children) within a block. It doesn't move the block itself.margin: 0 autoonly works on block elements that have a width smaller than their parent.- Vertical centring needs a parent taller than the child: set
heightormin-heighton it. place-items: centeris shorthand foralign-items: center; justify-items: center.
Common mistakes
- Using
text-align: centerto centre adivand only the text moves. - Trying
vertical-align: middleon a block element. It only applies to inline and table-cell elements.
Frequently asked questions
›How do I center a div vertically and horizontally?
Give the parent display: grid; place-items: center; and a height, such as min-height: 100vh. It's two lines and works in every modern browser.
›Why does margin: auto not center my element?
The element must be block-level and have a width smaller than its container. An inline element, or a block with width: auto (full width), has nothing to centre.
›How do I center an absolutely positioned element?
Use position: absolute; inset: 0; margin: auto; with a set width and height, or top: 50%; left: 50%; transform: translate(-50%, -50%);.