Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to vertically align into the center of the content of a div with defined width/height?

What would be the correct method to vertically center any content in a defined width/height div.

In the example there are two contents with different heights, what is the best way to center vertically both using the class .content . (and it works for every browser and without the solution of table-cell)

Have some solutions on mind, but would like to know other ideas, one is using position:absolute; top:0; bottom: 0; and margin auto.

like image 674
Ricardo Rodrigues Avatar asked Jun 10 '12 12:06

Ricardo Rodrigues


People also ask

How do I vertically align the content of a div to the center?

You can do this by setting the display property to “flex.” Then define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.

How do I align content vertically?

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.

How do I vertically align an element in CSS?

To get them centered along a line, you'd use vertical-align: middle; . Although note that it centers the text according to its tallest ascender and deepest descender. Each element lines up according to the line you've set, which doesn't change from element to element.


2 Answers

I have researched this a little and from what I have found you have four options:

Version 1: Parent div with display as table-cell

If you do not mind using the display:table-cell on your parent div, you can use of the following options:

.area{     height: 100px;     width: 100px;     background: red;     margin:10px;     text-align: center;     display:table-cell;     vertical-align:middle; }​ 

Live DEMO


Version 2: Parent div with display block and content display table-cell

.area{     height: 100px;     width: 100px;     background: red;     margin:10px;     text-align: center;     display:block; }  .content {     height: 100px;     width: 100px;     display:table-cell;     vertical-align:middle;     }​ 

Live DEMO


Version 3: Parent div floating and content div as display table-cell

.area{     background: red;     margin:10px;     text-align: center;     display:block;     float: left; }  .content {     display:table-cell;     vertical-align:middle;     height: 100px;     width: 100px; }​ 

Live DEMO


Version 4: Parent div position relative with content position absolute

The only problem that I have had with this version is that it seems you will have to create the css for every specific implementation. The reason for this is the content div needs to have the set height that your text will fill and the margin-top will be figured off of that. This issue can be seen in the demo. You can get it to work for every scenario manually by changing the height % of your content div and multiplying it by -.5 to get your margin-top value.

.area{     position:relative;      display:block;     height:100px;     width:100px;     border:1px solid black;     background:red;     margin:10px; }  .content {      position:absolute;     top:50%;      height:50%;      width:100px;     margin-top:-25%;     text-align:center; }​ 

Live DEMO

like image 98
Josh Mein Avatar answered Sep 28 '22 15:09

Josh Mein


This could also be done using display: flex with only a few lines of code. Here is an example:

.container {   width: 100px;   height: 100px;   display: flex;   align-items: center; } 

Live Demo

like image 38
martin36 Avatar answered Sep 28 '22 17:09

martin36