Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EM's for line-height

I would like to convert my new website from pixels to ems. My question is, should I also apply ems to my text line-height property?

like image 466
Caspert Avatar asked Feb 28 '13 10:02

Caspert


People also ask

Should I use em for line height?

line-height can be set in px , em 's, every unit will fit. line-height works best and future proof if you use a factor/multiplier, meaning no unit, but only a number that is multiplying your font-size. So, Yes, you can, to answer you question: no you should not.

How many pixels is 1.5 line height?

Again, we look to Google's Material Style Guide for the answer. Google runs a 1.5 line height for its body there, or 16px font size and a line-height of 24px. That's also coincidentally where the mega-popular Bootstrap framework sets its default.

How line height is calculated?

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. If your font size is 10px, your line height will be 3px, 5px, and 11px respectively.

What is the recommended line height?

The perfect line height depends on the design and size of the font itself. There is no magic number that works for all text. A line height of 1.5 times the font size is a good place to start from, and then you can adjust accordingly. Using an 8 point grid system works well when using 1.5 line height.


2 Answers

Assuming that “converting to ems” means using the em unit for font-size, then you should set line-height in a manner that also adapts to the font size. The two properties are closely related, and if you set one of them in em and the other (e.g.) in px or pt, then the page will break if the font size is changed. So it would work against the very idea of “using ems” to use essentially different units for essentially connected properties.

For example, if you set font-size: 1.5em and line-height: 18px, then things will depend on the font size of the element’s parent and may go very wrong if that size is much smaller or much larger than expected.

Whether you use the em unit or a pure number is a different issue. Using just a number, as in line-height: 1.2, is primarily equivalent to using the em unit, as in line-height: 1.2em. But there is the difference that when line-height is inherited, it is the pure number that gets inherited, not the computed value.

For example, if an inner element has twice the font size of its parent, then the inherited value 1.2 means that 1.2 times its own font size is used, which is OK. But if the parent had line-height: 1.2em, then the child would inherit a value that 1.2 times the parent’s font size – which is much smaller than its own font size.

for more explanation end examples see line-height @ Mozilla Developer Network

like image 146
Jukka K. Korpela Avatar answered Sep 20 '22 17:09

Jukka K. Korpela


line-height can be set in px, em's, every unit will fit.

line-height works best and future proof if you use a factor/multiplier, meaning no unit, but only a number that is multiplying your font-size.

.foo {   font-size: 1.3em; /* based that 1em == 10px */   line-height: 1.3; /* 16.9px line-height */ } 

So, Yes, you can, to answer you question: no you should not.

just go for the factor based line-height to be future proof.

like image 38
Mark Avatar answered Sep 23 '22 17:09

Mark