html - How do I vertically center text with CSS?

ID : 282

viewed : 134

Tags : htmlcssvertical-alignmenthtml

Top 5 Answer for html - How do I vertically center text with CSS?

vote vote

95

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>

vote vote

89

Another way (not mentioned here yet) is with Flexbox.

Just add the following code to the container element:

display: flex; justify-content: center; /* align horizontal */ align-items: center; /* align vertical */ 

Flexbox demo 1

.box {   height: 150px;   width: 400px;   background: #000;   font-size: 24px;   font-style: oblique;   color: #FFF;   text-align: center;   padding: 0 20px;   margin: 20px;   display: flex;   justify-content: center;   /* align horizontal */   align-items: center;   /* align vertical */ }
<div class="box">   Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh </div>

Alternatively, instead of aligning the content via the container, flexbox can also center a flex item with an auto margin when there is only one flex-item in the flex container (like the example given in the question above).

So to center the flex item both horizontally and vertically just set it with margin:auto

Flexbox Demo 2

.box {   height: 150px;   width: 400px;   background: #000;   font-size: 24px;   font-style: oblique;   color: #FFF;   text-align: center;   padding: 0 20px;   margin: 20px;   display: flex; } .box span {   margin: auto; }
<div class="box">   <span>margin:auto on a flex item centers it both horizontally and vertically</span>  </div>

NB: All the above applies to centering items while laying them out in horizontal rows. This is also the default behavior, because by default the value for flex-direction is row. If, however flex-items need to be laid out in vertical columns, then flex-direction: column should be set on the container to set the main-axis as column and additionally the justify-content and align-items properties now work the other way around with justify-content: center centering vertically and align-items: center centering horizontally)

flex-direction: column demo

.box {   height: 150px;   width: 400px;   background: #000;   font-size: 18px;   font-style: oblique;   color: #FFF;   display: flex;   flex-direction: column;   justify-content: center;   /* vertically aligns items */   align-items: center;   /* horizontally aligns items */ } p {   margin: 5px;   }
<div class="box">   <p>     When flex-direction is column...   </p>   <p>     "justify-content: center" - vertically aligns   </p>   <p>     "align-items: center" - horizontally aligns   </p> </div>

A good place to start with Flexbox to see some of its features and get syntax for maximum browser support is flexyboxes

Also, browser support nowadays is very good: caniuse

For cross-browser compatibility for display: flex and align-items, you can use the following:

display: -webkit-box; display: -webkit-flex; display: -moz-box; display: -ms-flexbox; display: flex; -webkit-flex-align: center; -ms-flex-align: center; -webkit-align-items: center; align-items: center; 
vote vote

70

You can easily do this by adding the following piece of CSS code:

display: table-cell; vertical-align: middle; 

That means your CSS finally looks like:

#box {    height: 90px;    width: 270px;    background: #000;    font-size: 48px;    font-style: oblique;    color: #FFF;    text-align: center;    margin-top: 20px;    margin-left: 5px;    display: table-cell;    vertical-align: middle;  }
<div id="box">    Some text  </div>

vote vote

69

For reference and to add a simpler answer:

Pure CSS:

.vertical-align {     position: relative;     top: 50%;     -webkit-transform: translateY(-50%);     -ms-transform: translateY(-50%);     transform: translateY(-50%); } 

Or as a SASS/SCSS Mixin:

@mixin vertical-align {     position: relative;     top: 50%;     -webkit-transform: translateY(-50%);     -ms-transform: translateY(-50%);     transform: translateY(-50%); } 

Use by:

.class-to-center {     @include vertical-align; } 

By Sebastian Ekström's blog post Vertical align anything with just 3 lines of CSS:

This method can cause elements to be blurry due to the element being placed on a “half pixel”. A solution for this is to set its parent element to preserve-3d. Like following:

.parent-element {     -webkit-transform-style: preserve-3d;     -moz-transform-style: preserve-3d;     transform-style: preserve-3d; } 

We live in 2015+ and Flex Box is supported by every major modern browser.

It will be the way websites are made from here on out.

Learn it!

vote vote

57

All credit goes to this link owner @Sebastian Ekström Link; please go through this. See it in action codepen. By reading the above article I also created a demo fiddle.

With just three lines of CSS (excluding vendor prefixes) we can do it with the help of a transform: translateY vertically centers whatever we want, even if we don’t know its height.

The CSS property transform is usually used for rotating and scaling elements, but with its translateY function we can now vertically align elements. Usually this must be done with absolute positioning or setting line-heights, but these require you to either know the height of the element or only works on single-line text, etc.

So, to do this we write:

.element {     position: relative;     top: 50%;     transform: translateY(-50%); }  

That’s all you need. It is a similar technique to the absolute-position method, but with the upside that we don’t have to set any height on the element or position-property on the parent. It works straight out of the box, even in Internet Explorer 9!

To make it even more simple, we can write it as a mixin with its vendor prefixes.

Top 3 video Explaining html - How do I vertically center text with CSS?

Related QUESTION?