Skip to main content

CSS Media Queries Cheatsheet

Complete reference for CSS media queries, breakpoints, and feature detection.
Click any code block to copy to clipboard.

Your Current Environment

Viewport Width

0px

Viewport Height

0px

Color Scheme

light

Reduced Motion

No

Device Pixel Ratio

1x

Orientation

Portrait

Media Types

all
@media all { }

Matches all devices. This is the default if no type is specified.

screen
@media screen { }

Matches devices with screens (computers, tablets, phones).

print
@media print { }

Matches printers and print preview mode.

Common Breakpoints (Mobile First)

The most popular breakpoint system, used by Tailwind CSS and many design systems.

/* Mobile first approach */
/* xs: 0px - Base styles, no media query needed */

/* sm: 640px and up */
@media (min-width: 640px) { }

/* md: 768px and up */
@media (min-width: 768px) { }

/* lg: 1024px and up */
@media (min-width: 1024px) { }

/* xl: 1280px and up */
@media (min-width: 1280px) { }

/* 2xl: 1536px and up */
@media (min-width: 1536px) { }

Desktop First (Max-Width)

/* Desktop first approach */
@media (max-width: 1535px) { } /* Below 2xl */
@media (max-width: 1279px) { } /* Below xl */
@media (max-width: 1023px) { } /* Below lg */
@media (max-width: 767px) { }  /* Below md */
@media (max-width: 639px) { }  /* Below sm */

Viewport / Dimensions

width
@media (width: 1024px) { }

Matches exact viewport width.

min-width
@media (min-width: 768px) { }

Matches viewport width >= value. Used for mobile-first design.

max-width
@media (max-width: 1023px) { }

Matches viewport width <= value. Used for desktop-first design.

height
@media (height: 800px) { }

Matches exact viewport height.

min-height
@media (min-height: 600px) { }

Matches viewport height >= value.

max-height
@media (max-height: 900px) { }

Matches viewport height <= value.

aspect-ratio
@media (aspect-ratio: 16/9) { }

Matches exact viewport aspect ratio.

min-aspect-ratio
@media (min-aspect-ratio: 1/1) { }

Matches viewport aspect ratio >= value (wider).

max-aspect-ratio
@media (max-aspect-ratio: 4/3) { }

Matches viewport aspect ratio <= value (taller).

orientation
@media (orientation: portrait) { }
@media (orientation: landscape) { }

Portrait: height > width. Landscape: width >= height.

Display Quality / Resolution

resolution
@media (resolution: 96dpi) { }
@media (resolution: 2dppx) { }

Matches exact pixel density. Units: dpi, dpcm, dppx (dots per px).

min-resolution
@media (min-resolution: 2dppx) { }
@media (min-resolution: 192dpi) { }

Matches devices with high pixel density (Retina displays).

max-resolution
@media (max-resolution: 1dppx) { }

Matches standard resolution displays.

-webkit-device-pixel-ratio
@media (-webkit-min-device-pixel-ratio: 2) { }
@media (-webkit-max-device-pixel-ratio: 1) { }

Legacy WebKit syntax for device pixel ratio. Use resolution for modern browsers.

Retina / High-DPI Detection

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

/* With legacy fallback */
@media (-webkit-min-device-pixel-ratio: 2),
       (min-resolution: 2dppx) {
  /* High-DPI styles */
}

Color Features

color
@media (color) { }
@media (min-color: 8) { }

Tests color capability. Value is bits per color component.

color-index
@media (min-color-index: 256) { }

Number of entries in color lookup table. 0 for true color.

monochrome
@media (monochrome) { }
@media (monochrome: 0) { }

Tests grayscale capability. Value is bits per pixel. 0 means color device.

color-gamut
@media (color-gamut: srgb) { }
@media (color-gamut: p3) { }
@media (color-gamut: rec2020) { }

Tests supported color gamut. p3 and rec2020 are wider gamuts (HDR displays).

Interaction Features

hover
@media (hover: none) { }
@media (hover: hover) { }

Tests if primary input can hover. none = touch, hover = mouse/trackpad.

pointer
@media (pointer: none) { }
@media (pointer: coarse) { }
@media (pointer: fine) { }

Tests primary pointing device precision. coarse = touch, fine = mouse.

any-hover
@media (any-hover: none) { }
@media (any-hover: hover) { }

Tests if ANY input device can hover (e.g., tablet with mouse).

any-pointer
@media (any-pointer: coarse) { }
@media (any-pointer: fine) { }

Tests if ANY pointing device exists with given precision.

Touch vs Mouse Detection

/* Touch-friendly styles */
@media (hover: none) and (pointer: coarse) {
  .button {
    min-height: 44px; /* Larger touch targets */
    padding: 12px 24px;
  }
}

/* Mouse/trackpad styles */
@media (hover: hover) and (pointer: fine) {
  .button:hover {
    background-color: var(--hover-color);
  }
}

User Preference Queries

prefers-color-scheme
@media (prefers-color-scheme: light) { }
@media (prefers-color-scheme: dark) { }

Detects user's preferred color scheme from OS settings.

prefers-reduced-motion
@media (prefers-reduced-motion: no-preference) { }
@media (prefers-reduced-motion: reduce) { }

Respects user's motion sensitivity settings. Important for accessibility.

prefers-contrast
@media (prefers-contrast: no-preference) { }
@media (prefers-contrast: more) { }
@media (prefers-contrast: less) { }
@media (prefers-contrast: custom) { }

Detects if user prefers higher or lower contrast.

prefers-reduced-transparency
@media (prefers-reduced-transparency: no-preference) { }
@media (prefers-reduced-transparency: reduce) { }

Detects if user prefers less transparency/blur effects.

forced-colors
@media (forced-colors: none) { }
@media (forced-colors: active) { }

Detects Windows High Contrast Mode or similar forced color modes.

Dark Mode Implementation

:root {
  --bg-color: #ffffff;
  --text-color: #1a1a1a;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg-color: #1a1a1a;
    --text-color: #ffffff;
  }
}

Accessible Animations

/* Default: with animations */
.element {
  transition: transform 0.3s ease;
}

/* Reduced motion: instant or no animation */
@media (prefers-reduced-motion: reduce) {
  .element {
    transition: none;
  }

  .animated-element {
    animation: none;
  }
}

Display Mode (PWA)

display-mode: browser
@media (display-mode: browser) { }

Standard browser tab with URL bar and browser chrome.

display-mode: standalone
@media (display-mode: standalone) { }

PWA running as standalone app without browser UI.

display-mode: minimal-ui
@media (display-mode: minimal-ui) { }

Standalone with minimal navigation controls.

display-mode: fullscreen
@media (display-mode: fullscreen) { }

App is running in fullscreen mode.

Scripting Detection

scripting: none
@media (scripting: none) { }

JavaScript is completely disabled.

scripting: initial-only
@media (scripting: initial-only) { }

JS runs only on initial page load (e.g., printing).

scripting: enabled
@media (scripting: enabled) { }

JavaScript is fully enabled.

Logical Operators

and - Combine conditions

@media screen and (min-width: 768px) and (max-width: 1023px) {
  /* Tablet only */
}

or (comma) - Either condition

@media (max-width: 639px), (orientation: portrait) {
  /* Mobile OR portrait orientation */
}

not - Negate entire query

@media not screen {
  /* Not a screen device (print, etc.) */
}

@media not (hover: hover) {
  /* Devices that cannot hover */
}

only - Hide from old browsers

@media only screen and (min-width: 768px) {
  /* Only applies to modern browsers */
}

Range Syntax (CSS Media Queries Level 4)

Modern syntax using comparison operators. Supported in all modern browsers.

Comparison Operators

/* Greater than or equal */
@media (width >= 768px) { }

/* Less than */
@media (width < 1024px) { }

/* Range (between) */
@media (768px <= width < 1024px) { }
@media (768px <= width <= 1023px) { }

/* Equivalent to traditional syntax */
@media (min-width: 768px) and (max-width: 1023px) { }

Container Queries

Query the size of a parent container instead of the viewport. Supported in all modern browsers.

Basic Container Query

/* Define a container */
.card-container {
  container-type: inline-size;
  container-name: card;
}

/* Query the container */
@container card (min-width: 400px) {
  .card {
    display: grid;
    grid-template-columns: 1fr 2fr;
  }
}

/* Anonymous container */
@container (min-width: 300px) {
  .item { /* Queries nearest ancestor container */ }
}

Container Types

/* Size queries on inline axis only (width in horizontal writing) */
container-type: inline-size;

/* Size queries on both axes */
container-type: size;

/* No size queries, but can be queried with style() */
container-type: normal;

Container Query Units

cqw

1% of container width

cqh

1% of container height

cqi

1% of inline size

cqb

1% of block size

cqmin

Smaller of cqi/cqb

cqmax

Larger of cqi/cqb

Practical Examples

Responsive Typography

html {
  font-size: 14px;
}

@media (min-width: 768px) {
  html { font-size: 16px; }
}

@media (min-width: 1280px) {
  html { font-size: 18px; }
}

Responsive Grid

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

@media (min-width: 640px) {
  .grid { grid-template-columns: repeat(2, 1fr); }
}

@media (min-width: 1024px) {
  .grid { grid-template-columns: repeat(3, 1fr); }
}

@media (min-width: 1280px) {
  .grid { grid-template-columns: repeat(4, 1fr); }
}

Print Styles

@media print {
  /* Hide navigation and footers */
  nav, footer, .no-print { display: none; }

  /* Use black text on white */
  body {
    color: black;
    background: white;
  }

  /* Avoid page breaks inside elements */
  article, section {
    page-break-inside: avoid;
  }

  /* Show full URLs for links */
  a[href]::after {
    content: " (" attr(href) ")";
  }
}

Browser Support Notes

  • Media Queries Level 4 (range syntax): All modern browsers since 2023
  • Container Queries: Chrome 105+, Firefox 110+, Safari 16+
  • prefers-color-scheme: All modern browsers, IE not supported
  • prefers-reduced-motion: All modern browsers
  • hover/pointer: All modern browsers
  • color-gamut: Safari 10+, Chrome 58+, Firefox 110+