Performance: Container query preparation
Container queries affect how the browser recalculates styles and layouts when container sizes change, impacting rendering speed and visual stability.
@container (min-width: 30rem) { .card { font-size: 1.5rem; padding: 2rem; } }
@container (min-width: 30rem) { .card { font-size: 1.5rem; } } @container (min-width: 30rem) { .card { padding: 2rem; } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Multiple separate container queries | No extra DOM nodes | Multiple reflows on container resize | Higher paint cost due to frequent style changes | [X] Bad |
| Combined container queries in one block | No extra DOM nodes | Single reflow on container resize | Lower paint cost with fewer style changes | [OK] Good |
@container in Sass?@container in Sass@container rule in Sass is used to write container queries clearly and apply styles conditionally based on container size.container-type to enable size-based queries.container-size, container-query, and container-style are not valid CSS properties for this purpose..box have if its container width is 500px?
@container (min-width: 600px) {
.box {
background-color: blue;
}
}
.box {
background-color: red;
}.box outside the container query has background-color: red;, so this style applies.@container (max-width 400px) {
.card {
padding: 1rem;
}
}max-width: 400px..card is valid, padding in rem is allowed, and max-width is a valid container query condition..card component differently when its container is narrower than 300px or wider than 700px using Sass container queries. Which of the following is the correct way to write this?Pass the test and this pattern moves unknown