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>
edit just re-read the questions and saw one box needs floating to the right... so the below doesn't work... but might be adaptable
First of all, you should be using spans rather than divs as the content is going to be inline to finish with. I don't know the ins and outs of why, but this means your elements will behave a bit friendlier across browsers.
Once you've done that, this works a treat, even in ie6 and 7:
Do you mean baseline in the typographic sense? (That is, each line of text is level with a corresponding line in the other column) If that's the case, the font sizes need to be multiples of each other -- for example, 12 and 18px (1:1.5).
If you mean the divs need to be the same height, there isn't an easy way to do this. You can manually set a height (height:100px;), or use javascript to adjust one as the other changes.
Or, you can fake the same height by enclosing the two divs in a container, and then applying a background style to the container that mimics the look of the columns, setting it to repeat vertically. That way, you get faux columns. For an in-depth look, see this classic A List Apart article.
Did you mean, you have two pieces of text, and both need to be at the bottom of the columns? (sorry, can't post an image yet)
One way you can do this is using the Faux Columns method above.
The other way is to set up the text in its own container inside the text. Then, position both absolutely at the bottom of the columns ... here's a long snippet:
Ok, firstly the pure CSS way. You can get vertical alignment to the bottom using relative+absolute positioning like this:
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:
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.
You can do this using line-height but it only works on inline elements and if the text does not wrap.
I changed the
div
tospan
. If you want to keepdiv
just adddisplay:inline
to your style.If you put the right floated div before the left floated div in the HTML and they'll line up vertically.
Alternatively, you can float both divs left - that's perfectly valid - and how most CSS designs are coded.