Media Queries

What You'll Learn

  • Master media query syntax with @media rules for responsive breakpoints
  • Understand mobile-first vs desktop-first responsive design strategies
  • Use logical operators (and, or, not) to create complex queries
  • Apply modern Level 4 range syntax for more readable media queries
  • Implement common breakpoint patterns used in production frameworks
  • Optimize layouts for different devices and orientations
  • Handle advanced media features like resolution, hover, and pointer

Introduction to Media Queries

Media queries are the foundation of responsive web design, allowing you to apply different styles based on device characteristics like screen width, height, orientation, and resolution. Introduced with CSS3, media queries enable a single HTML document to adapt its presentation for phones, tablets, desktops, and even print.

While modern CSS offers fluid design techniques with clamp() and viewport units, media queries remain essential for major layout changes, component variations, and handling device-specific features. Understanding when to use media queries versus fluid design is key to creating robust responsive experiences.

Basic Media Query Syntax

A media query consists of a media type (optional) and one or more media features that test for specific conditions. When all conditions are true, the styles within the media query block are applied.

/* Basic syntax */ @media media-type and (media-feature) { /* Styles applied when condition is true */ } /* Most common: screen width */ @media screen and (min-width: 768px) { .container { max-width: 1200px; } } /* Multiple conditions with 'and' */ @media screen and (min-width: 768px) and (max-width: 1024px) { .sidebar { display: block; } }

Common Breakpoint Patterns

Industry-standard breakpoints are based on common device sizes. These breakpoints have evolved from actual device widths and are widely used across major CSS frameworks like Bootstrap, Tailwind, and Material Design.

/* Standard breakpoints (mobile-first) */ /* Extra Small: < 640px (default, no media query) */ /* Small: ≥640px (landscape phones) */ @media (min-width: 640px) { .container { max-width: 640px; } } /* Medium: ≥768px (tablets) */ @media (min-width: 768px) { .container { max-width: 768px; } } /* Large: ≥1024px (laptops/desktops) */ @media (min-width: 1024px) { .container { max-width: 1024px; } } /* Extra Large: ≥1280px (large desktops) */ @media (min-width: 1280px) { .container { max-width: 1280px; } } /* 2XL: ≥1536px (ultra-wide) */ @media (min-width: 1536px) { .container { max-width: 1536px; } }

Mobile-First Approach (Recommended)

Mobile-first design starts with styles for the smallest screens and progressively enhances for larger displays using min-width queries. This approach is recommended because it naturally creates a performance benefit for mobile users and ensures your design works on constrained devices.

/* Base styles (mobile) */ .nav { flex-direction: column; padding: 1rem; } /* Enhance for tablets */ @media (min-width: 768px) { .nav { flex-direction: row; padding: 1.5rem; } } /* Enhance for desktops */ @media (min-width: 1024px) { .nav { padding: 2rem; } }

Desktop-First Approach

Desktop-first design starts with styles for large screens and uses max-width queries to adapt for smaller devices. While less common today, this approach can be appropriate for applications primarily used on desktop or when retrofitting existing desktop-only designs.

/* Base styles (desktop) */ .sidebar { width: 300px; position: fixed; left: 0; } /* Adapt for tablets */ @media (max-width: 1024px) { .sidebar { width: 250px; } } /* Adapt for mobile */ @media (max-width: 768px) { .sidebar { width: 100%; position: static; } }

Modern Range Syntax (Level 4)

CSS Media Queries Level 4 introduces a more intuitive range syntax using comparison operators. This makes queries more readable and easier to understand at a glance.

/* Old syntax */ @media (min-width: 768px) and (max-width: 1024px) { .element { /* styles */ } } /* New Level 4 syntax - More readable! */ @media (768px <= width <= 1024px) { .element { /* styles */ } } /* Greater than or equal */ @media (width >= 768px) { .element { /* styles */ } } /* Less than or equal */ @media (width <= 768px) { .element { /* styles */ } } /* Greater than (exclusive) */ @media (width > 767px) { .element { /* styles */ } } /* Less than (exclusive) */ @media (width < 768px) { .element { /* styles */ } }

Logical Operators: and, or, not

Media queries support logical operators to create complex conditions. You can combine multiple features with and, provide alternatives with or (comma), and negate conditions with not.

/* AND: All conditions must be true */ @media screen and (min-width: 768px) and (max-width: 1024px) { .element { /* Applies only between 768px and 1024px */ } } /* Multiple AND conditions */ @media (min-width: 768px) and (orientation: landscape) and (hover: hover) { .element { /* Large screen, landscape, with hover capability */ } } /* OR: Any condition can be true (comma-separated) */ @media (max-width: 640px), (orientation: portrait) { .mobile-menu { /* Applies if narrow OR portrait */ } } /* NOT: Negates the entire query */ @media not all and (min-width: 768px) { .element { /* Applies when NOT at least 768px wide */ /* Same as (max-width: 767px) */ } } /* NOT with specific features */ @media not (hover: hover) { .button { /* No hover capability (touch devices) */ padding: 1rem; /* Bigger touch targets */ } }

Orientation and Aspect Ratio

Beyond width, you can query device orientation and aspect ratio to create designs that adapt to how users hold their devices or the shape of their screens.

/* Portrait: height > width */ @media (orientation: portrait) { .hero { height: 60vh; } } /* Landscape: width > height */ @media (orientation: landscape) { .hero { height: 80vh; } } /* Aspect ratio */ @media (aspect-ratio: 16/9) { .video-container { /* Optimized for widescreen */ } } /* Minimum aspect ratio (wider screens) */ @media (min-aspect-ratio: 16/9) { .content { max-width: 1400px; } } /* Maximum aspect ratio (taller screens) */ @media (max-aspect-ratio: 1/1) { .sidebar { display: none; /* Hide on very tall screens */ } }

Advanced Media Features

Modern media queries support many advanced features for fine-grained control over responsive design, especially for handling different input methods and display capabilities.

/* Resolution (for high-DPI displays) */ @media (min-resolution: 2dppx) { .logo { background-image: url('logo@2x.png'); } } /* Hover capability (mouse vs touch) */ @media (hover: hover) { .button:hover { background: darkblue; } } @media (hover: none) { /* Touch devices - no hover state */ .button:active { background: darkblue; } } /* Pointer precision */ @media (pointer: fine) { /* Mouse or trackpad - precise pointer */ .clickable { padding: 0.5rem; } } @media (pointer: coarse) { /* Touch - imprecise pointer */ .clickable { padding: 1rem; /* Bigger touch targets */ min-height: 44px; /* iOS accessibility requirement */ } } /* Display mode (for PWAs) */ @media (display-mode: standalone) { /* App is installed as PWA */ .install-prompt { display: none; } }

Media Types

Media types specify the category of device. While screen is most common, other types exist for specific output media.

/* Screen (default) - computers, tablets, phones */ @media screen and (min-width: 768px) { /* Standard responsive styles */ } /* Print - for printing pages */ @media print { body { font-size: 12pt; color: black; } .no-print { display: none; } } /* All media types */ @media all and (min-width: 768px) { /* Applies to all media types */ } /* Speech - for screen readers (rarely used) */ @media speech { body { voice-rate: slow; } }

Practical Responsive Patterns

Here are complete, production-ready patterns that combine media queries effectively for common responsive design scenarios.

Responsive Navigation

/* Mobile: hamburger menu */ .nav { display: none; } .hamburger { display: block; } /* Desktop: horizontal menu */ @media (min-width: 768px) { .nav { display: flex; gap: 2rem; } .hamburger { display: none; } }

Responsive Grid Layout

.grid { display: grid; grid-template-columns: 1fr; gap: 1rem; } @media (min-width: 640px) { .grid { grid-template-columns: repeat(2, 1fr); gap: 1.5rem; } } @media (min-width: 1024px) { .grid { grid-template-columns: repeat(4, 1fr); gap: 2rem; } }

Responsive Typography

/* Combine fluid and breakpoint approaches */ h1 { font-size: clamp(2rem, 5vw, 3rem); } @media (min-width: 768px) { h1 { font-size: clamp(2.5rem, 5vw, 4rem); } } @media (min-width: 1024px) { h1 { font-size: clamp(3rem, 5vw, 5rem); } }

Container Pattern

.container { width: 100%; padding: 0 1rem; margin: 0 auto; } @media (min-width: 640px) { .container { max-width: 640px; } } @media (min-width: 768px) { .container { max-width: 768px; } } @media (min-width: 1024px) { .container { max-width: 1024px; } } @media (min-width: 1280px) { .container { max-width: 1280px; } }

Key Takeaways