Day 65: using the em unit in container queries

Relative units used in container queries work differently than relative units in media queries.

If you use em in a media query, the font-size of the <body>, <html>, or any other element on the page doesn’t matter. That’s because relative length units in media queries are based on the initial value, which means that units are never based on results of declarations. em in a media query is relative to the font-size, defined by the user agent or the user’s preferences.

/* 
The font size of both <body> and <html> is 40px,
the media query doesn't fire at 2560px (40 * 64),
but at 1024px (16 * 64) (assuming that the base
font size in the browser is 16px).
*/


html, body {
font-size: 40px;
}

@media (min-width: 64em) {
body {
background: aqua;
}
}

With container queries that’s different. Relative length units are evaluated based on the computed values of the query container.

The container query in the following example fires at 512px (32 * 16 = 512) because the font size of the container is 16px.

section {
font-size: 16px;
container: wrapper / inline-size;
}

.card {
background-color: yellow;
}

@container wrapper (min-width: 32em) {
.card {
background-color: hotpink;
}
}
<section>
<h2>Latest news</h2>

<div class="card">
<h2>Hey, I'm a card!</h2>
</div>
</section>

You can grab and resize the <section> by clicking and dragging it in the bottom right corner.

Latest news

Hey, I’m a card!

If you change the font size of the container to 26px, the media query fires at 832px (32 * 26 = 832)

.large {
font-size: 26px;
}
<section class="large">
<h2>Latest news</h2>

<div class="card">
<h2>Hey, I'm a card!</h2>
</div>
</section>

Latest news

Hey, I’m a card!

<p>My blog doesn’t support comments yet, but you can reply via <a href=’mailto:blog@matuzo.at?subject=Comment%20on%20%E2%80%9CDay%2065%3A%20using%20the%20em%20unit%20in%20container%20queries%E2%80%9D’>e-mail</a>.</p>


This content originally appeared on Manuel Matuzović - Web development blog and was authored by Manuel Matuzović

Relative units used in container queries work differently than relative units in media queries.

If you use em in a media query, the font-size of the <body>, <html>, or any other element on the page doesn't matter. That's because relative length units in media queries are based on the initial value, which means that units are never based on results of declarations. em in a media query is relative to the font-size, defined by the user agent or the user’s preferences.

/* 
The font size of both <body> and <html> is 40px,
the media query doesn't fire at 2560px (40 * 64),
but at 1024px (16 * 64) (assuming that the base
font size in the browser is 16px).
*/


html, body {
font-size: 40px;
}

@media (min-width: 64em) {
body {
background: aqua;
}
}

With container queries that's different. Relative length units are evaluated based on the computed values of the query container.

The container query in the following example fires at 512px (32 * 16 = 512) because the font size of the container is 16px.

section {
font-size: 16px;
container: wrapper / inline-size;
}

.card {
background-color: yellow;
}

@container wrapper (min-width: 32em) {
.card {
background-color: hotpink;
}
}
<section>
<h2>Latest news</h2>

<div class="card">
<h2>Hey, I'm a card!</h2>
</div>
</section>

You can grab and resize the <section> by clicking and dragging it in the bottom right corner.

Latest news

Hey, I'm a card!

If you change the font size of the container to 26px, the media query fires at 832px (32 * 26 = 832)

.large {
font-size: 26px;
}
<section class="large">
<h2>Latest news</h2>

<div class="card">
<h2>Hey, I'm a card!</h2>
</div>
</section>

Latest news

Hey, I'm a card!

<p>My blog doesn't support comments yet, but you can reply via <a href='mailto:blog@matuzo.at?subject=Comment%20on%20%E2%80%9CDay%2065%3A%20using%20the%20em%20unit%20in%20container%20queries%E2%80%9D'>e-mail</a>.</p>


This content originally appeared on Manuel Matuzović - Web development blog and was authored by Manuel Matuzović


Print Share Comment Cite Upload Translate Updates
APA

Manuel Matuzović | Sciencx (2022-12-23T09:38:54+00:00) Day 65: using the em unit in container queries. Retrieved from https://www.scien.cx/2022/12/23/day-65-using-the-em-unit-in-container-queries/

MLA
" » Day 65: using the em unit in container queries." Manuel Matuzović | Sciencx - Friday December 23, 2022, https://www.scien.cx/2022/12/23/day-65-using-the-em-unit-in-container-queries/
HARVARD
Manuel Matuzović | Sciencx Friday December 23, 2022 » Day 65: using the em unit in container queries., viewed ,<https://www.scien.cx/2022/12/23/day-65-using-the-em-unit-in-container-queries/>
VANCOUVER
Manuel Matuzović | Sciencx - » Day 65: using the em unit in container queries. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/12/23/day-65-using-the-em-unit-in-container-queries/
CHICAGO
" » Day 65: using the em unit in container queries." Manuel Matuzović | Sciencx - Accessed . https://www.scien.cx/2022/12/23/day-65-using-the-em-unit-in-container-queries/
IEEE
" » Day 65: using the em unit in container queries." Manuel Matuzović | Sciencx [Online]. Available: https://www.scien.cx/2022/12/23/day-65-using-the-em-unit-in-container-queries/. [Accessed: ]
rf:citation
» Day 65: using the em unit in container queries | Manuel Matuzović | Sciencx | https://www.scien.cx/2022/12/23/day-65-using-the-em-unit-in-container-queries/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.