Practices for using em in CSS
Let's look at the full HTML code of the page:
<html>
<head>
<title></title>
</head>
<body>
<main>
<div>
<p>
text
</p>
</div>
</main>
</body>
<html>
By default, all page tags have some font size. In fact, the html tag has some font size by default:
html {
font-size: 16px; /* default value */
}
So we can give page tags sizes in em, and they will calculate their sizes relative to their immediate parents.
Let's, for example, specify the font size for the tag main:
main {
font-size: 1.5em; /* corresponds to 24px */
}
Now let’s specify the font size for the tag div:
div {
font-size: 2em; /* corresponds to 48px */
}
Let's say we have HTML code for which we will solve problems:
<main>
<h1>header</h1>
<div>
<p>
text
</p>
<p>
text
</p>
</div>
<div>
<p>
text
</p>
<p>
text
</p>
</div>
</main>
In all the problems below, rewrite all units specified in pixels into em:
main {
margin: 16px auto;
}
h1 {
font-size: 32px;
}
p {
font-size: 32px;
}
main {
margin: 32px auto;
}
h1 {
font-size: 32px;
}
div {
font-size: 16px;
margin-bottom: 32px;
}
p {
font-size: 20px;
}