CSS @media Rule

Beginner
⏱️ 14 min read
📚 Updated: Jul 2026
🎯 24 Tutorials
Responsive

What You’ll Learn

The @media rule lets you apply styles based on device characteristics — viewport width, orientation, print mode, hover capability, and more. This hub explains media query syntax and links to 24 feature tutorials with try-it examples.

01

@media

Conditional.

02

width

Breakpoints.

03

print

Paper styles.

04

hover

Input type.

05

Mobile-first

min-width.

06

24 guides

Full index.

Introduction

The @media rule in CSS is a powerful feature that applies styles based on the characteristics of the device or viewport displaying the content. It is essential for responsive designs that adapt to various screen sizes and device types, ensuring a seamless experience across phones, tablets, and desktops.

What Are Media Queries?

A media query is the condition inside @media. When the condition is true, the enclosed CSS rules apply; when false, they are ignored. Combine multiple conditions with and, or, or not for precise targeting.

💡
Beginner Tip

Always include <meta name="viewport" content="width=device-width, initial-scale=1.0"> in your HTML. Without it, mobile browsers may not report the real viewport width and your media queries will not work as expected.

Default Value

The @media rule has no default value — it only activates when you write a query. If no media query matches, the default styles outside @media blocks are used.

📝 Syntax

The syntax for @media involves a media query that defines when the enclosed styles apply:

CSS
@media media-type and (condition) {
  /* CSS rules here */
}

media-type is optional (screen, print, all). condition is a media feature and value, such as max-width: 600px or orientation: landscape.

Property Values

ValueDescription
media-typeType of media: screen, print, or all (optional)
conditionFeature and value pair, e.g. max-width: 600px, orientation: landscape

Common patterns

CSS
/* Mobile-only (max-width) */
@media (max-width: 600px) { }

/* Desktop-up (min-width, mobile-first) */
@media (min-width: 768px) { }

/* Print stylesheet */
@media print { }

/* Combined conditions */
@media screen and (min-width: 600px) and (orientation: landscape) { }

⚡ Quick Reference

QueryWhen it matches
(max-width: 600px)Viewport 600px wide or narrower
(min-width: 768px)Viewport 768px wide or wider
printPage is being printed
(orientation: landscape)Device is wider than tall
(hover: hover)Primary input can hover
(prefers-reduced-motion: reduce)User prefers less animation

When to Use Media Queries

  • Responsive layouts — Stack columns on phones, expand grids on desktops with min-width / max-width.
  • Print styles — Hide navbars, expand links, use black text on white with @media print.
  • Touch vs mouse — Larger tap targets when (hover: none); hover effects when (hover: hover).
  • Orientation — Adjust layouts when the device rotates to landscape.
  • Accessibility — Reduce motion when prefers-reduced-motion: reduce.

👀 Live Preview

Resize your browser to see how max-width queries change behavior:

@media (max-width: 600px)

Default: dark text. Below 600px viewport this section would switch to compact mobile styles.

CSS Media Feature Tutorial Index

Search by feature name or browse by category. Every card links to a full guide with four try-it examples and FAQs.

Interaction Features

4 tutorials

Detect hover, pointer, and input capabilities.

Media Types

2 tutorials

Target screen displays or printed output.

Viewport Features

6 tutorials

Respond to window size, shape, and resolution.

Overflow Features

2 tutorials

Detect how content overflows the viewport.

Display & Color

6 tutorials

Color depth, gamut, and display characteristics.

App & UI Modes

1 tutorial

Progressive web app and display contexts.

Layout Features

1 tutorial

Grid layout capability detection.

Environment Features

2 tutorials

Scripting and update frequency.

Examples Gallery

Five responsive patterns from the reference guide. Resize the window or use DevTools device mode to test width-based queries.

📱 Responsive Layout

Adapt styles to viewport width and orientation.

Example 1 — Basic Usage (max-width)

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

responsive.html
body {
  color: #1e293b;
}

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

How It Works

Below 600px the nested body rule overrides the default color. This is the classic mobile breakpoint pattern.

Example 2 — Print Styles

Apply a white background when the page is printed.

print.html
body {
  background-color: #e2e8f0;
}

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

How It Works

@media print only applies during printing or print preview — not on screen.

Example 3 — Landscape Orientation

Change text color when the device is in landscape mode.

orientation.html
body {
  color: #1e293b;
}

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

How It Works

Rotate a phone or widen a narrow browser window to trigger orientation: landscape.

📈 Mobile-First & Combined

Build up from small screens and combine multiple conditions.

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

Mobile-first uses min-width to add layout complexity as screens grow — often cleaner than many max-width overrides.

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

Chain conditions with and. Every part of the query must be true for the rules to apply.

💬 Usage Tips

  • Mobile-first — Write base styles for small screens; use min-width to enhance.
  • Viewport meta tag — Required for accurate width queries on mobile.
  • Logical breakpoints — Use content-based breakpoints, not just device sizes.
  • DevTools — Chrome and Firefox device mode tests media queries instantly.
  • Separate print CSS — Keep print rules in a dedicated @media print block.

⚠️ Common Pitfalls

  • Missing viewport meta — Mobile browsers may use a fake wide viewport without it.
  • Too many breakpoints — Start with 2–3; add more only when content breaks.
  • px vs em in queriesem in media queries is relative to browser default, not your root font-size.
  • Hover on touch — Do not rely on hover alone; use any-hover or provide touch alternatives.
  • Print color waste — Dark backgrounds on screen can waste ink when printed.

♿ Accessibility

  • prefers-reduced-motion — Disable or shorten animations for users who request it.
  • prefers-contrast — Offer higher-contrast styles when supported.
  • Touch targets — Use (pointer: coarse) to enlarge buttons on touch devices.
  • Readable at all sizes — Test typography at every breakpoint, not just desktop.
  • Do not hide critical contentdisplay: none on mobile must not remove essential info.

🧠 How @media Works

1

Browser evaluates conditions

Viewport size, media type, and features are checked against your query.

Match
2

Rules apply or skip

Matching @media blocks merge into the cascade; non-matching blocks are ignored.

Cascade
3

Re-evaluate on change

Resize the window, rotate the device, or print — queries re-run automatically.

Live
=

Responsive page

Layout, typography, and visibility adapt to each device and context.

🖥 Browser Compatibility

The @media rule has 100% browser support in Chrome, Firefox, Safari, Edge, and Opera. It is a fundamental part of responsive CSS.

Baseline · Universal support

@media rule

All major browsers support @media and standard media features like width, orientation, and print.

100% Modern browsers
Google Chrome All versions · External CSS
Full support
Mozilla Firefox All versions · External CSS
Full support
Apple Safari All versions · External CSS
Full support
Microsoft Edge All versions · External CSS
Full support
Opera All modern versions
Full support
@media rule 100% supported

Bottom line: @media is safe for production responsive design. Some newer media features may have partial support — check individual feature tutorials for details.

🎉 Conclusion

The @media rule is crucial for responsive web design. It lets you create styles that adapt to different screen sizes, orientations, print mode, and input types.

Experiment with the five examples above, then explore the 24 feature tutorials in the index. Pair media queries with Flexbox, Grid, and relative length units for fully adaptive layouts.

💡 Best Practices

✅ Do

  • Use mobile-first min-width breakpoints
  • Include the viewport meta tag in HTML
  • Test in DevTools device mode
  • Provide print-specific styles
  • Respect prefers-reduced-motion

❌ Don’t

  • Hide essential content on mobile only
  • Rely on hover without touch fallbacks
  • Add dozens of arbitrary breakpoints
  • Forget print styles for long articles
  • Assume desktop width on phones without viewport meta

Key Takeaways

Knowledge Unlocked

Five things to remember about @media

Your map to 24 media feature tutorials.

5
Core concepts
W 02

width

Breakpoints.

Layout
prt 03

print

Paper CSS.

Type
min 04

Mobile-first

min-width.

Pattern
and 05

Combine

Multi-query.

Target

❓ Frequently Asked Questions

The @media rule applies a block of CSS only when a media query condition is true — for example when the viewport is under 600px wide, when printing, or when the device supports hover. It is the foundation of responsive web design.
A media query is the condition inside @media, such as (max-width: 600px) or (orientation: landscape). It can include a media type like screen or print, plus one or more media features combined with and, or, or not.
Mobile-first means you write base styles for small screens, then use min-width media queries to add enhancements for larger viewports. Example: default single column, then @media (min-width: 768px) for a two-column layout.
screen targets displays like monitors and phones. print targets printed pages. Use @media print to hide navigation, adjust colors, and optimize typography for paper.
width and max-width for viewport size, orientation for portrait/landscape, hover and pointer for input types, resolution for pixel density, and prefers-reduced-motion for accessibility.
Read the overview and syntax, try the five examples, then open the width or any-hover tutorial. For responsive layouts start with width; for touch vs mouse start with any-hover.

Start Your First Media Feature Tutorial

Open the width guide for responsive breakpoints, or any-hover for touch vs mouse.

any-hover 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.

24 people found this page helpful