Question
I have a div that contains text, and I want the text inside it to be vertically centered.
Here is the current HTML and CSS:
<div id="box">
Lorem ipsum dolor sit
</div>
#box {
height: 170px;
width: 270px;
background: #000;
font-size: 48px;
color: #fff;
text-align: center;
}
What is the best way to vertically center the text inside this div using CSS?
Short Answer
By the end of this page, you will understand how vertical centering works in CSS, why it can be confusing at first, and which techniques to use in different situations. You will learn the modern flexbox approach, when line-height works, and how to choose the right method for single-line and multi-line text.
Concept
Vertical centering in CSS means placing content in the middle of a container along its vertical axis.
This is a very common layout task, but beginners often find it tricky because CSS was not originally designed with simple vertical alignment for block elements in mind. Some properties sound like they should help, such as vertical-align, but they do not work the way many people expect on normal div elements.
The best modern solution is usually Flexbox.
With Flexbox, you can tell a container to align its child content both horizontally and vertically:
#box {
display: flex;
justify-content: center;
align-items: center;
}
Why this matters in real programming:
- Centering text in buttons, cards, badges, banners, and modals
- Positioning loading messages and empty-state messages
- Building reusable UI components that stay aligned at different sizes
- Creating layouts that work for both short and long text
A key point is that the best technique depends on the content:
- Single-line text:
line-heightcan work - Single-line or multi-line text:
flexboxis usually better - Complex layouts:
gridcan also help
For modern CSS, flexbox is the most practical and reliable answer.
Mental Model
Think of the div as a box in a room.
- Horizontal centering means moving the text left or right until it sits in the middle.
- Vertical centering means moving the text up or down until it sits halfway between the top and bottom.
With older CSS, this often felt like trying to balance an object by guessing distances.
With Flexbox, the container becomes a smart alignment system. You tell the box:
justify-content: center→ center things along one axisalign-items: center→ center things along the other axis
So instead of manually calculating spacing, you let CSS do the positioning for you.
Syntax and Examples
Modern solution with Flexbox
<div id="box">Lorem ipsum dolor sit</div>
#box {
height: 170px;
width: 270px;
background: #000;
color: #fff;
font-size: 48px;
text-align: center;
display: flex;
justify-content: center;
align-items: center;
}
Why this works
display: flexturns thedivinto a flex containerjustify-content: centercenters content on the main axisalign-items: centercenters content on the cross axis
In a default flex row layout, this combination centers the text both vertically and horizontally.
Single-line text with line-height
If the text is only one line, you can set equal to the container height:
Step by Step Execution
Consider this example:
<div id="box">Hello</div>
#box {
height: 170px;
width: 270px;
display: flex;
justify-content: center;
align-items: center;
background: black;
color: white;
}
Here is what happens step by step:
- The browser creates a
divwith the textHelloinside it. height: 170pxgives the container vertical space.width: 270pxgives the container horizontal space.display: flexchanges the layout behavior of the container.- The text becomes a flex item inside the container.
justify-content: centermoves the content to the middle on the main axis.align-items: centermoves the content to the middle on the cross axis.- The result is text centered inside the box.
If the text becomes longer and wraps to another line, Flexbox still keeps the whole text block centered, which is one reason it is preferred over .
Real World Use Cases
Vertical centering is used in many everyday UI elements:
- Buttons: center button labels inside fixed-height buttons
- Cards: center short messages inside a panel
- Hero sections: place headings in the middle of a banner
- Loaders and empty states: center text like
Loading...orNo results - Navigation items: align text within menu bars
- Profile badges and counters: center numbers or initials inside boxes or circles
Example: a call-to-action button
.button {
display: flex;
justify-content: center;
align-items: center;
height: 48px;
padding: 0 16px;
}
This keeps the label centered even if the font or content changes.
Real Codebase Usage
In real projects, developers usually prefer Flexbox or Grid because they are predictable and easy to maintain.
Common patterns include:
Reusable centered components
.center-content {
display: flex;
justify-content: center;
align-items: center;
}
This utility class can be reused across many components.
Guarding against variable content length
When text might wrap, developers avoid line-height tricks and use Flexbox instead.
Centering icons and text together
.badge {
display: inline-flex;
align-items: center;
justify-content: center;
}
This works well for pills, badges, and status labels.
Responsive layouts
Flexbox makes it easier to keep items centered across screen sizes without recalculating heights.
Component libraries
UI libraries often use utility classes like:
display: flexalign-items: centerjustify-content: center
Common Mistakes
1. Using vertical-align on a normal div
Many beginners try this:
#box {
vertical-align: middle;
}
This usually does not vertically center text inside a block-level div.
Why
vertical-align mainly applies to:
- inline elements
- inline-block elements
- table cells
It is not the right tool for most div centering tasks.
2. Using line-height for multi-line text
Broken approach for wrapping text:
#box {
height: 170px;
line-height: 170px;
}
If the text becomes two lines, spacing looks wrong.
Avoid it by using Flexbox
#box {
display: flex;
align-items: center;
: center;
}
Comparisons
| Method | Best for | Works with multi-line text? | Notes |
|---|---|---|---|
flexbox | Most modern layouts | Yes | Usually the best choice |
grid | Simple two-axis centering | Yes | Very clean with place-items: center |
line-height | Single-line text | No | Quick trick, but limited |
vertical-align | Table cells or inline elements | Sometimes | Not suitable for normal block divs |
Flexbox vs line-height
Cheat Sheet
/* Best general solution */
.container {
display: flex;
justify-content: center;
align-items: center;
}
/* Single-line text only */
.container {
height: 50px;
line-height: 50px;
text-align: center;
}
/* Grid alternative */
.container {
display: grid;
place-items: center;
}
Quick rules
- Use
flexboxfor reliable vertical centering - Use
line-heightonly for single-line text - Do not rely on
vertical-alignfor standarddivcentering - Make sure the container has height
- In flex row layout:
justify-content= horizontalalign-items= vertical
Good default for this question
FAQ
How do I vertically center text in a div with CSS?
The most common modern solution is to use Flexbox:
div {
display: flex;
align-items: center;
justify-content: center;
}
Why does vertical-align: middle not work on my div?
Because vertical-align does not work the way many expect on normal block-level elements. It is mainly for inline elements and table cells.
What is the easiest way to center single-line text vertically?
Set line-height equal to the container height. This is simple, but only reliable for one line of text.
Is Flexbox better than line-height for vertical centering?
Yes, in most cases. Flexbox works better with multi-line content and is more flexible for real layouts.
Can CSS Grid center text vertically too?
Yes. You can use:
display: grid;
place-items: center;
This centers content both vertically and horizontally.
Do I need a fixed height to vertically center text?
Mini Project
Description
Build a centered message box that displays a short status message in the middle of a panel. This demonstrates how to vertically and horizontally center text using modern CSS in a realistic UI component.
Goal
Create a fixed-size message box where the text stays centered, even if the content changes.
Requirements
- Create a
divcontainer with a visible background color - Center the text both vertically and horizontally
- Use a modern CSS approach
- Make sure the text remains readable with contrasting colors
Keep learning
Related questions
Can You Style Half a Character in CSS? Text Effects with CSS and JavaScript
Learn how to style half of a character using CSS and JavaScript, including overlay techniques for dynamic text effects.
Get Screen, Page, and Browser Window Size in JavaScript
Learn how to get screen size, viewport size, page size, and scroll position in JavaScript across major browsers.
Get the Selected Radio Button Value with jQuery
Learn how to find which radio button is selected in jQuery and get its value with simple examples and common mistakes.