CSS Media Queries

Create responsive, adaptive designs that work beautifully across all devices!

What Are CSS Media Queries?

CSS Media Queries allow you to apply different styles based on device characteristics like screen size, resolution, orientation, and more. They're the foundation of responsive web design, enabling your website to adapt to different viewing environments.

Media queries use the @media rule to conditionally apply CSS when a specific condition is true.

Example 1: Basic Media Query Syntax

/* Default styles */
.responsive-box {
  background-color: #3498db;
  color: white;
  padding: 1.25rem;
  width: 100%;
  text-align: center;
}

/* Apply when screen width is 37.5rem or less */
@media (max-width: 37.5rem) {
  .responsive-box {
    background-color: #e74c3c;
    font-size: 0.9rem;
  }
}
Resize your browser window to see this box change!

This box changes color and font size when the viewport width is 37.5rem or less.

Media Query Components

Basic syntax: @media [media-type] [and/not/only] ([media-feature]) { /* CSS rules */ }

Example 2: Media Types & Features

/* Styles for screens with dark color scheme */
@media screen and (prefers-color-scheme: dark) {
  .theme-aware {
    background-color: #222;
    color: #f0f0f0;
    border-color: #444;
  }
}

/* Styles for print */
@media print {
  .theme-aware {
    background-color: white !important;
    color: black !important;
    border: 0.0625rem solid black !important;
  }
}
This box adapts to your system's color scheme preference and will have special styling when printed.

If your system is set to dark mode, this box will have a dark background.

Common Media Features

Example 3: Complex Media Queries

/* Base styles */
.complex-demo {
  background-color: #3498db;
  color: white;
  padding: 1.25rem;
  border-radius: 0.5rem;
  text-align: center;
}

/* Medium screens in landscape mode */
@media (min-width: 37.5rem) and (max-width: 64rem) and (orientation: landscape) {
  .complex-demo {
    background-color: #9b59b6;
    border-radius: 1rem;
  }
}

/* High-resolution displays */
@media (min-resolution: 2dppx) {
  .complex-demo {
    border: 0.125rem solid gold;
  }
}
This box has different styles based on complex conditions.

This example demonstrates combining multiple conditions and targeting high-resolution displays.

Mobile-First vs. Desktop-First

Mobile-First Approach

Start with styles for small screens, then add complexity for larger screens using min-width queries.

/* Base styles for mobile */
.element { width: 100%; }

/* Tablet and up */
@media (min-width: 48rem) {
  .element { width: 50%; }
}

/* Desktop */
@media (min-width: 64rem) {
  .element { width: 33.33%; }
}

Desktop-First Approach

Start with styles for large screens, then simplify for smaller screens using max-width queries.

/* Base styles for desktop */
.element { width: 33.33%; }

/* Tablet and down */
@media (max-width: 63.9375rem) {
  .element { width: 50%; }
}

/* Mobile */
@media (max-width: 47.9375rem) {
  .element { width: 100%; }
}

Example 4: Responsive Layout

.responsive-layout {
  display: flex;
  flex-wrap: wrap;
  gap: 1.25rem;
}

.responsive-item {
  flex: 1 1 100%;
  background-color: #3498db;
  color: white;
  padding: 1.25rem;
  text-align: center;
  border-radius: 0.5rem;
}

@media (min-width: 37.5rem) {
  .responsive-item {
    flex: 1 1 calc(50% - 0.625rem);
  }
}

@media (min-width: 56.25rem) {
  .responsive-item {
    flex: 1 1 calc((100% - 2.5rem) / 3);
  }
}
Item 1
Item 2
Item 3

Resize the window to see how these items adapt from 1 column to 2 columns to 3 columns.

Best Practices & Tips

Example 5: User Preference Media Queries

/* Respect user's motion preferences */
.animated-element {
  transition: transform 0.3s ease;
}
.animated-element:hover {
  transform: scale(1.1);
}

/* Disable animation if user prefers reduced motion */
@media (prefers-reduced-motion: reduce) {
  .animated-element {
    transition: none;
  }
  .animated-element:hover {
    transform: none;
  }
}
Hover over me!

This element animates on hover, but respects the user's reduced motion preference if enabled.

Further Reading