views:

50

answers:

2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
</head>
<body>
<br /><br /><br />
<div style="line-height:150%">
<span style="display:block;font-size:240%;">test</span>
</div>
</body>
</html>

First double click on the text test,then it's highlighted,

then you can see via firebug that the <span> it's in is shorter than the text itself.

How to make the span automatically grow with the text?

+3  A: 

Try changing line-height to 1.5

K Prime
Oh,seems working.Why?What's the difference?
`line-height: 150%` will make it 1.5 times whatever the line-height of the `div` is, and fix it for the children as well, despite the children's `font-size` being bigger. `line-height: 1.5` means it'll be 1.5 times of the whatever each element's `font-size` is - for your example, the `line-height` of the span is `2.4 * 1.5 * <font-size of div>`
K Prime
K Prime is correct. Also, see http://stackoverflow.com/questions/2040788/whats-the-difference-between-line-height1-5-and-line-height150-in-css/2040799#2040799
Mathias Bynens
+1  A: 

This is your original code:

<div style="line-height: 150%;">
 <span style="display: block; font-size: 240%;">Test</span>
</div>

This sets the line-height of all the elements inside the div to 150%. This percentage is relative to their computed font size. The span inherits the font-size of the div, and the line-height is calculated based on that. You set the font-size of the span to 24 times its inherited size, but the line-height is unaffected.

In short: line-height: 150% will take 150% of the element’s computed font size to compute the line height, which is equivalent to multiply it by 1.5.


As mentioned, using line-height: 1.5; instead fixes that behavior:

<div style="line-height: 1.5;">
 <span style="display: block; font-size: 240%;">Test</span>
</div>

This triggers the recalculation of the line-height based on the new font size. It sets the line-height of all the elements inside the div to 1.5 times. This percentage is relative to their actual font size. The span inherits the font-size of the div. You set the font-size of the span to 2.4 times its inherited size, and the new line-height is calculated based on that.

In short: line-height: 1.5 (without units) will multiply the element’s actual font size by 1.5 to compute the line height.

Mathias Bynens
But isn't the `span`'s computed font size 150% already?
Nope. Its computed `line-height` is `150%`, but its `font-size` is just inherited (`100%`), then reset to `240%`. `240%` (`font-size`) > `150%` (`line-height`) — that’s why, in this case, you should use the dynamic `line-height: 1.5` instead of the static `line-height: 150%`. [Terminology courtesy of K Prime ;)]
Mathias Bynens