Essential CSS
JavaScript Roadmap
Learn JavaScript
Mastering Web Typography
Practical Astro
Understanding Async JS
Unorthodox Tailwind
API Masterclass
Build and Deploy
The Beginner Freelancer
Real World Node
Profile Billing Logout
Essential CSS
Lesson Plan
  1. Introduction
    1. Welcome to Essential CSS
    2. Getting started with the course
    3. What are file formats?
    4. Web standards
    5. What is HTML?
    6. What is CSS?
    7. Devtools, your indispensable best friend
  2. CSS Key Concepts
    1. Some background about CSS
    2. CSS Syntax and Terminology
    3. Selectors
    4. Cascade and Specificity
    5. Origin and Importance
    6. Inheritance
    7. How browsers handle CSS
  3. Basic Mental Model
    1. Everything on the web is a box
    2. The box model
    3. How big is your box?
    4. The relationship between px and em
    5. The `box-sizing` property
    6. The `display` property
    7. Formatting contexts
    8. Block boxes
    9. Inline boxes
    10. What is inline-block?
  4. Flexbox and Grid
    1. The parent-child relationship
    2. Sizing in Flexbox
    3. Flex directions
    4. Basic grid concepts
    5. Setting up a grid
    6. Placing grid items
    7. Box alignment
  5. Building web layouts
    1. Arranging your boxes on the web
    2. Positioning schemes
    3. Floating boxes
  6. Typography
    1. Making your text look good
    2. What is a font?
    3. Typesetting with CSS

The relationship between px and em

1m:48s

Earlier when we talked about user agent stylesheets, you learned that every browser provides some sensible default CSS values to ensure that even “unstyled” content is presented in a fairly readable manner.

Devtools user-agent font-size value set to 16px

The sensible default that browsers have set for the font-size property happens to be 16px, and it is an inheritable property. This means the font-size of a parent element also applies to its children. It makes sense for a property like font-size to be inherited because can you imagine how tedious it would be to set the font-size for every single nested element on your web page?

The em unit represents the font size of the element being referenced. This means if you set a margin of 1em on an element, the browser will equate this to the font-size value of the element itself.

Browser resolving 1em to 16px

The browser treats the element’s font size as 1 em unit, so if the font-size value is 16px, then 2em will compute to 32px for that element. Because font-size is an inheritable property, this makes things interesting when it comes to nested elements.

Let’s say you have some markup consisting of nested elements like this:

<body>
<header>This is a header</header>
<main>
<p>This is a paragraph within the main element.</p>
<div>
<p>This is a paragraph inside a div inside the main element. Oh my.</p>
</div>
</main>
</body>

And you have applied some font sizes on each level of nesting:

body {
font-size: 18px;
}
main {
font-size: 1.25em;
}
div {
font-size: 1.2em;
}

The em sizing has a compounding effect as each child element inherits the font-size of its direct parent. This results in the most nested p element having a final computed font-size of 27px. Because 18 _ 1.25 _ 1.2 = 27.

Firefox devtools showing the computation of nested font-sizes

Being aware of this behaviour and understanding how em values work will help you when you are building out (and debugging) full page layouts. We will go into this in greater detail in the later section on layouts.