Media Queries

suggest change

Media queries conditionally apply styles according to the output medium, viewport, or user preferences:

@media (width >= 48rem) {
  .layout {
    grid-template-columns: 2fr 1fr;
  }
}

@media print {
  nav,
  .interactive-controls {
    display: none;
  }
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
  }
}

Frequently useful media features include:

Feature Example use
width, height Adapt to viewport dimensions.
orientation Respond to the viewport's aspect orientation.
resolution Select density-dependent assets.
hover, pointer Adapt optional interactions to input capabilities.
prefers-reduced-motion Reduce nonessential movement.
prefers-color-scheme Offer light and dark themes.
forced-colors Adapt to a restricted user color palette.

Range syntax can make dimensional queries easier to read:

@media (30rem <= width < 60rem) {
  /* Styles for this viewport-width interval. */
}

Choose breakpoints according to where content needs to adapt, not named device models. A component that responds to its allocated space should generally use a container query instead.

For mobile pages, include the standard viewport declaration in HTML:

<meta name="viewport" content="width=device-width, initial-scale=1">

Feedback about page:

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



Table Of Contents