DEV Community

Cover image for Quick CSS Quiz #3
Abdelrahman Ismail
Abdelrahman Ismail

Posted on • Updated on

Quick CSS Quiz #3

In this series, I try to focus on CSS weird parts, throw quiz and its answer, hoping to give a better understanding of how CSS works in depth.

The problem:

Having the following code snippet, for a paragraph element with font-size equals 12px, you want the paragraph's line-height to be equals to the double of its font-size(24px). Which of the following line-height values will achieve this if you apply it to the body element?
(note that the default font-size for browser is 16px)

<p style="font-size: 12px">Having fun solving CSS Quiz</p>

<style>
body {
  line-height: <value>;
}
</style>
Enter fullscreen mode Exit fullscreen mode

Options:

  • 200%
  • 2em
  • 2
  • double

The answer:

you can find the answer below, but please give yourself a couple of minutes to think about it, to make sure you benefit from this quiz.






๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡





The correct answer is: "2" ๐ŸŽ‰๐ŸŽ‰
If you wonder why this is the correct answer, please read the discussions below, I will post the explanation soon.

Cover image original artwork by: Sergi Delgado

Latest comments (9)

Collapse
 
equiman profile image
Camilo Martinez • Edited

No matter if always I miss the answer... I love this CSS quiz.

I choose 2em ... jajajaja. Now I know a little more about CSS.

CSS Quiz 3 - Equiman 0

Collapse
 
ismail9k profile image
Abdelrahman Ismail

Thank you ๐Ÿค—๐Ÿค—, this is the purpose of this series, to give you a deep understanding of how CSS really works.

Collapse
 
equiman profile image
Camilo Martinez

This series of post deserve to be in #challenge tag

Collapse
 
ismail9k profile image
Abdelrahman Ismail

To solve this quiz correctly, we should understand the line-height behavior with different values.

<length>:
The browser compute the line-height value for the current element, then nested child elements will inherit this fixed value form it.
For the first two options (which produce same results, gotcha!)
body line-height = 16 * 2 = 32px
p line-height = 32px inherited from body element

<number> (unitless):
Tells the browser to every nested element line-height value is this number multiplied by the element's own font size. In most cases, this is the preferred way to set line-height and avoid unexpected results due to inheritance.
For 2 option
body line-height = 16 * 2 = 32px
p line-height = 12 * 2 = 24px (which is the correct answer)

For more info:
line-height MDN
3 things (almost) no one knows css

Collapse
 
andrerpena profile image
Andrรฉ Pena

How is this different from 2em?

Collapse
 
ismail9k profile image
Abdelrahman Ismail

That's why this may seem tricky, CSS has a completely different behavior when using line-height with em rather than using unit-less value,

Collapse
 
equinusocio profile image
Mattia Astorino • Edited

This is very easy. A line-height with unitless value rappresent the ratio based on the element font-size computed value (or inherited) and allows children to compute their line-height based on their font-size.

Ps: same โ€œquizโ€ here.
allthingssmitty.com/2017/01/30/nop...

Collapse
 
ismail9k profile image
Abdelrahman Ismail • Edited

It's very easy if you already know how line-height with unit-less value works ๐Ÿ˜‰, but unfortunately most developers go with 2em.

And the original quiz posted by Kevin Yank here: 3 things (almost) no one knows css

Collapse
 
equinusocio profile image
Mattia Astorino

Yes but unitless line height is just one of the accepted value. Itโ€™s not a โ€œtrickโ€ like the previous quiz.