HTML, CSS and JavaScript basics tutorial

Setting CSS font size: understanding em, percent, and inheritance

All videos of the tutorial HTML, CSS and JavaScript basics tutorial
Jan Brinkmann
By Jan Brinkmann Last updated on

Every text on the web has a size, but who decides it? The answer lies in the CSS property font-size. With it, you define how large headings, paragraphs, and other text elements are displayed. In this guide, you will learn how to first set font sizes in pixels, why relative units like em and percent are the better choice, and how inheritance simplifies your design.

Basics HTML, CSS and JavaScript - 35 Set font size

The video is only loaded from YouTube when you play it – data (including your IP address) is then transmitted to YouTube/Google. More in the privacy policy

Key Takeaways

  • You set the font size with the font-size property in CSS.
  • Pixel values are fixed and do not automatically adapt to different screen sizes.
  • The default font size in most browsers is 16 pixels, and body text should not be smaller than that.
  • Relative units like em and percent refer to the font size of the parent element.
  • Due to inheritance, an adjustment to the body changes all elements contained within it.

Step-by-Step Guide

Step 1: Understand the Starting Point

Imagine a simple document that contains only an H1-level heading and a paragraph. This is exactly the structure you will work with in the following steps. The heading is the H1 element, and the paragraph is a normal block of text. You can style both independently of each other.

Setting CSS font size: understanding em, percent, and inheritance

Step 2: Set the Heading's Font Size in Pixels

To change the size of the heading, you write a rule for the H1 element in your CSS and set the font-size property to a pixel value, for example 32 pixels. If you save the file and reload the page, the heading stays at this size. If you then try 75 pixels, the heading becomes significantly larger. If you set 15 pixels instead, it even looks smaller than the body text. So you can see right away how strongly the value has an effect.

h1 {
  font-size: 32px;
}
Setting CSS font size: understanding em, percent and inheritance

Step 3: Use the Default Size of 16 Pixels as a Guideline

Most browsers use a font size of 16 pixels for normal text. This value is no coincidence, because it is easy to read, even when eyesight declines. You should therefore generally not make body text smaller. For footnotes, image captions, or notes, however, a smaller font is perfectly reasonable. The actual body text, though, should be at least 16 pixels.

Setting CSS font size: understanding em, percent and inheritance

Step 4: Why Fixed Pixel Values Reach Their Limits

Before you dive deeper, it is worth taking a look at the problem with fixed values. In the past, screen width was fairly uniform. If a page was 1000 pixels wide, you could specify exactly that the heading should be 24 pixels and the body text 16 pixels. Today, people view websites on smartphones, tablets, and monitors with very different resolutions. You cannot predict at which resolution a visitor will open your page. Therefore, you should not rely too heavily on rigid pixel values.

Setting CSS font size: understanding em, percent, and inheritance

Step 5: Set the Base Font Size with Percentage Values

A more flexible solution starts with the body element. If you set font-size there to 100 percent, this corresponds to the browser's default size, so usually 16 pixels. You can verify this using your browser's developer tools by inspecting the element and looking at the computed font size.

It gets interesting when you then inspect the H1 heading. There, the browser displays about 32 pixels, even though you have not set a value. This is because the browser uses a size of 200 percent of the parent font size for H1 headings by default. So the size is not fixed, but results relative to its surroundings.

body {
  font-size: 100%;
}
Setting CSS font size: understanding em, percent, and inheritance

Step 6: Style Headings Relatively with em or Percent

You can now set the size of the heading relatively yourself. If you want it to be only one and a half times as large as the base value, for example, you set font-size to 150 percent or alternatively to 1.5 em. Both values have the same effect, because 1 em corresponds to 100 percent of the parent font size. The heading thus becomes about 24 pixels in size and fits better with the rest of the document.

h1 {
  font-size: 1.5em;
}
Setting CSS font size: understanding em, percent, and inheritance

Step 7: Using Inheritance in the Document

Now it gets interesting. The heading and the paragraph are nested inside the body element. They therefore inherit its font size. If I change the size of the body, this automatically affects all contained elements. At the same time, you can adjust individual elements specifically. If you set font-size to 75 percent for all paragraphs, they become smaller than the base value. In the example, the size drops from 16 to 12 pixels.

p {
  font-size: 75%;
}
Setting CSS font size: understanding em, percent, and inheritance

It is important that percentage values always refer to the element in which the respective element is located. This means that a value for the paragraph refers to the size of the surrounding element, not to the page as a whole.

Setting CSS font size: understanding em, percent, and inheritance

Step 8: Trying Out a Change to the body as a Calculation Example

Now set the base font size of the body to 20 pixels. Everything on the page becomes larger. At 1.5 em, the heading is mathematically 30 pixels. The paragraph at 75 percent reaches 15 pixels. The calculation checks out, because 75 percent of 20 pixels gives 15 pixels. With this example, you can see that all sizes depend dynamically on the base value. A single change is enough to adjust the entire document.

Setting CSS font size: understanding em, percent, and inheritance

Step 9: Choosing Relative Units Instead of Pixels as the Default

It is recommended to define font sizes generally using em or percent rather than fixed pixel values. This way, your layout stays consistent even when the resolution changes. With so-called media queries, you can additionally check how wide the display device is. For a narrow view of about 300 pixels in width, you then simply change the base font size, for example to 14 pixels. All elements that relate to this value relatively adapt automatically. So you don't have to rework every element individually.

Setting CSS font size: understanding em, percent, and inheritance

Step 10: Outlook on Font Families

You have now learned the basics of font sizes. The next step is about fonts and font families. There you will learn how to influence the appearance of your text even more precisely through the typeface.

Setting CSS font size: understanding em, percent and inheritance

Summary – Setting Font Size with CSS: Comparing Pixels, em, and Percent

With font-size you control the size of your text. Fixed pixel values are easy to understand but do not adapt flexibly to different screens. Relative values such as em and percent refer to the parent font size and ensure that you can adjust the entire document with a single change to the base value. Inheritance makes this approach particularly effective. Make sure that body text stays at least 16 pixels in size, and use relative units as the default.

FAQ

Why are pixel values problematic for font sizes?Pixel values are fixed and do not change when the visitor's screen size or resolution changes.

What minimum font size should body text have?Body text should generally be at least 16 pixels in size, as this corresponds to the default size of most browsers and is easy to read.

What is the difference between em and percent?Both values refer to the parent font size, where 1 em corresponds exactly to 100 percent.

What does inheritance mean for font sizes?Child elements adopt the font size of their parent element, for example the body, unless you specify otherwise.

How can I adjust the font size for small screens?With media queries, you can check the screen width and change the base font size in that case, so that all relatively defined elements adapt automatically.

1220,1256,274
Jan Brinkmann
Trainer About Jan Brinkmann Web development with passion! Jan has been a developer for over 15 years. All courses by Jan Brinkmann