Codectionary / Developer documentation / CSS

Media Queries

Media queries apply CSS conditionally based on characteristics of the device or viewport, most commonly width, forming the foundation of responsive design. They use the @media at-rule with a condition, and can be combined with and/or logic to test multiple conditions at once.

Syntax

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

Examples

Mobile-First Breakpoints

The recommended approach - base styles for mobile, then progressively enhance for larger screens.

.container {
  padding: 16px; /* mobile default */
}

@media (min-width: 768px) {
  .container {
    padding: 32px;
    max-width: 720px;
    margin: 0 auto;
  }
}

@media (min-width: 1024px) {
  .container {
    max-width: 960px;
  }
}

Combining Conditions

Testing multiple media features together, and targeting print output.

@media (min-width: 768px) and (orientation: landscape) {
  .sidebar { display: block; }
}

@media print {
  .no-print { display: none; } /* hide navigation, buttons, etc when printing */
}

Parameters and values

  • min-width / max-width (feature): Tests viewport width, the most common responsive breakpoint
  • orientation (feature): Tests portrait vs landscape orientation
  • print (media type): Applies styles specifically for printed output

Best practices

  • Write mobile-first CSS using min-width breakpoints, progressively adding complexity for larger screens, rather than max-width desktop-first overrides
  • Base breakpoints on where your own content actually starts looking cramped or awkward, rather than copying generic device-width numbers
  • Prefer container queries over media queries for component-level responsiveness - reserve media queries for page-level, viewport-driven layout
  • Use @media print to hide non-essential UI (navigation, buttons) and optimize layout specifically for printed pages

At a glance

Purpose
Presentation and layout
File extension
.css
Runs in
Web browsers
Usually used with
HTML and JavaScript

Specifications & further reading

Related CSS documentation