Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does HTML5 ignore line-height smaller than font-size?

Tags:

html

css


i'm switching some pages over to HTML5 which contains headlines that need to be set with a really small line height. Now since <!DOCTYPE html> any line-height below the font-size is ignored. I can space them out all I want, but no chance bringing them closer together. Anyone know why that is and if it's cureable?
Thanks, thomas

Edit: Found it. My old markup was <a style="line-height:12px;" href="#">something</a> which worked in XHTML 1.0 transitional but not in HTML5.
I changed it to <div style="line-height:12px;"><a href="#">something</a> an that works!
Thanks!

like image 424
thomas Avatar asked Feb 16 '11 09:02

thomas


People also ask

Can line height be less than font size?

If the value of the line-height property is smaller than the value of the font-size , text may overflow the element, and text on one line may overlap text on another line above or below it. It is often convenient to set the value of the line-height in the font shorthand property.

Does line height change with font size?

Sets line height to be equal to a multiple of the font size. If your font size is 10px, your line height will be 10px, 18px, and 20px, respectively. Sets line height as a percentage of the font size of the element.

How do you calculate line height from font size?

For the optimal readability and accessibility, you should go with140 – 180% line height (this is the space around a line of text). This means if your font size is 16pt, your line height should be at least 16 x 1.4 = 22.4pt (140%), or 16 x1. 8= 28.8pt (180%) maximum value.

What is the default line height in HTML?

The default line height in most browsers is about 110% to 120%. This is a paragraph with a smaller line-height. This is a paragraph with a smaller line-height. This is a paragraph with a bigger line-height.


1 Answers

Your <a> tag is an inline element and it appears in HTML5 inline elements defer to its parent 'block' element's line-height ( or all the way up to the <body> style if that is the immediate parent ).

Example:

body { line-height:20px; }  a { line-height:12px; } 

and this markup:

<body>     <a href="#">test</a> </body> 

The <a> tag will have a line-height of 20px not 12px.

So your 'inline' <a style="line-height:12px;" href="#">something</a> didn't work but did when you wrapped it in the 'block'-level <div> element because block elements can dictate line-height.

A better way than bloating your markup by wrapping your inline element in a block element, just use CSS to make the tag display 'inline-block'.

<a style="display:inline-block; line-height:12px;" href="#">something</a>

Even better, give your <a> a class (change 'xx' below to something semantic):

<a class="xx" href="#">something</a> 

Then in your CSS file set that class to 'inline-block':

.xx { display:inline-block; line-height:12px; } 

Hope that helps.

like image 80
ucsarge Avatar answered Sep 22 '22 00:09

ucsarge