I am displaying two DIV panels with the code below. The only difference between the two panels is that the SPAN element within the first DIV element has a font-size: 14px style applied to it while that of the second DIV element doesn’t have this style. On Firefox as well as Chrome the first panel appears to have a greater height than the second one. Here is a demo URL: http://jsfiddle.net/UWX2u/
A copy of the code follows:
<!DOCTYPE html>
<html>
<head>
<title>Margin border</title>
<style type="text/css">
body {
line-height: 38px;
font-size: 32px;
}
#panel1 {
background: #00a000;
float: left;
}
#panel2 {
background: orange;
float: left;
}
#panel1 p span {
font-size: 14px;
}
</style>
</head>
<body>
<div id="panel1">
<p>Foo <span>Bar</span></p>
</div>
<div id="panel2">
<p>Foo <span>Bar</span></p>
</div>
</body>
</html>
With Firebug I could see that the computed height of the P element in the first DIV is 44.1833px while that of the P element in the second DIV is 38px. Why does this difference occur?
If I remove the line-height: 38px property from the CSS, both DIV elements have the same height. Here is a page that demonstrates this: http://jsfiddle.net/FJUDn/
As to “Why” it Happens
According to W3C (bold emphasis added),
So what this tells me is that
1) the
line-heightcan go taller if need be (it is just a minimum), and2) your shrinking of the font would seem to be shifting how the font applies the minimum above and below the baseline of the
spanfor theline-height: 38px. Apparently the smaller font size redistributes more spacing below the baseline of the smaller font, pushing theptag taller. This seems confirmed in that if you add to thespanavertical-align: topas I did here, then the baseline shifts up for the smaller font, and no size change occurs in theptag.