Question
How can I vertically align text inside a div using CSS?
I want the text inside this div to appear vertically centered, but the approaches I tried are not working. In particular, I used vertical-align: middle, but it does not seem to affect the text placement inside a normal block-level div.
.testimonialText {
position: absolute;
left: 15px;
top: 15px;
width: 150px;
height: 309px;
vertical-align: middle;
text-align: center;
font-family: Georgia, "Times New Roman", Times, serif;
font-style: italic;
padding: 1em 0;
}
<div class="testimonialText">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
What is the correct and most effective way to vertically center text inside a div?
Short Answer
By the end of this page, you will understand why vertical-align: middle does not vertically center text inside a normal div, and you will learn the main CSS techniques used to center content vertically, especially modern Flexbox-based solutions.
Concept
In CSS, vertical centering depends on how the element participates in layout. A common beginner mistake is assuming that vertical-align: middle works on every element. It does not.
vertical-align mainly affects:
- inline elements
- inline-block elements
- table cells
A normal div is a block-level element, so vertical-align usually has no effect on its inner text layout.
If you want to vertically center content inside a div, the best method depends on the situation:
- Flexbox: best general-purpose modern solution
- Grid: also simple and powerful
- line-height: useful for a single line of text
- table-cell: older technique, sometimes still seen in legacy code
- absolute positioning + transform: useful in special layout cases
Why this matters in real programming:
- Centering text is common in buttons, cards, hero sections, banners, modals, and empty states.
- Using the wrong property can waste time because the CSS appears valid but has no visible effect.
- Knowing which layout system you are using helps you choose the correct centering technique quickly.
Mental Model
Think of CSS layout like different kinds of containers.
- A normal
divis like a regular box on a shelf. vertical-alignis a rule for how items sit next to each other in a row, not how content floats up and down inside a box.- Flexbox turns the box into a smart container that can place its contents in the center automatically.
So instead of telling the text to "align vertically," you usually tell the container how to arrange its children.
A good mental shortcut is:
- Need to center content inside a box? Use Flexbox.
- Need to align inline items beside each other?
vertical-alignmay help.
Syntax and Examples
Modern recommended approach: Flexbox
.testimonialText {
display: flex;
align-items: center; /* vertical centering */
justify-content: center; /* horizontal centering */
width: 150px;
height: 309px;
text-align: center;
font-family: Georgia, "Times New Roman", Times, serif;
font-style: italic;
padding: 1em;
}
<div class="testimonialText">
Center this text inside the box.
</div>
Why it works
display: flexmakes thediva flex container.align-items: centercenters content vertically.justify-content: centercenters content horizontally.
This is the most common modern solution.
For a single line of text: line-height
Step by Step Execution
Consider this example:
<div class="box">Hello world</div>
.box {
display: flex;
align-items: center;
justify-content: center;
height: 100px;
border: 1px solid #999;
}
Here is what happens step by step:
- The browser creates a
divwith the textHello worldinside it. height: 100pxgives the box a fixed vertical space.display: flexchanges thedivinto a flex container.- The text becomes flex content inside that container.
align-items: centerplaces the content in the vertical middle of the 100px box.justify-content: centerplaces the content in the horizontal middle.- The result is text centered both vertically and horizontally.
Now compare that to this broken expectation:
Real World Use Cases
Vertical centering appears in many everyday UI elements:
- Buttons: text should sit neatly in the middle
- Cards: a short message may need centered placement
- Loading states: "Loading..." often appears in the middle of a panel
- Empty states: messages like "No results found" are often centered in a container
- Hero banners: headings and call-to-action text may need vertical centering
- Profile widgets: avatars and labels often need centered alignment
Example: centered message in a dashboard card
.emptyState {
display: flex;
align-items: center;
justify-content: center;
height: 200px;
text-align: center;
color: #666;
}
<div class="emptyState">No notifications yet</div>
Real Codebase Usage
In real projects, developers usually solve vertical centering with layout systems rather than one-off hacks.
Common patterns
1. Flexbox utility classes
Many codebases create reusable classes like this:
.flex-center {
display: flex;
align-items: center;
justify-content: center;
}
Then they reuse it:
<div class="card flex-center">Saved successfully</div>
2. Guarding against unknown content length
If text might wrap to multiple lines, Flexbox is safer than line-height.
3. Component styling
In UI components, vertical centering is often built directly into the component's container:
.modalHeader {
display: flex;
align-items: center;
}
4. Combining alignment with spacing
Developers often pair centering with padding and max-width for readability:
.messageBox {
: flex;
: center;
: center;
: ;
: center;
}
Common Mistakes
1. Using vertical-align on a normal block div
Broken example:
.box {
height: 200px;
vertical-align: middle;
}
Why it fails:
vertical-aligndoes not vertically center block content inside a normaldiv.
Use this instead:
.box {
display: flex;
align-items: center;
}
2. Using line-height for multi-line text
Broken example:
.box {
height: 200px;
line-height: 200px;
}
Why it fails:
- This only works well for a single line.
- Wrapped text becomes awkward and unreadable.
3. Forgetting to give the container a height
Broken example:
Comparisons
| Technique | Best for | Works with multi-line text? | Modern? | Notes |
|---|---|---|---|---|
display: flex + align-items: center | Most layouts | Yes | Yes | Best general solution |
display: grid + place-items: center | Simple centering | Yes | Yes | Very clean syntax |
line-height | Single-line text | No | Limited use | Quick but not flexible |
display: table-cell + vertical-align: middle |
Cheat Sheet
/* Best general solution */
.box {
display: flex;
align-items: center; /* vertical */
justify-content: center; /* horizontal */
}
/* Grid alternative */
.box {
display: grid;
place-items: center;
}
/* Single line only */
.box {
height: 50px;
line-height: 50px;
text-align: center;
}
/* Legacy approach */
.box {
display: table-cell;
vertical-align: middle;
}
Rules to remember
vertical-aligndoes not vertically center text inside a normal blockdiv.text-align: centeris for horizontal centering of inline content.- Flexbox is usually the safest choice.
- Multi-line text needs Flexbox, Grid, or table-cell style approaches.
FAQ
Why does vertical-align: middle not work on a div?
Because a normal div is a block element, and vertical-align does not control how block content is vertically positioned inside it.
What is the best modern way to vertically center text in CSS?
Usually Flexbox:
display: flex;
align-items: center;
justify-content: center;
Can I use line-height to center text vertically?
Yes, but only for single-line text. It is not a good solution for wrapped or multi-line content.
Does text-align: center vertically center text?
No. It only centers text horizontally.
Is CSS Grid also good for centering?
Yes. A simple option is:
display: grid;
place-items: center;
What if my text spans multiple lines?
Use Flexbox or Grid. They handle multi-line content much better than line-height.
Does the container need a height for vertical centering?
Mini Project
Description
Build a simple status card that displays a message centered inside a fixed-height box. This demonstrates the correct modern way to vertically and horizontally align text inside a div using CSS Flexbox.
Goal
Create a message card whose text stays centered inside the box, even when the content wraps onto multiple lines.
Requirements
- Create an HTML
divthat acts as a card. - Give the card a fixed height and width.
- Center the text vertically inside the card.
- Center the text horizontally inside the card.
- Use a solution that still works when the text wraps to multiple lines.
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.
Check If a Checkbox Is Checked with jQuery
Learn how to check whether a checkbox is checked in jQuery using the correct selector, with examples, mistakes, and practical patterns.
Convert HTML and CSS to PDF in PHP: Options, Limits, and Practical Approaches
Learn how HTML-to-PDF conversion works in PHP, why CSS support varies, and how to choose a practical approach for Linux web servers.