CSS Layout

CSS Media Queries

CSS Media Queries are conditional CSS rules that apply styles based on device characteristics like screen size, resolution, orientation, or print capabilities, enabling responsive design adaptation.

How do CSS media queries work?

A media query applies a block of CSS only when a condition is true, usually the viewport width. @media (min-width: 768px) { … } applies its rules when the viewport is 768px or wider. You can also query orientation, user preferences such as prefers-color-scheme and prefers-reduced-motion, and input type with hover and pointer.

/* Mobile-first */
.nav { flex-direction: column; }
@media (min-width: 768px) {
  .nav { flex-direction: row; }
}

/* Modern range syntax */
@media (400px <= width <= 900px) { … }

@media (prefers-color-scheme: dark) { body { background: #111; } }
@media (prefers-reduced-motion: reduce) { * { animation: none; } }

Interactive Media Queries Demo

Code Editor

<div class="responsive-layout">
  <div class="content">
    <h2>Responsive Content</h2>
    <p>This content adapts to different screen sizes using media queries.</p>
  </div>
</div>

Preview

Media Query Fundamentals

CSS Media Queries properties define apply CSS rules only when certain conditions are met. They are the foundation of responsive web design, enabling different styles for different devices.

Basic Syntax

@media media-type and (media-feature) {
  /* CSS rules */
}

Media Type

screen, print, all, speech

Logical Operators

and, not, only, comma (or)

Media Features

width, height, orientation, etc.

Common Breakpoints

Standard Breakpoints

/* Mobile First */
@media (min-width: 480px) { /* Small tablets */ }
@media (min-width: 768px) { /* Tablets */ }
@media (min-width: 1024px) { /* Desktop */ }
@media (min-width: 1200px) { /* Large desktop */ }

Range Queries

/* Target specific ranges */
@media (min-width: 768px) and (max-width: 1023px) {
  /* Tablets only */
}

@media (max-width: 767px) {
  /* Mobile only */
}

Modern Range Syntax

/* New range syntax (Level 4) */
@media (width >= 768px) { /* Greater than or equal */ }
@media (768px <= width < 1024px) { /* Between values */ }
@media (width < 768px) { /* Less than */ }

The new range syntax is more intuitive and easier to read than traditional min/max syntax.

Media Features

Dimension Features

@media (width: 1024px) { /* Exact width */ }
@media (min-width: 768px) { /* Minimum width */ }
@media (max-width: 1023px) { /* Maximum width */ }
@media (height > 600px) { /* Height condition */ }
@media (aspect-ratio: 16/9) { /* Aspect ratio */ }

Control layout based on viewport dimensions

Interaction Features

@media (hover: hover) { /* Can hover */ }
@media (hover: none) { /* Touch device */ }
@media (pointer: fine) { /* Mouse/trackpad */ }
@media (pointer: coarse) { /* Touch */ }
@media (any-hover: hover) { /* Any hover */ }

Adapt to different input methods

Orientation

@media (orientation: portrait) {
  /* Tall screens */
}

@media (orientation: landscape) {
  /* Wide screens */
}

Detect device orientation

Resolution

@media (min-resolution: 2dppx) {
  /* High DPI screens */
}

@media (-webkit-min-device-pixel-ratio: 2) {
  /* Retina displays */
}

Target high-resolution displays

Color Scheme

@media (prefers-color-scheme: dark) {
  /* Dark mode */
}

@media (prefers-color-scheme: light) {
  /* Light mode */
}

Respect user preferences

Advanced Media Queries

Complex Conditions

/* Multiple conditions with AND */
@media screen and (min-width: 768px) and (orientation: landscape) {
  /* Tablet landscape only */
}

/* Multiple conditions with OR (comma) */
@media (max-width: 767px), (orientation: portrait) {
  /* Mobile OR portrait mode */
}

/* NOT operator */
@media not screen and (max-width: 767px) {
  /* Not mobile screens */
}

Combine multiple conditions for precise targeting.

Accessibility Features

/* Reduced motion preference */
@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* High contrast preference */
@media (prefers-contrast: high) {
  /* Increase contrast */
}

/* Transparency preference */
@media (prefers-reduced-transparency: reduce) {
  /* Reduce transparency effects */
}

Respect user accessibility preferences for better UX.

Print Styles

@media print {
  /* Hide non-essential elements */
  .no-print, nav, aside {
    display: none !important;
  }

  /* Optimize for print */
  body {
    font-size: 12pt;
    line-height: 1.4;
    color: black;
    background: white;
  }

  /* Show link URLs */
  a::after {
    content: " (" attr(href) ")";
  }
}

Optimize layouts for printing with print-specific styles.

Best Practices

Best Practices

  • • Use mobile-first approach with min-width
  • • Organize media queries by breakpoint
  • • Use logical operators effectively
  • • Test on real devices, not just browser tools
  • • Consider accessibility preferences
  • • Don't forget print styles

Performance Tips

  • • Group related queries together
  • • Avoid duplicate selectors across queries
  • • Use CSS custom properties for consistency
  • • Consider container queries for components
  • • Minimize redundant code between breakpoints
  • • Test performance impact of complex queries

CSS Media Queries — step by step

1 / 4
1

Basic Syntax

Write @media (max-width: 768px) { ... } to apply styles only at certain viewport widths. Use min-width for mobile-first, max-width for desktop-first.

1 of 4
💡Mobile-First Saves Code
Writing mobile styles as the default and adding complexity at wider breakpoints with min-width queries means less CSS overall — you never override large styles back to small ones.

🎯 Quick Check

Which media feature respects user OS settings to disable animations?

CSS Media Queries: summary

Key points

  • min-width queries suit mobile-first CSS; max-width queries suit desktop-first CSS. Pick one approach and stay consistent.
  • Media queries add no specificity, so a later rule with the same selector wins. Put them after the base styles.
  • Use em or rem for breakpoints if you want them to respect the user's font-size setting.
  • hover: hover detects devices with a real hover, so touch screens don't get stuck hover states.

Common mistakes

  • Writing media queries before the base rule, so the base rule overrides them.
  • Forgetting the viewport meta tag, so phones report a 980px viewport and the mobile queries never match.

Frequently asked questions

›What is the difference between min-width and max-width in media queries?

min-width: 768px matches screens 768px and wider (mobile-first). max-width: 767px matches screens up to 767px (desktop-first).

›Why is my media query not working?

Check that the viewport meta tag is present, that the query comes after the rule it should override, and that the selector inside has the same or higher specificity.

›Can I use media queries in inline styles?

No. Media queries only work in stylesheets or <style> blocks. For per-component behaviour, use container queries or classes.