Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS vertically align floating divs

Tags:

html

css

I have a div (#wrapper) containing 2 divs standing side by side.

I would like the right-div to be vertically aligned. I tried vertical-align:middle on my main wrapper but it is not working. It is driving me crazy!

Hope someone can help.

http://cssdesk.com/LWFhW

HTML:

<div id="wrapper">   <div id="left-div">     <ul>       <li>One</li>       <li>Two</li>     </ul>   </div>     <div id="right-div">     Here some text...   </div> </div> 

CSS:

#wrapper{   width:400px;   float:left;   height:auto;   border:1px solid purple;}  #left-div{   width:40px;   border:1px solid blue;   float:left;}  #right-div{   width:350px;   border:1px solid red;   float:left;}  ul{   list-style-type: none;   padding:0;   margin:0;} 
like image 212
Marc Avatar asked Feb 24 '12 12:02

Marc


People also ask

How do you vertically center a floating DIV?

Here it is in two easy steps: Add ' display: flex ' to our container element. Add ' align-items: center ' to our container element.

How do you vertically align floated elements?

By creating a wrapper around the content you want floated, you can then use the ::after or ::before pseudo selectors to vertically align your content within the wrapper. You can adjust the size of that content all you want without it affecting the alignment.

How do I align vertically in CSS?

CSS Demo: vertical-align The vertical-align property can be used in two contexts: To vertically align an inline element's box inside its containing line box. For example, it could be used to vertically position an image in a line of text. To vertically align the content of a cell in a table.

Is vertical-align deprecated?

Deprecated as an attribute Occasionally you will see “valign” used on table cells to accomplish vertical alignment. e.g. <td valign=top> . It should be noted that this attribute is deprecated and should not be used.


2 Answers

You have no luck with floated elements. They don't obey vertical-align,

you need, display:inline-block instead:

http://cssdesk.com/2VMg8

BEWARE


Be careful with display: inline-block; as it interpretes the white-space between the elements as real white-space. It does not ignores it like display: block does.

I recommend this:

Set the font-size of the containing element to 0 (zero) and reset the font-size to your needed value in the elements like so

ul {     margin: 0;     padding: 0;     list-style: none;     font-size: 0; } ul > li {     font-size: 12px; } 

See a demonstration here: http://codepen.io/HerrSerker/pen/mslay


CSS

#wrapper{   width:400px;   height:auto;   border:1px solid green;   vertical-align: middle;   font-size: 0; }  #left-div{   width:40px;   border:1px solid blue;   display: inline-block;   font-size: initial;   /* IE 7 hack */   *zoom:1;   *display: inline;   vertical-align: middle; }  #right-div{   width:336px;   border:1px solid red;   display: inline-block;     font-size: initial;   /* IE 7 hack */   *zoom:1;   *display: inline;   vertical-align: middle; }    
like image 92
yunzen Avatar answered Sep 24 '22 14:09

yunzen


You can do this quite easily with display table and display table-cell.

#wrapper {     width: 400px;     float: left;     height: auto;     display: table;     border: 1px solid green; }  #right-div {     width: 356px;     border: 1px solid red;     display: table-cell;     vertical-align: middle; } 

EDIT: Actually quickly messed around on CSS Desk for you - http://cssdesk.com/RXghg

ANOTHER EDIT: Use Flexbox. This will work but it's pretty outdated - http://www.cssdesk.com/davf5

#wrapper {     display: flex;     align-items: center;     border:1px solid green; }  #left-div {     border:1px solid blue; }  #right-div {     border:1px solid red; } 
like image 27
SpaceBeers Avatar answered Sep 26 '22 14:09

SpaceBeers