CSS Vertical Align: Vertically Center Text (Quick)
Vertically center text inside a div with CSS. Use Flexbox, Grid, or line-height and table-cell fallbacks for reliable css vertical align across modern browsers.
How do I vertically center text within a div using CSS?
I have a div element containing text that I want to align vertically center. Here’s my current CSS:
#box {
height: 170px;
width: 270px;
background: #000;
font-size: 48px;
color: #FFF;
text-align: center;
}
And the HTML:
<div id="box">
Lorem ipsum dolor sit
</div>
What is the best method to vertically center the text inside this div?
Use CSS Flexbox to vertical align text inside your div: set the container to display: flex and add align-items: center (vertical) plus justify-content: center (horizontal) for a simple, robust css vertical align center. Flexbox handles single- and multi-line text and adapts when the box size changes; for a single-line fixed-height block you can also use line-height, and CSS Grid or display: table-cell are viable fallbacks for older layouts.
Contents
- Best method: CSS Flexbox for vertical align center
- Examples: css vertical align for single-line and multi-line text
- Alternatives and legacy methods (vertical-align, table-cell)
- Common pitfalls and troubleshooting
- When to use each method (quick guide)
- Sources
- Conclusion
Best method: CSS Flexbox for vertical align center
Flexbox is the easiest, most reliable way to vertically center text in a div. With one small change to your CSS the browser will center whatever’s inside the box — single-line, wrapped text, or multiple children — and keep it centered if the box size changes.
Example (apply this to your existing #box):
#box {
height: 170px;
width: 270px;
background: #000;
color: #FFF;
font-size: 48px;
text-align: center;
display: flex; /* make it a flex container */
align-items: center; /* vertical centering (cross axis) */
justify-content: center; /* horizontal centering (main axis) */
}
HTML stays the same:
<div id="box">
Lorem ipsum dolor sit
</div>
Why this works: by default flex-direction is row, so align-items centers along the vertical (cross) axis. If you change the direction to column, then justify-content: center will be the vertical centering rule — keep that swap in mind. See the detailed explanation on MDN for align-items and flexbox alignment if you want to dive deeper: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_flexible_box_layout/Aligning_items_in_a_flex_container. For a live demo of vertical centering with flexbox, check Philip Walton’s solved-by-flexbox examples: https://philipwalton.github.io/solved-by-flexbox/demos/vertical-centering/.
Examples: css vertical align for single-line and multi-line text
- Flexbox (multi-line, responsive — recommended)
#box {
display: flex;
align-items: center;
justify-content: center;
height: 170px;
width: 270px;
padding: 0 10px; /* allow text to wrap nicely */
text-align: center;
}
- CSS Grid (equally simple)
#box {
display: grid;
place-items: center; /* shorthand for align-items + justify-items */
height: 170px;
width: 270px;
text-align: center;
}
Grid is just as robust for simple centering and useful if your layout already uses CSS Grid.
- line-height (single-line only)
#box {
height: 170px;
line-height: 170px; /* only works reliably for a single line */
text-align: center;
}
Quick and tiny, but fragile: the text will break vertically if it wraps or if font-size changes. W3Schools summarizes line-height and centering approaches here: https://www.w3schools.com/css/css_align.asp.
- Absolute + transform (works without flex/grid if needed)
<div id="box"><span class="inner">Lorem ipsum</span></div>
#box { position: relative; height:170px; width:270px; }
#box .inner {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
}
This centers an inner element both vertically and horizontally; handy when you need pixel-perfect overlap.
Alternatives and legacy methods (vertical-align, table-cell)
-
vertical-alignonly affects inline-level or table-cell elements. It does not vertically center a block-level<div>by itself. The CSS-Tricks article onvertical-alignexplains the property and its limitations: https://css-tricks.com/almanac/properties/v/vertical-align/. -
display: table-cell+vertical-align: middleis a common legacy trick:
#box {
display: table-cell;
vertical-align: middle;
text-align: center;
height: 170px;
width: 270px;
}
It works in older browsers and for simple content, but it changes document semantics and plays poorly with modern layout systems.
- Inline-block +
vertical-align: middlecan align inline elements next to each other, but it’s not a general replacement for Flexbox when you need robust block centering.
In short: use Flexbox or Grid for modern projects; keep table-cell tricks for legacy contexts.
Common pitfalls and troubleshooting
- “Vertical align not working”: If you try
vertical-alignon a normal block-level div it won’t work — it needs inline or table-cell context. Check the element’sdisplayfirst. - Text seems off-center because of padding/margin: remember padding increases the box space; centering centers the entire content box (including padding). Remove or adjust padding, or use
box-sizing. - Using
line-heightwith wrapping text: if the text wraps to two lines, the line-height trick fails. Use Flexbox instead. - Confusing
align-itemsvsjustify-content: those flip whenflex-directionis changed. Default row →align-itemscontrols vertical centering. Column →justify-contentcontrols vertical centering. - Collapsed height: if your container has no explicit height (content-determined), vertical centering may not be visible. Make the container a fixed/min height, or center within a parent that has height.
- Browser quirks: modern browsers support flexbox and grid well; IE10–11 have partial support for older flexbox syntaxes. For broad compatibility read the MDN notes: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_flexible_box_layout/Aligning_items_in_a_flex_container.
If you’re still stuck, try isolating the problem: remove extra wrappers, give the box an obvious background, and toggle display:flex in devtools to see the effect.
When to use each method (quick guide)
- Use Flexbox (recommended): general-purpose vertical centering for single/multi-line text and mixed children.
- Use Grid: when your page already uses CSS Grid or you need to center multiple axis-aligned items.
- Use line-height: only for a single line of text inside a fixed-height box.
- Use absolute + transform: when you need to overlap or center a child independent of document flow.
- Use display:table-cell / vertical-align:middle: legacy support or very specific table-like layouts.
Pick Flexbox first — it’s short, readable, and future-friendly.
Sources
- CSS Horizontal & Vertical Align — W3Schools
- Aligning items in a flex container — MDN
- vertical-align — CSS-Tricks
- Vertical Centering — Solved by Flexbox (Philip Walton demo)
- CSS vertical-align how-to (freeCodeCamp article)
Conclusion
For your #box the best, simplest css vertical align solution is Flexbox — add display:flex; align-items:center; justify-content:center; and you’ll get a reliable vertical align center that handles single- and multi-line text and adapts to resizing. If you only have a single line and a fixed height, line-height is a tiny shortcut; otherwise prefer Flexbox (or Grid) and fall back to display: table-cell only for legacy needs.