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.
Fundamentals
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.
Foundation
📝 Syntax
The syntax for @media involves a media query that defines when the enclosed styles apply:
CSS
@mediamedia-typeand(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
Value
Description
media-type
Type of media: screen, print, or all (optional)
condition
Feature and value pair, e.g. max-width: 600px, orientation: landscape
Sidebar appears only when all conditions match: screen + min-width 600px + landscape.
How It Works
Chain conditions with and. Every part of the query must be true for the rules to apply.
Tips
💬 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.
Watch Out
⚠️ 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 queries — em 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.
A11y
♿ 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 content — display: 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.
Compatibility
🖥 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 ChromeAll versions · External CSS
Full support
Mozilla FirefoxAll versions · External CSS
Full support
Apple SafariAll versions · External CSS
Full support
Microsoft EdgeAll versions · External CSS
Full support
OperaAll modern versions
Full support
@media rule100% supported
Bottom line:@media is safe for production responsive design. Some newer media features may have partial support — check individual feature tutorials for details.
Wrap Up
🎉 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.
Assume desktop width on phones without viewport meta
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about @media
Your map to 24 media feature tutorials.
5
Core concepts
@01
@media
Conditional.
Rule
W02
width
Breakpoints.
Layout
prt03
print
Paper CSS.
Type
min04
Mobile-first
min-width.
Pattern
and05
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.