Performance: Media queries
Media queries affect how CSS is applied based on device features, impacting rendering and layout recalculations during page load and resizing.
Jump into concepts and practice - no test required
@media screen and (max-width: 600px) { .box { width: 100%; padding: 20px; } }
@media screen and (max-width: 600px) { .box { width: 100vw; } } @media screen and (max-width: 600px) { .box { padding: 20px; } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Multiple separate media queries for same condition | Low | Multiple style recalculations | Moderate | [X] Bad |
| Combined media queries for same condition | Low | Single style recalculation | Low | [OK] Good |
@media queries?@media does@media queries let CSS change styles depending on device features like screen width.@media usage.@media (max-width: 600px) with parentheses and colon.body have on a screen 500px wide?body { background-color: white; } @media (max-width: 600px) { body { background-color: lightblue; } }@media max-width: 800px { p { font-size: 1.2rem; } }@media (max-width: 800px).p is valid, and semicolon is present. Using max-width is correct if targeting screens 800px or less.