Skip to content

Commit 0d44fff

Browse files
feat(curriculum): add interactive examples to em and rem lesson (freeCodeCamp#62955)
Co-authored-by: Huyen Nguyen <25715018+huyenltnguyen@users.noreply.github.com>
1 parent 99d91a1 commit 0d44fff

1 file changed

Lines changed: 20 additions & 5 deletions

File tree

curriculum/challenges/english/blocks/lecture-working-with-relative-and-absolute-units/672bb7f08b58df93ed2a8768.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ challengeType: 19
55
dashedName: what-are-ems-and-rems-in-css
66
---
77

8-
# --description--
8+
# --interactive--
99

1010
In the previous lesson, we learned about absolute length units like pixels. While absolute units can be helpful in certain situations, there will be times when you want to use relative units.
1111

@@ -15,16 +15,15 @@ In this lesson, we will learn about two relative units: `em`s and `rem`s.
1515

1616
To better understand how this works, let's take a look at an example:
1717

18+
:::interactive_editor
19+
1820
```html
21+
<link rel="stylesheet" href="styles.css" />
1922
<p class="para">I am a paragraph element</p>
2023

2124
<div class="blue-box"></div>
2225
```
2326

24-
For the HTML, we have a paragraph and `div` element. The paragraph element has a class of `para`, and the `div` element has a class of `blue-box`.
25-
26-
Here is the accompanying CSS:
27-
2827
```css
2928
.para {
3029
font-size: 20px;
@@ -36,9 +35,15 @@ Here is the accompanying CSS:
3635
background-color: blue;
3736
color: white;
3837
padding: 10px;
38+
width: 100px;
39+
height: 100px;
3940
}
4041
```
4142

43+
:::
44+
45+
For the HTML, we have a paragraph and a `div` element. The paragraph element has a class of `para`, and the `div` element has a class of `blue-box`.
46+
4247
For the `para` class, we set the `font-size` to `20px` and the `margin-bottom` to `1.5em`. This means that the margin will be 1.5 times the font size of the paragraph element. `1.5em` results in 30 pixels of margin at the bottom of the paragraph. We have also set a `border` of `2px solid red` so you can see the margins better.
4348

4449
For the `blue-box` class, we set the background color to `blue`, the text color to `white`, and the `padding` to `10px` on all four sides.
@@ -63,6 +68,14 @@ By default, the font size of the `html` element is `16px`. If the user increases
6368

6469
Here is an example of using the `rem` unit for the font size instead of pixels:
6570

71+
:::interactive_editor
72+
73+
```html
74+
<link rel="stylesheet" href="styles.css" />
75+
<p>This is regular text.</p>
76+
<p class="para">This text is slightly larger.</p>
77+
```
78+
6679
```css
6780
.para {
6881
font-size: 1.2rem;
@@ -71,6 +84,8 @@ Here is an example of using the `rem` unit for the font size instead of pixels:
7184
}
7285
```
7386

87+
:::
88+
7489
By setting the font size to `1.2rem`, the font size of the paragraph element will be 1.2 times the font size of the root element. If the user hasn't changed the default font size, the font size of the paragraph element will be `19.2px` because it is 1.2 times `16px`.
7590

7691
So when should you use `rem` units? `rem` units are preferred over pixels for typography because they scale proportionally with the user's browser settings. This makes your content more accessible to users with visual impairments.

0 commit comments

Comments
 (0)