You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: curriculum/challenges/english/blocks/lecture-working-with-relative-and-absolute-units/672bb7f08b58df93ed2a8768.md
+20-5Lines changed: 20 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ challengeType: 19
5
5
dashedName: what-are-ems-and-rems-in-css
6
6
---
7
7
8
-
# --description--
8
+
# --interactive--
9
9
10
10
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.
11
11
@@ -15,16 +15,15 @@ In this lesson, we will learn about two relative units: `em`s and `rem`s.
15
15
16
16
To better understand how this works, let's take a look at an example:
17
17
18
+
:::interactive_editor
19
+
18
20
```html
21
+
<linkrel="stylesheet"href="styles.css" />
19
22
<pclass="para">I am a paragraph element</p>
20
23
21
24
<divclass="blue-box"></div>
22
25
```
23
26
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
-
28
27
```css
29
28
.para {
30
29
font-size: 20px;
@@ -36,9 +35,15 @@ Here is the accompanying CSS:
36
35
background-color: blue;
37
36
color: white;
38
37
padding: 10px;
38
+
width: 100px;
39
+
height: 100px;
39
40
}
40
41
```
41
42
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
+
42
47
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.
43
48
44
49
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
63
68
64
69
Here is an example of using the `rem` unit for the font size instead of pixels:
65
70
71
+
:::interactive_editor
72
+
73
+
```html
74
+
<linkrel="stylesheet"href="styles.css" />
75
+
<p>This is regular text.</p>
76
+
<pclass="para">This text is slightly larger.</p>
77
+
```
78
+
66
79
```css
67
80
.para {
68
81
font-size: 1.2rem;
@@ -71,6 +84,8 @@ Here is an example of using the `rem` unit for the font size instead of pixels:
71
84
}
72
85
```
73
86
87
+
:::
88
+
74
89
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`.
75
90
76
91
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