Skip to main content

CSS Responsive Design

Preference Media Queries

Respect motion, contrast, color-scheme, transparency, and input preferences exposed by the user agent. The examples respond to reduced motion, increased contrast, color schemes, transparency, hover capability, and coarse or fine pointers.

Responsive design is not only about screen size. People tell their devices "reduce motion", "use dark mode" or "increase contrast", and CSS can read those settings and respect them.

These queries let the page keep doing the same job while changing how it looks or moves for the person using it. They work just like the size queries in CSS Media Queries, and they are a core part of Accessible CSS.

Reduced motion and more contrast

Example

css

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
}

@media (prefers-contrast: more) {
  .button { border-width: 2px; }
}

For people who get dizzy or sick from movement, the first block shortens animations to almost nothing and turns off smooth scrolling. The second makes button borders thicker when someone asks for more contrast.

"Reduce motion" does not have to mean "no motion at all". A quick fade to show that something changed is usually fine. Big slides, zooms, parallax and anything that loops are what to remove.

Mouse, touch, or both

Example

css

@media (hover: hover) and (pointer: fine) {
  .menu__item:hover { background: #eef2ff; }
}

@media (any-pointer: coarse) {
  .icon-button { min-width: 2.75rem; min-height: 2.75rem; }
}

hover: hover and pointer: fine mean the main input is something like a mouse, so hover effects make sense. any-pointer: coarse means at least one input is a finger, so tap targets get bigger.

Do not use these to guess the device. A laptop with a touchscreen has both a mouse and a finger, which is why the any- versions exist. And never hide something important behind hover: touch users cannot hover.

Preference Media Features

Media featureDetectsExample
prefers-reduced-motionA request to reduce non-essential motion.(prefers-reduced-motion: reduce)
prefers-color-schemeA light or dark color preference.(prefers-color-scheme: dark)
prefers-contrastA request for more or less contrast.(prefers-contrast: more)
forced-colorsAn active forced-color palette.(forced-colors: active)

Complete Example

Complete runnable example

html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Preference Media Queries example</title>
  <style>
    * { box-sizing: border-box; }
    body {
      margin: 0;
      padding: 2rem;
      font-family: system-ui, sans-serif;
      line-height: 1.5;
    }
    main { max-width: 70rem; margin-inline: auto; }
    article, section, aside, form, .card, .panel {
      padding: 1rem;
      border: 1px solid #cbd5e1;
      border-radius: 0.6rem;
    }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
}

@media (prefers-contrast: more) {
  .button { border-width: 2px; }
}

@media (hover: hover) and (pointer: fine) {
  .menu__item:hover { background: #eef2ff; }
}

@media (any-pointer: coarse) {
  .icon-button { min-width: 2.75rem; min-height: 2.75rem; }
}
  </style>
</head>
<body>
  <main><nav class="menu"><button class="menu__item">Hover-capable menu item</button></nav><button class="icon-button" aria-label="Save">★</button><p>Emulate reduced motion or increased contrast in developer tools.</p></main>
</body>
</html>

Browser Support

Feature
Chrome
Edge
Firefox
Safari
prefers-reduced-motion74796310.1
prefers-contrast969610114.1

Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.

How to test

You do not need to change your system settings each time. In Chrome DevTools, open the Rendering panel and switch "Emulate CSS media feature" to reduced motion, dark mode or more contrast. Then check that everything important is still visible and usable.

Preference Media Queries exercises

Motion only when welcome

Give the box a transition of transform 0.3s only for people who have no preference for less motion.

The HTML your code works on
<div class="box">Box</div>
<p class="text">Text</p>

Result

Check your answer to see it here.