Custom Properties in Media Queries

suggest change

Custom properties participate in the cascade, so a media query can change a shared design value:

:root {
  --page-gutter: 1rem;
  --column-count: 1;
}

@media (width >= 40rem) {
  :root {
    --page-gutter: 2rem;
    --column-count: 2;
  }
}

@media (width >= 70rem) {
  :root {
    --column-count: 4;
  }
}

.card-grid {
  display: grid;
  grid-template-columns: repeat(var(--column-count), minmax(0, 1fr));
  gap: var(--page-gutter);
  padding-inline: var(--page-gutter);
}

This can be useful when several components share the same responsive value. If only one rule uses the value, changing the property directly in the media query is usually simpler.

Container queries can override component-scoped custom properties when a component should respond to its own available space rather than the viewport.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents