NeuroAgent

How to Vertically Center Text in a Div

Learn multiple methods to vertically center text in a div using CSS. Discover modern Flexbox, Grid, and traditional techniques with code examples.

Question

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:

css
#box {
  height: 170px;
  width: 270px;
  background: #000;
  font-size: 48px;
  color: #FFF;
  text-align: center;
}

And the HTML:

html
<div id="box">
  Lorem ipsum dolor sit
</div>

What is the best method to vertically center the text inside this div?

NeuroAgent

There are several effective methods to vertically center text within a div using CSS, with the most modern and recommended approach being the CSS display: flex property. You can achieve vertical centering by adding display: flex and align-items: center to your existing CSS, or using the CSS Grid method which offers excellent browser support and is particularly useful for creating responsive layouts.


Contents


Traditional CSS Methods

Absolute Positioning with Transform

This method uses absolute positioning combined with CSS transforms:

css
#box {
  height: 170px;
  width: 270px;
  background: #000;
  font-size: 48px;
  color: #FFF;
  text-align: center;
  position: relative;
}

#box::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: 1px;
}

#box span {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

HTML structure needed:

html
<div id="box">
  <span>Lorem ipsum dolor sit</span>
</div>

This method works well but requires additional HTML markup and can be complex for simple centering tasks.


Modern CSS Flexbox Method

Flexbox is currently the most recommended method for vertical centering due to its simplicity and widespread browser support.

Basic Flexbox Implementation

css
#box {
  height: 170px;
  width: 270px;
  background: #000;
  font-size: 48px;
  color: #FFF;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
}

This single approach handles both horizontal and vertical centering simultaneously. The align-items: center property vertically aligns items, while justify-content: center handles horizontal alignment.

Flexbox with Multiple Lines

For content that might wrap to multiple lines:

css
#box {
  height: 170px;
  width: 270px;
  background: #000;
  font-size: 48px;
  color: #FFF;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 10px; /* Optional: space between wrapped lines */
}

CSS Grid Method

CSS Grid provides another modern and elegant solution for vertical centering:

Basic Grid Implementation

css
#box {
  height: 170px;
  width: 270px;
  background: #000;
  font-size: 48px;
  color: #FFF;
  display: grid;
  place-items: center;
}

The place-items: center is a shorthand property that sets both align-items and justify-items to center, making it incredibly concise.

Alternative Grid Method

css
#box {
  height: 170px;
  width: 270px;
  background: #000;
  font-size: 48px;
  color: #FFF;
  display: grid;
  align-items: center;
  justify-items: center;
}

Line Height Method

This method works well for single-line text but has limitations for multi-line content:

Single-Line Text Implementation

css
#box {
  height: 170px;
  width: 270px;
  background: #000;
  font-size: 48px;
  color: #FFF;
  text-align: center;
  line-height: 170px;
}

Important: The line height must equal the container height. For responsive designs where height might change, this method becomes less practical.

Modified Line Height Method

css
#box {
  height: 170px;
  width: 270px;
  background: #000;
  font-size: 48px;
  color: #FFF;
  text-align: center;
  line-height: normal;
  display: inline-block;
  vertical-align: middle;
}

#box::after {
  content: '';
  display: inline-block;
  height: 100%;
  vertical-align: middle;
}

Table Cell Method

This method mimics traditional table layout behavior:

Implementation

css
#box {
  height: 170px;
  width: 270px;
  background: #000;
  font-size: 48px;
  color: #FFF;
  text-align: center;
  display: table-cell;
  vertical-align: middle;
}

This approach provides good compatibility but can be less intuitive than modern CSS methods and may have performance implications in complex layouts.


Comparison of Methods

Method Code Complexity Browser Support Best Use Case Limitations
Flexbox Low Excellent Modern layouts, responsive design None for most use cases
CSS Grid Low Excellent Grid-based layouts Overkill for simple centering
Line Height Very Low Universal Single-line text Fails with multi-line content
Absolute Positioning High Good Complex positioning scenarios Requires additional HTML
Table Cell Medium Universal Legacy browser support Less semantic, performance issues

Recommendation Summary

  • For modern web development: Use Flexbox (display: flex + align-items: center)
  • For grid-based layouts: Use CSS Grid (display: grid + place-items: center)
  • For single-line text: Use Line Height method for simplicity
  • For legacy support: Consider Table Cell method

Browser Compatibility Considerations

Modern Methods (Flexbox & Grid)

Both Flexbox and CSS Grid have excellent browser support in modern browsers:

  • Flexbox: Supported by all modern browsers including IE11+ with vendor prefixes
  • CSS Grid: Supported by all modern browsers including IE10+ with vendor prefixes

Legacy Support

For older browsers like Internet Explorer 8 and 9, you might need fallback methods:

css
#box {
  /* Modern browsers */
  display: flex;
  align-items: center;
  justify-content: center;
  
  /* Fallback for IE9 */
  display: -ms-flexbox;
  -ms-flex-align: center;
  -ms-flex-pack: center;
  
  /* Fallback for IE8 */
  display: table;
  vertical-align: middle;
}

Sources

  1. MDN Web Docs - Using CSS flexible boxes
  2. CSS Tricks - Complete Guide to Flexbox
  3. W3C CSS Grid Layout Module Level 1
  4. Can I Use - CSS Flexbox Support
  5. Can I Use - CSS Grid Support

Conclusion

Vertically centering text in a div is a common CSS requirement with multiple solutions available. The best modern approach is using Flexbox with display: flex, align-items: center, and justify-content: center properties, as it provides excellent browser support, simple implementation, and works well with responsive designs.

For your specific example with the black div containing white text, simply add these three properties to your existing CSS:

css
#box {
  /* Your existing properties */
  height: 170px;
  width: 270px;
  background: #000;
  font-size: 48px;
  color: #FFF;
  text-align: center;
  
  /* Add these for vertical centering */
  display: flex;
  align-items: center;
  justify-content: center;
}

This solution will work across all modern browsers and provides the most maintainable code for future development. If you’re working on a project with specific legacy browser requirements, consider using the line height method for single-line text or the table cell method as a fallback option.