Computer scienceFrontendCSSBasic propertiesWorking with Text

line-height

4 minutes read

In this topic, we will talk about the line-height property. This property helps to specify the distance before and after inline elements. Browsers define its value automatically depending on the family and size of the text font, but we can change it if needed.

Purpose and usage

This property quite literally determines the height of a line.

Three bordered text lines

Most often line-height is used to change the distance between lines in text. Varying the line spacing makes the text more readable and pleasing to the eye.

To better understand the importance of the right line-height selection, let's look at the three ways of formatting one quote:

div {
    line-height: 0.7;
}

Representation of low line height between text lines

div {
    line-height: 1.5;
}

Representation of large line height between text lines

div {
    line-height: 2.7;
}

Very large line height

The second option seems to be the best, not too cramped, not too spacey. So how can we specify the desired value?

Basic value

The first value that line-height gets from the browser is normal. This value is calculated specifically for each case and is used if we don't intend to write it on our own. The value depends on the family and size of the text font and usually roughly equals the font size multiplied by 1.2.

div {
    line-height: normal;
}

Normal line height

Well, if browsers set the value themselves, why would we want to change it? It turns out that the default option isn't always good enough. For example, if we set a small font size and normal line-height, the result might be undesirable:

div {
    line-height: normal;
}

Normal line height on the big text

It will be nicer if we add some space to the text:

Large line height on the big text

So how can we do it?

Unitless values

Let's replace normal with some non-negative number for the gray quote above and compare the results:

div {
    line-height: 1.5;
}

Medium line height

So what did just happen? The browser took the number that we specified in the property and multiplied it by the font size. The resulting number is the new line-height pixel value.

For most fonts recommended unitless line-height ranges from 1.5 to 1.7.

Length values

We can also use any of the CSS standard units like px, em, cm, and so on. Let’s review some examples.

div {
    line-height: 20px;
}

The text with normal line height on the green background

div {
    line-height: 50px;
}

Very big line height text on the green background

div {
    line-height: 1.5em;
}

Normak line height text on the beige background

div {
    line-height: 3em;
}

Sans-serig text with the big line-height on beige background

Percentage values

One more way to set the line-height value is “number + %”. Here we do the same thing as with the unitless values: we write how many times the font size needs to be increased using percentages. For example, if we have the font size equal to 20px and the line-height equal to 150%, then the browser calculates 150% of 20px, so the final line-height is 30px.

Here’s the last set of examples for this topic:

div {
    line-height: 50%;
}

Handwritten text with small line height on the purple background

div {
    line-height: 150%;
}

Handwritten text with normal line height on the purple background

div {
    line-height: 300%;
}

Handwritten text with big line height on the purple background

Conclusion

There are many ways to make the text on your website more esthetically pleasing and readable. Adding the right amount of space to the text is one of them.

So, let's move on to solving the problems!

214 learners liked this piece of theory. 1 didn't like it. What about you?
Report a typo