CSSMedia Queries (@media)

Media Queries (@media)

Media queries allow you to apply different CSS based on device characteristics like viewport width, height, orientation, and more. They're essential for responsive design, letting you adapt layouts for phones, tablets, and desktops. Understanding media queries is fundamental to modern CSS development.

Basic media query syntax

CSS
<!-- @media rule: apply styles conditionally -->
@media (max-width: 768px) {
  .container {
    width: 100%;
    flex-direction: column;
  }
}

<!-- Styles apply when viewport <= 768px -->

/* min-width: styles apply when viewport >= 768px */
@media (min-width: 768px) {
  .container {
    width: 600px;
    flex-direction: row;
  }
}

<!-- Styles apply when viewport >= 768px -->

/* Range: both min and max -->
@media (min-width: 768px) and (max-width: 1200px) {
  .container {
    width: 700px;
  }
}

<!-- Styles apply in range 768px - 1200px -->

/* Device type: screen, print, etc -->
@media screen {
  /* Applies to screens (not print) */
}

@media print {
  /* Applies when printing */
}

@media (orientation: landscape) {
  /* Landscape orientation */
}

@media (orientation: portrait) {
  /* Portrait orientation -->
}
Common responsive breakpoints

Breakpoint

Width

Device

Mobile

< 640px

Small phones

Tablet

640px - 1024px

Tablets, larger phones

Desktop

1024px+

Desktop computers

HD

1920px+

Large monitors

CSS
<!-- Mobile-first: start with mobile, add features for larger screens -->

/* Mobile: base styles */
.container {
  width: 100%;
  padding: 10px;
}

.grid {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.item {
  width: 100%;
}

<!-- All stacked vertically on mobile -->

/* Tablet: medium screens -->
@media (min-width: 640px) {
  .container {
    max-width: 640px;
    margin: 0 auto;
    padding: 20px;
  }

  .grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 15px;
  }

  .item {
    width: auto;
  }
}

<!-- Two columns on tablet -->

/* Desktop: large screens -->
@media (min-width: 1024px) {
  .container {
    max-width: 1200px;
    padding: 30px;
  }

  .grid {
    grid-template-columns: 1fr 1fr 1fr;
    gap: 20px;
  }
}

<!-- Three columns on desktop -->

/* Common breakpoints -->

@media (max-width: 640px) { /* small */ }
@media (min-width: 640px) and (max-width: 1024px) { /* medium */ }
@media (min-width: 1024px) { /* large */ }
@media (min-width: 1920px) { /* extra large --> }
Practical responsive patterns

CSS
<!-- Responsive navigation -->
.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 20px;
  background: #f5f5f5;
}

.menu {
  display: none;  /* hide menu on mobile -->
}

@media (min-width: 768px) {
  .menu {
    display: flex;
    gap: 20px;
  }
}

<!-- Mobile: menu hidden (hamburger instead) -->
<!-- Desktop: menu visible -->

/* Responsive grid */
.cards {
  display: grid;
  grid-template-columns: 1fr;
  gap: 20px;
}

@media (min-width: 600px) {
  .cards {
    grid-template-columns: 1fr 1fr;
  }
}

@media (min-width: 1000px) {
  .cards {
    grid-template-columns: 1fr 1fr 1fr;
  }
}

<!-- 1 column mobile, 2 tablet, 3 desktop -->

/* Responsive font sizes -->
.title {
  font-size: 24px;
}

@media (min-width: 768px) {
  .title {
    font-size: 32px;
  }
}

@media (min-width: 1024px) {
  .title {
    font-size: 40px;
  }
}

<!-- Font scales with viewport -->

/* Show/hide elements responsively -->
.mobile-only {
  display: block;
}

.desktop-only {
  display: none;
}

@media (min-width: 768px) {
  .mobile-only {
    display: none;
  }

  .desktop-only {
    display: block;
  }
}

<!-- Different elements for mobile/desktop -->
Mobile-first vs Desktop-first

CSS
<!-- Mobile-first: start small, enhance up (preferred) -->
.container {
  /* Mobile styles: base */
  width: 100%;
  padding: 10px;
}

@media (min-width: 768px) {
  /* Tablet enhancement */
  .container {
    width: 750px;
  }
}

@media (min-width: 1024px) {
  /* Desktop enhancement */
  .container {
    width: 1200px;
  }
}

<!-- Benefits: -->
<!-- - Better performance (fewer styles override) -->
<!-- - Works on devices without media query support -->
<!-- - Progressive enhancement -->

/* Desktop-first: start large, reduce down */
.container {
  /* Desktop styles: base */
  width: 1200px;
  padding: 30px;
}

@media (max-width: 1024px) {
  /* Tablet reduction */
  .container {
    width: 800px;
  }
}

@media (max-width: 768px) {
  /* Mobile reduction */
  .container {
    width: 100%;
    padding: 10px;
  }
}

<!-- Less preferred but sometimes used -->

/* Mobile-first is recommended -->
/* Simpler, better performance, more maintainable -->
Other media query features

CSS
<!-- Dark mode: prefers-color-scheme -->
@media (prefers-color-scheme: dark) {
  body {
    background: #222;
    color: #fff;
  }
}

<!-- User prefers dark mode -->

/* Reduced motion: accessibility -->
@media (prefers-reduced-motion: reduce) {
  * {
    animation: none;
    transition: none;
  }
}

<!-- User prefers reduced motion -->

/* High contrast mode -->
@media (prefers-contrast: more) {
  .text {
    font-weight: bold;
    color: #000;
  }
}

<!-- User prefers high contrast -->

/* Aspect ratio: monitor type -->
@media (aspect-ratio: 16 / 9) {
  /* Landscape monitors */
}

/* Touch devices -->
@media (hover: none) {
  /* Touch device, no hover -->
  button:hover { /* ignored */ }
}

@media (hover: hover) {
  /* Desktop with mouse -->
  button:hover { color: blue; }
}

/* Light level: ambient light sensor -->
@media (light-level: bright) {
  /* Bright environment */
  body { background: white; }
}
Note
Media queries (`@media`) let you apply CSS based on device characteristics. Use `min-width` for mobile-first approach, `max-width` for desktop-first. Common breakpoints: 640px (tablet), 1024px (desktop). Test on actual devices or use browser DevTools to verify responsive behavior.
Next
Fluid typography: [Fluid Typography](/css/fluid-typography).