Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I vertically align elements in a div?

I have a div with two images and an h1. All of them need to be vertically aligned within the div, next to each other.

One of the images needs to be absolute positioned within the div.

What is the CSS needed for this to work on all common browsers?

<div id="header">   <img src=".." ></img>   <h1>testing...</h1>   <img src="..."></img> </div> 
like image 418
Abdu Avatar asked Sep 17 '08 02:09

Abdu


People also ask

How do I vertically align an item in a div?

For this to work, you need to have a parent container with the display: table; property, and inside that container you will have the number of columns you want to have centered, with the display: table-cell; (and vertical-align: middle; ) property.

How do you vertically align a div in CSS?

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

How do I vertically align a div inside a div?

Vertically centering div items inside another div Just set the container to display:table and then the inner items to display:table-cell . Set a height on the container, and then set vertical-align:middle on the inner items.

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.


2 Answers

Now that Flexbox support is increasing, this CSS applied to the containing element would vertically center the contained item:

.container {     display: flex;     align-items: center; } 

Use the prefixed version if you also need to target Internet Explorer 10, and older (< 4.4 (KitKat)) Android browsers:

.container {     display: -ms-flexbox;       display: -webkit-flex;     display: flex;         -ms-flex-align: center;     -webkit-align-items: center;        -webkit-box-align: center;         align-items: center; } 
like image 28
E. Serrano Avatar answered Sep 19 '22 06:09

E. Serrano


Wow, this problem is popular. It's based on a misunderstanding in the vertical-align property. This excellent article explains it:

Understanding vertical-align, or "How (Not) To Vertically Center Content" by Gavin Kistner.

“How to center in CSS” is a great web tool which helps to find the necessary CSS centering attributes for different situations.


In a nutshell (and to prevent link rot):

  • Inline elements (and only inline elements) can be vertically aligned in their context via vertical-align: middle. However, the “context” isn’t the whole parent container height, it’s the height of the text line they’re in. jsfiddle example
  • For block elements, vertical alignment is harder and strongly depends on the specific situation:
    • If the inner element can have a fixed height, you can make its position absolute and specify its height, margin-top and top position. jsfiddle example
    • If the centered element consists of a single line and its parent height is fixed you can simply set the container’s line-height to fill its height. This method is quite versatile in my experience. jsfiddle example
    • … there are more such special cases.
like image 138
Konrad Rudolph Avatar answered Sep 21 '22 06:09

Konrad Rudolph