CSS chapters

Chapter 7 of 17

Media queries and user preferences

Try this in our CSS Compiler →

The idea

A media query applies rules when a browser condition matches. It is not an if statement acting on arbitrary variables. Use viewport queries for layout and preference queries for accessibility. A narrow layout should remain readable rather than hiding essential information.

Worked example

body { padding: 24px; }
@media (max-width: 600px) { body { padding: 12px; } }
@media (prefers-reduced-motion: reduce) { * { animation: none; transition: none; } }

What to check

The smaller padding applies at or below 600 CSS pixels. Reduced motion is a user preference, not a device-speed test.

Practice task

Give a list a wide-screen two-column layout and a narrow-screen single column without removing any list items.

Check your understanding

Explain the result in your own words before changing the example. Change one thing at a time, compare the result with your prediction, and keep the version that demonstrates the concept clearly.

Try it yourself

Output
Ctrl/Cmd+Enter runs · Ctrl/Cmd+S downloads the project · Local auto-save is always on