Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Size Text With rem and em

In this lesson, you sized the headings and lists of the seed swap page you built in HTML in rem, watched em sizes shrink at every level of a nested list, and saw rem follow the browser’s font size setting while pixels didn’t. Along the way, you met the Computed pane.

Key Points

  • The Computed pane sits next to Styles in the Elements panel. It shows each property’s final value for the selected element, and font sizes show in pixels. Type font-size into its filter to see one property. The What to Bring heading started at 24 pixels.
  • Show all: with the <html> element selected, the filtered list is empty. The browser’s own rules size headings, but nothing, not even the browser, sets the root’s font size. Check Show all to list every property, and leave it on.
  • The root’s font size is the font size of the <html> element, 16 pixels by default. It comes from your browser’s font size setting, and the browser’s default sizes build on it. A heading’s 24 pixels is one and a half times 16.
  • rem: one rem is the root’s font size, so 1.25rem is 1.25 × 16 = 20 pixels, and the Computed pane reads 20px.
  • em, in font-size, is the font size of the element’s parent, which is why rem is short for root em. With li { font-size: 0.8em; }, Vegetables is 0.8 of 16, 12.8 pixels. Beans’ list sits inside Vegetables and takes on its 12.8 pixels, so Beans is 0.8 of 12.8, 10.24 pixels, and Runner beans is 8.192.
  • Watch out: with em, each nested item multiplies by 0.8 again, so the deeper it sits, the smaller it gets. With 0.8rem, every item is 12.8 pixels, however deep.
  • The visitor’s setting: in Chrome’s settings, under Appearance, Font size set to Very large is 24 pixels instead of 16. The root became 24 pixels, the headings 30 and the list items 19.2. Set Font size back to Medium (Recommended) when you’re done.
  • Watch out: 1rem and 16px only match until a visitor changes the setting, and pixels ignore it. Headings written as 20px stayed at 20 pixels at Very large, smaller than the 24-pixel paragraphs under them. Zooming the page grows pixels too, but many people use the font size setting instead.
  • The rule: size text in rem. Every rem measures from the root, which follows the visitor’s setting as long as you don’t give html a font size in pixels, as in html { font-size: 16px; }. em suits things that grow with their own text, which you’ll meet with spacing.
  • Other units you’ll meet: vw and vh are hundredths of the viewport’s width and height, the area the page is drawn in. % is a share of something that depends on the property, and ch is the width of a zero in the current font, a rough guide to line length. They’re mostly for layout, which comes later.

Final Code

Rosa’s new list in events/seed-swap.html, after the What to Bring list:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Your notice’s colors are readable now. Next is size. Some visitors tell their browser to show all text bigger, and the unit you pick decides whether your page listens.

00:11 The seed swap page shares events.css with the notice, so it has the same greens on honeydew. Rosa would like its section headings a little smaller. She’s also added the seeds people are looking for, as a list inside a list inside a list.

00:26 Copy it from the text below the video into your page, after the What to Bring list.

00:32 Inspect the What to Bring heading, and next to Styles, click Computed. This pane shows each property’s final value for the selected element. Type font-size into its filter.

00:44 Font sizes show in pixels. This heading’s is 24 pixels.

00:49 Now select the html element at the top of the tree, the page’s root. The list is empty. The browser’s own rules size headings, but nothing sets the root’s.

01:00 Check Show all to list every property, and leave it on. The root’s font size is 16 pixels. It comes from your browser’s font size setting, and the heading’s 24 is one and a half times 16.

01:14 You could size headings in pixels, but CSS has a unit made for text. In events.css, add a font size of 1.25rem to your existing h2 rule.

01:26 One rem is the root’s font size, so 1.25rem is 1.25 times 16. Save, and the headings shrink. The Computed pane reads 20 pixels.

01:40 Em scales from a font size too. In font-size, one em is the parent’s. That’s why rem is short for root em. In a new rule at the end of the file, give every list item 0.8em, and save.

01:55 Walk down Rosa’s list in the Computed pane. Vegetables is 0.8 of 16, 12.8 pixels. Beans’ list sits inside Vegetables and takes on its 12.8 pixels, so Beans is 0.8 of 12.8, 10.24 pixels.

02:13 Runner beans is 0.8 of that again, 8.192 pixels. Watch out: each nested item multiplies by 0.8 again, so the deeper it sits, the smaller it gets. Change it to 0.8rem, and every item is 12.8 pixels, however deep.

02:34 In Chrome’s settings, under Appearance, set Font size to Very large. That’s 24 pixels instead of 16.

02:43 Everything on the page grew. The root is now 24 pixels, the headings 30, and the list items 19.2.

02:52 Say you’d written the headings as 20 pixels instead. At Medium, that looks the same as 1.25rem. At Very large, the pixel headings stay at 20, smaller than the paragraphs under them.

03:04 Watch out: 1rem and 16 pixels only match until a visitor changes the setting, and pixels ignore it. Zooming the page grows pixels too, but many people use this setting instead.

03:16 Set Font size back to Medium when you’re done.

03:21 So size text in rem. Every rem measures from the root, which follows the visitor’s setting as long as you don’t give html a font size in pixels. Em suits things that grow with their own text, which you’ll meet with spacing.

03:36 Other people’s CSS uses other units too. vw and vh are hundredths of the viewport’s width and height, the area the page is drawn in. Percent is a share of something that depends on the property.

03:49 ch is the width of a zero in the current font, a rough guide to line length. They’re mostly for layout, which comes later. That’s all you need about them for now.

03:59 Your seed swap page now sizes its text in rem.

04:04 Next is Exercise: Style the Work Day Notice, where you’ll give Rosa’s notice its colors and text sizes. It’s the course’s first exercise. You write CSS in the browser, run it, and automated tests tell you what’s working.

04:18 Run it early and often.

Become a Member to join the conversation.