CSS Media Queries

Beginner
14 min read
Updated: Nov 2026
5 examples
24 feature guides

Definition and Usage

Media queries let your CSS react to the device and viewport—width, orientation, print mode, hover ability, and more. You write them with the @media rule so styles apply only when a condition is true.

@media vs media query

The @media rule is the CSS at-rule that wraps a block of styles. The media query is the condition after @media (for example (max-width: 600px)). When the query matches, those styles apply; when it does not, the browser ignores that block.

What should beginners learn first?

Start with width (min-width / max-width) for responsive layouts, then print for paper styles, orientation for portrait vs landscape, and hover for mouse vs touch. Those cover most CSS tutorial needs.

Hub vs single-feature pages

This page teaches how media queries work in a CSS tutorial. Each name in the index links to a focused feature guide (for example CSS width Media Feature). For the properties you change inside a query, see CSS Properties; for targeting elements, see CSS Selectors.

Viewport meta required.

Include <meta name="viewport" content="width=device-width, initial-scale=1"> in HTML. Without it, mobile browsers may not report the real width and your queries will misfire.

Base styles first.

Write default CSS outside @media, then override inside queries. Do not put every style inside a media block.

Prefer mobile-first.

Base styles for small screens, then min-width to enhance larger viewports—usually cleaner than stacking many max-width overrides.

CSS Syntax

An @media rule takes an optional media type plus one or more features:

CSS
@media media-type and (feature: value) {
  /* CSS rules here */
}

/* Everyday patterns */
@media (max-width: 600px) { }
@media (min-width: 768px) { }
@media print { }
@media screen and (min-width: 600px) and (orientation: landscape) { }

media-type is optional (screen, print, all). Features sit in parentheses and can be chained with and.

Core CSS Concepts

How media queries are grouped when you style real pages:

KindExamplesUse for
Viewport sizewidth, heightResponsive breakpoints
Media typesscreen, printDisplay vs paper
Orientationorientation, aspect-ratioPortrait / landscape layouts
Inputhover, pointerTouch vs mouse polish
Displayresolution, color-gamutPixel density and color range

Common queries cheat sheet

QueryWhen it matches
(max-width: 600px)Viewport 600px wide or narrower
(min-width: 768px)Viewport 768px wide or wider (mobile-first)
printPrinting or print preview
(orientation: landscape)Viewport wider than tall
(hover: hover)Primary input can hover

When to use which query

JobApproach
Responsive layoutBase mobile CSS + min-width (width)
Print stylesheet@media print — hide nav, simplify colors
Touch vs mouse(hover: none) for larger taps; (hover: hover) for hover polish
Device rotationorientation portrait / landscape

How to Use Media Queries in CSS

A practical workflow for this tutorial track:

  1. Add the viewport meta tag to your HTML document.
  2. Write base styles for the smallest screen you support.
  3. Add @media (min-width: …) blocks to enhance tablet and desktop layouts.
  4. Use @media print for print-only tweaks (hide chrome, dark text on white).
  5. Wrap hover-only polish in (hover: hover) so touch users are not blocked.
  6. Open a feature page from the index when you need values, examples, and FAQs.

Live Preview

Drag the slider to simulate viewport width. Below 600px the sample switches styles—same idea as @media (max-width: 600px).

Breakpoint at 600px — narrow viewports get the red compact style.

@media (max-width: 600px)

Wide viewport: default styles. Shrink below 600px to trigger the query.

CSS Examples

Five everyday patterns from a CSS tutorial: max-width, print, orientation, mobile-first min-width, and combined queries.

Example 1 — max-width breakpoint

Change text color when the viewport is 600px or less.

CSS
body {
  color: #1e293b;
}

@media (max-width: 600px) {
  body {
    color: #dc2626;
  }
}
width Tutorial

How It Works

  1. Base rule sets the default color for all viewports.
  2. max-width: 600px matches when the viewport is 600px or narrower.
  3. Nested body rule overrides color only while the query matches.
  4. Resize the window or use DevTools device mode to test.

Example 2 — Print styles

Apply a white background when the page is printed.

CSS
body {
  background-color: #e2e8f0;
}

@media print {
  body {
    background-color: #fff;
  }
}
print Tutorial

How It Works

  1. @media print targets print and print preview only.
  2. Screen styles stay untouched for normal browsing.
  3. White background reduces ink and improves contrast on paper.
  4. Also hide nav, ads, and decorative chrome in print blocks.

Example 3 — Landscape orientation

Change text color when the device is in landscape mode.

CSS
body {
  color: #1e293b;
}

@media (orientation: landscape) {
  body {
    color: #2563eb;
  }
}

How It Works

  1. orientation: landscape matches when width > height.
  2. Rotate a phone or widen a short browser window to trigger it.
  3. Portrait keeps the base styles outside the query.
  4. Combine with width when you need both size and rotation.

Example 4 — Mobile-first min-width

Start with a single column; add a two-column grid from 768px upward.

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

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

How It Works

  1. Base grid is one column—good for phones.
  2. min-width: 768px adds a second column as space grows.
  3. Mobile-first adds complexity instead of fighting it with max-width.
  4. gap spaces columns without margin hacks.

Example 5 — Combined media query

Match screen devices that are at least 600px wide and in landscape.

CSS
@media screen and (min-width: 600px) and (orientation: landscape) {
  .sidebar {
    display: block;
  }
}
screen Tutorial

How It Works

  1. screen limits the query to displays (not print).
  2. and requires every condition to be true.
  3. min-width and orientation refine when the sidebar shows.
  4. Miss one condition and the whole block stays inactive.

CSS Media Feature Tutorial Index

Search by feature name or browse by category. Every row links to a full guide with try-it examples and FAQs (24 tutorials).

Interaction Features

4 tutorials

Detect hover, pointer, and input capabilities.

FeatureDescriptionTutorial
hoverPrimary input can hover over elements.Try it
any-hoverAny input mechanism can hover.Try it
any-pointerAny input has fine or coarse pointer.Try it
pointerPrimary pointer is fine or coarse.Try it

Media Types

2 tutorials

Target screen displays or printed output.

FeatureDescriptionTutorial
printStyles applied when printing a page.Try it
screenStyles for screen-based devices.Try it

Viewport Features

6 tutorials

Respond to window size, shape, and resolution.

FeatureDescriptionTutorial
aspect-ratioMatch viewport width-to-height ratio.Try it
heightQuery viewport height.Try it
widthQuery viewport width — most common for responsive layout.Try it
orientationPortrait or landscape orientation.Try it
resolutionDevice pixel density (dpi, dppx).Try it
scanProgressive vs interlaced displays.Try it

Overflow Features

2 tutorials

Detect how content overflows the viewport.

FeatureDescriptionTutorial
overflow-blockBlock-axis overflow behavior.Try it
overflow-inlineInline-axis overflow behavior.Try it

Display & Color

6 tutorials

Color depth, gamut, and display characteristics.

FeatureDescriptionTutorial
colorBits per color component.Try it
color-gamutsRGB, P3, or rec2020 color range.Try it
color-indexIndexed-color display lookup table size.Try it
inverted-colorsUser has inverted colors enabled.Try it
light-levelAmbient light level (dim, normal, washed).Try it
monochromeMonochrome display bit depth.Try it

App & UI Modes

1 tutorial

Progressive web app and display contexts.

FeatureDescriptionTutorial
display-modebrowser, standalone, fullscreen, minimal-ui.Try it

Layout Features

1 tutorial

Grid layout capability detection.

FeatureDescriptionTutorial
gridWhether device supports CSS Grid.Try it

Environment Features

2 tutorials

Scripting and update frequency.

FeatureDescriptionTutorial
scriptingWhether JavaScript is available.Try it
updateHow fast the output can update.Try it

Edge Cases & Pitfalls

Most media-query mistakes are missing viewport meta, desktop-first clutter, or hiding content behind hover.

meta

No viewport meta

Without width=device-width, phones often report ~980px and your breakpoints never fire.

px

Too many max-width overrides

Desktop-first sheets grow messy. Prefer base mobile styles plus min-width enhancements.

and

Over-combined queries

Chaining many and conditions can make a block almost never match. Keep queries focused.

hover

Essential UI only on :hover

Touch devices may never hover. Keep critical actions visible; wrap decorative hover styles in (hover: hover).

em

em breakpoints surprise

Media query em values are relative to the browser default font size, not your root CSS font-size.

print

Forgetting print styles

Users still print docs and invoices. Hide chrome and force readable contrast with @media print.

Browser Support

Core @media features—width, height, orientation, screen, and print—are Baseline CSS. Newer features vary; each feature tutorial notes its own support.

✓ Baseline · Widely available

CSS media queries

Responsive @media rules work in all major browsers. Check individual guides for newer media features.

100% core queries
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
Core media queries Universal

Bottom line: Learn width, print, orientation, and hover first—they are production-safe. Newer features document compatibility on their own pages.

Key Takeaways

  • @media applies CSS only when a media query matches.
  • Start with width breakpoints, then print, orientation, and hover.
  • Mobile-first: base styles + min-width enhancements.
  • Next: width, print, or the feature index above.

One line: Media queries adapt CSS to viewport and device—set the viewport meta, style mobile first, then open feature guides from the index.

Frequently Asked Questions

The @media rule wraps CSS that applies only when a media query matches—for example a max viewport width, print mode, or hover capability. It is the foundation of responsive CSS in this tutorial.
A media query is the condition after @media, such as (max-width: 600px) or (orientation: landscape). It can include an optional media type (screen, print) plus features combined with and, or, or not.
Mobile-first means you write base styles for small screens, then use min-width queries to add layout for larger viewports. Example: one column by default, then @media (min-width: 768px) for two columns.
screen targets displays (phones, monitors). print targets paper or print preview. Use @media print to hide navigation, simplify colors, and tune typography for printing.
Start with width (min-width / max-width), then print, orientation, and hover. Those cover responsive layouts, print styles, rotation, and touch vs mouse.
Open the width tutorial for breakpoints, or continue with any-hover from the sidebar. Pair media queries with CSS Properties so you know what styles change inside each query.

Start with a breakpoint tutorial

Open width for responsive layouts, or continue with any-hover from the sidebar.

width tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful