Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I vertically center text with CSS? [duplicate]

I have a <div> element which contains text and I want to align the contents of this <div> vertically center.

Here is my <div> style:

#box {   height: 170px;   width: 270px;   background: #000;   font-size: 48px;   color: #FFF;   text-align: center; }
<div id="box">   Lorem ipsum dolor sit </div>

What is the best way to achieve this goal?

like image 504
Irakli Lekishvili Avatar asked Jan 14 '12 21:01

Irakli Lekishvili


People also ask

How do I center text vertically in CSS?

To center both vertically and horizontally, use padding and text-align: center : I am vertically and horizontally centered.

How do I center multiple text in CSS?

To center text in CSS, use the text-align property and define it with the value “center.” Let's start with an easy example. Say you have a text-only web page and want to center all the text. Then you could use the CSS universal selector (*) or the type selector body to target every element on the page.

How do I vertically align text in a div using CSS?

The CSS just sizes the div, vertically center aligns the span by setting the div's line-height equal to its height, and makes the span an inline-block with vertical-align: middle. Then it sets the line-height back to normal for the span, so its contents will flow naturally inside the block.


1 Answers

You can try this basic approach:

div {    height: 100px;    line-height: 100px;    text-align: center;    border: 2px dashed #f69c55;  }
<div>    Hello World!  </div>

It only works for a single line of text though, because we set the line's height to the same height as the containing box element.


A more versatile approach

This is another way to align text vertically. This solution will work for a single line and multiple lines of text, but it still requires a fixed height container:

div {    height: 100px;    line-height: 100px;    text-align: center;    border: 2px dashed #f69c55;  }  span {    display: inline-block;    vertical-align: middle;    line-height: normal;  }
<div>    <span>Hello World!</span>  </div>

The CSS just sizes the <div>, vertically center aligns the <span> by setting the <div>'s line-height equal to its height, and makes the <span> an inline-block with vertical-align: middle. Then it sets the line-height back to normal for the <span>, so its contents will flow naturally inside the block.


Simulating table display

And here is another option, which may not work on older browsers that don't support display: table and display: table-cell (basically just Internet Explorer 7). Using CSS we simulate table behavior (since tables support vertical alignment), and the HTML is the same as the second example:

div {    display: table;    height: 100px;    width: 100%;    text-align: center;    border: 2px dashed #f69c55;  }  span {    display: table-cell;    vertical-align: middle;  }
<div>    <span>Hello World!</span>  </div>

Using absolute positioning

This technique uses an absolutely positioned element setting top, bottom, left and right to 0. It is described in more detail in an article in Smashing Magazine, Absolute Horizontal And Vertical Centering In CSS.

div {    display: flex;    justify-content: center;    align-items: center;    height: 100px;    width: 100%;    border: 2px dashed #f69c55;  }
<div>    <span>Hello World!</span>  </div>
like image 147
14 revs, 7 users 76% Avatar answered Sep 24 '22 17:09

14 revs, 7 users 76%