TExcelTAD
Home › CSS › CSS Media Queries

CSS Media Queries

A media query applies CSS only when certain conditions are true — most often, when the screen is wider or narrower than a given size. Media queries are the core tool of responsive design: one page that adapts to phones, tablets and desktops.

Note: The playgrounds on this page are resizable. Drag the edge of the preview narrower and wider to watch the queries switch on and off. The breakpoints are deliberately small (around 400–600px) so you can trigger them easily.

Syntax

css
@media media-type and (condition) {
  /* rules that apply only when the condition is true */
}
html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; margin: 0; padding: 20px; background: #ffcdd2; transition: background .3s; }
  .msg::after { content: "Narrow: under 500px"; font-size: 22px; font-weight: bold; }
  @media (min-width: 500px) {
    body { background: #c8e6c9; }
    .msg::after { content: "Wide: 500px or more"; }
  }
</style>
</head>
<body>
  <div class="msg"></div>
  <p>Resize the frame across 500px.</p>
</body>
</html>

Media types and features

Media type Meaning
all Every device (default)
screen Screens: phones, tablets, monitors
print Printed pages and print preview
Feature Example True when
min-width (min-width: 600px) Viewport is at least 600px wide
max-width (max-width: 599px) Viewport is at most 599px wide
range syntax (400px <= width < 800px) Width in the range
orientation (orientation: landscape) Wider than tall
prefers-color-scheme (prefers-color-scheme: dark) User chose dark mode
prefers-reduced-motion (prefers-reduced-motion: reduce) User wants less animation
hover (hover: hover) Primary pointer can hover (mouse)

Combine conditions with and, list alternatives with a comma (or), and negate with not.

Mobile-first breakpoints

The recommended approach is mobile first: write the base styles for small screens, then add min-width queries that enhance the layout as space grows.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  * { box-sizing: border-box; }
  body { font-family: system-ui, sans-serif; margin: 0; padding: 10px; }
  .grid { display: grid; grid-template-columns: 1fr; gap: 10px; }
  .grid div { background: #5c6bc0; color: white; padding: 18px; border-radius: 8px; text-align: center; }
  @media (min-width: 400px) {
    .grid { grid-template-columns: 1fr 1fr; }
    .grid div { background: #26a69a; }
  }
  @media (min-width: 600px) {
    .grid { grid-template-columns: repeat(4, 1fr); }
    .grid div { background: #ef6c00; }
  }
</style>
</head>
<body>
  <div class="grid"><div>1</div><div>2</div><div>3</div><div>4</div></div>
  <p>Under 400px: 1 column (blue) · 400px+: 2 columns (teal) · 600px+: 4 columns (orange)</p>
</body>
</html>

Tip: Choose breakpoints where your content starts to look wrong, not by specific device sizes. Devices change every year; your content doesn't.

A responsive navigation bar

A typical pattern: stacked links on small screens, a horizontal bar on wider ones.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; margin: 0; }
  nav { background: #263238; }
  nav .logo { color: #ffca28; font-weight: 800; padding: 12px 16px; }
  nav ul { list-style: none; margin: 0; padding: 0; }
  nav a { display: block; padding: 10px 16px; color: white; text-decoration: none; border-top: 1px solid #37474f; }
  nav a:hover { background: #37474f; }
  @media (min-width: 520px) {
    nav { display: flex; align-items: center; justify-content: space-between; }
    nav ul { display: flex; }
    nav a { border-top: none; }
  }
</style>
</head>
<body>
  <nav>
    <div class="logo">★ Brand</div>
    <ul><li><a href="#">Home</a></li><li><a href="#">Products</a></li><li><a href="#">About</a></li></ul>
  </nav>
  <p style="padding:0 16px">Resize across 520px.</p>
</body>
</html>

User preference queries

Media queries can also respect user settings. This example honours dark mode and reduced motion.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  :root { --bg: #fafafa; --fg: #212121; }
  @media (prefers-color-scheme: dark) {
    :root { --bg: #212121; --fg: #fafafa; }
  }
  body { background: var(--bg); color: var(--fg); font-family: system-ui, sans-serif; padding: 20px; }
  .spin { width: 60px; height: 60px; border-radius: 12px; background: linear-gradient(45deg, #e91e63, #ff9800); animation: spin 2s linear infinite; }
  @keyframes spin { to { transform: rotate(360deg); } }
  @media (prefers-reduced-motion: reduce) {
    .spin { animation: none; }
  }
  @media print {
    .spin { display: none; }
  }
</style>
</head>
<body>
  <p>Colours follow your system's light/dark setting.</p>
  <div class="spin"></div>
  <p>The square stops spinning if your OS has "reduce motion" on.</p>
</body>
</html>

Where media queries can go

Location Example
Inside a stylesheet @media (min-width: 600px) { … }
On a <link> <link rel="stylesheet" href="print.css" media="print">
In @import @import url(wide.css) (min-width: 900px);

Also remember the viewport meta tag in every responsive page; without it, mobile browsers pretend to be ~980px wide and your queries won't fire as expected:

html
<meta name="viewport" content="width=device-width, initial-scale=1">

Test yourself

In a mobile-first stylesheet, which kind of query do you mostly write?

  1. max-width
  2. min-width
  3. orientation
  4. print

When does @media (max-width: 600px) apply?

  1. When the viewport is wider than 600px
  2. When the viewport is 600px wide or narrower
  3. Only on phones
  4. Only when printing

Which media feature detects that the user prefers dark mode?

  1. (theme: dark)
  2. (prefers-color-scheme: dark)
  3. (color: dark)
  4. (dark-mode: on)