Performance: Responsive grid with breakpoints
This affects page load speed and rendering performance by controlling how many grid items and styles are applied at different screen sizes.
Jump into concepts and practice - no test required
$breakpoints: (small: 600px, medium: 900px); .grid { display: grid; grid-template-columns: repeat(4, 1fr); @media (max-width: map-get($breakpoints, medium)) { grid-template-columns: repeat(2, 1fr); } @media (max-width: map-get($breakpoints, small)) { grid-template-columns: 1fr; } }
@media (max-width: 600px) { .grid-item { width: 100%; } } @media (max-width: 900px) { .grid-item { width: 50%; } } @media (min-width: 901px) { .grid-item { width: 25%; } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Multiple overlapping media queries with repeated widths | Low (grid container + items) | Multiple reflows on resize | Medium paint cost due to layout changes | [X] Bad |
| Single grid container with breakpoint variables and minimal media queries | Low (grid container + items) | Single reflow per breakpoint change | Lower paint cost due to efficient layout | [OK] Good |
@mixin breakpoint($size) {
@media (min-width: $size) {
@content;
}
}
.grid {
display: grid;
grid-template-columns: repeat(1, 1fr);
@include breakpoint(600px) {
grid-template-columns: repeat(2, 1fr);
}
@include breakpoint(900px) {
grid-template-columns: repeat(3, 1fr);
}
}@mixin breakpoint($size) {
@media min-width: $size {
@content;
}
}
.grid {
display: grid;
grid-template-columns: repeat(1, 1fr);
@include breakpoint(768px) {
grid-template-columns: repeat(2, 1fr);
}
}