Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertically align floating divs

Tags:

css

I am trying to float two divs with different font-sizes. I can't find a way to align the text on the same baseline. Here is what I have been trying:

<div id="header">     <div id="left" style="float:left; font-size:40px;">BIG</div>     <div id="right" style="float:right;">SMALL</div> </div> 
like image 265
chellmuth Avatar asked Jun 20 '09 23:06

chellmuth


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 items in CSS?

Use the CSS line-height property Add the line-height property to the element containing a text larger than its font size. By default, equal spaces will be added above and below the text, and you'll get a vertically centered text.

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.

How do you align text vertically?

1 Select the text you want to center between the top and bottom margins. 2 On the Page Layout tab, click the Page Setup Dialog Box Launcher. 3 Select the Layout tab. 4 In the Vertical alignment box, click Center 5 In the Apply to box, click Selected text, and then click OK.


1 Answers

Ok, firstly the pure CSS way. You can get vertical alignment to the bottom using relative+absolute positioning like this:

<div id="header">   <div id="left">BIG</div>   <div id="right">SMALL</div> </div> <style type="text/css"> html, body { margin: 0; padding: 0; } #header { position: relative; height: 60px; } #left { font-size: 40px; position: absolute; left: 0; bottom: 0; } #right { position: absolute; right: 0; bottom: 0; } </style> 

You may have some issues with this, like IE6 behaviour as well as z-index issues with things like menus (last time I tried this my menus appeared under the absolute content).

Also, depending on whether all the elements need to be absolutely positioned or not, you may need to start doing things like specifying the height of the containing element explicitly, which is usually undesirable. Ideally, you'd want the container to auto-size for its children.

The problem is that the baselines of the different-sized fonts will not match up and I don't know of a "pure" CSS way of doing this.

With tables however the solution is trivial:

<table id="header"> <tr>   <td id="left">BIG</td>   <td id="right">SMALL</td> </tr> </table> <style type="text/css"> #header { width: 100%; } #header td { vertical-align: baseline; } #left { font-size: 40px; } #right { text-align: right; } </style> 

Try it and you'll see it works perfectly.

The anti-table crowd will scream blue murder but the above is the simplest, most browser-compatible way (particularly if IE6 support is required) of doing this.

Oh and always prefer using classes to inline CSS styles.

like image 176
cletus Avatar answered Nov 10 '22 18:11

cletus