tags:

views:

692

answers:

3

My basic CSS rule for font-size and line-height is pretty simple:

body {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 16px;
    line-height: 24px;
}

When I create a paragraph that contains an <em> or a <strong> (or even some <code> which uses a different font-size than the <body>), I notice that these elements increase the line-height by a pixel or two.

This leads me to believe that most browsers take the largest inline element to display line-height. Is that right?

My goal is to make the line-height consistent, regardless of which elements are inline.

The only way I've found to make the line-height consistent is to define:

em, strong, code, etc {
    line-height: 100%;
}

Is this the right way to go about this? Or am I being too much of a perfectionist?

Edit 1:

This also seems to work:

em, strong, code, etc {
    line-height: 1;
}

Edit 2:

After playing with this for a few days, it doesn't appear that there is a reliable solution for keeping a consistent line-height. If anyone has any ideas, I'd certainly love to hear them.

Edit 3:

For my own future reference and from my own research, these tags should be considered with line-height: 1 as long as they are displayed inline along with other standard body text:

abbr, acronym, b, big, cite, code, del, dfn, em, i, ins, kbd, q,
samp, small, strong, sub, sup, tt, var

These elements are questionable:

a, bdo, img, span
A: 

I think that's a pretty neat idea.

However, don't forget that if the bold/italic text is too large then you could impair readability by making all the lines really tall when only one needs to be. You may also want something to deal with superscript and subscript, if your example doesn't already take care of it.

DisgruntledGoat
Ooooo yeah, working on sub and sup now, but about ready to give up. Lol
Jeff
Hmm, just came back to this and I don't think what I posted is right. Setting the line-height to 100% doesn't keep it consistent - the line with the larger text is larger, but subsequent lines go back to normal. Your example of setting the line height to 0 keeps all the lines to their original height - in this case you need to make sure the larger text doesn't overlap the other lines.
DisgruntledGoat
A: 

I'm not seeing this behavior, but I'm using WebKit. Does the problem happen for you if you throw some bold tags into this example?

http://www.w3schools.com/css/tryit.asp?filename=trycss%5Fline-height

Azeem.Butt
Yep, it sure does (FF 3.0.x). I'm working on this problem now and it doesn't seem like there's an amicable solution.
Jeff
Probably not. Web typography is several hundred years behind the rest of the world.
Azeem.Butt
A: 

The spec confirms what you thought, that browsers take the largest font for the line-height calculation. That means your only option (and the one the spec mentions) is to set the line height for ALL the inline boxes on lines you care about. You probably would not be happy with the results of selecting body * and setting the font size. So add some Divs with the same class around whatever blocks of text you want to look uniform, and there you go:

div.uniformlines * { 
    line-height: 100%;
    font-size: 16px;
}
Willfulwizard
Great info! What about `p * { line-height: 0; }`, or better yet maybe `p a, p code, p big { line-height: 0; }`? Now we're getting somewhere. `;)`
Jeff
Followup to my last comment. Be careful with my code above. If you have, for example, `<p><a href="#">foo</a></p>`, the line-height will calculate to zero, which is probably not what you want.
Jeff