@media
Applies styles only when the device meets a condition: a matching window width, a dark theme, a mouse, or printing the page.
Example
#.courses {
display: grid;
gap: 10px;
}
@media (width >= 500px) {
.courses {
grid-template-columns: repeat(3, 1fr);
}
}<style>
.course { padding: 12px 14px; border: 1px solid #27272a; border-radius: 12px; background: #18181b; }
.course strong { display: block; }
.course span { color: #a1a1aa; font-size: 13px; }
</style>
<div class="courses">
<div class="course"><strong>HTML and CSS</strong><span>11 modules</span></div>
<div class="course"><strong>JavaScript</strong><span>12 modules</span></div>
<div class="course"><strong>Python</strong><span>12 modules</span></div>
</div>On a computer the frame is wider than 500 px and the cards sit in three columns. On a narrow phone screen a single column remains, which is the base style.
Definition and usage
#The @media rule wraps part of a stylesheet in a condition. The styles inside apply only when the browser window or the device meets the given criteria: it has a certain width, prefers a dark theme, supports hover, or is printing the page. The browser evaluates the condition continuously, so when you narrow the window or change the theme, the styles switch on and off without a reload.
The most popular approach is mobile first. You design the base style, written outside any rule, for a narrow phone screen, and @media (width >= …) rules add changes for wider and wider screens: more columns, larger spacing, a horizontal menu. The stylesheet ends up shorter, because nothing has to be overridden on a phone, and a single column is a natural starting point for content.
You can write the width in two ways. The classic one is (min-width: 600px) and (max-width: 599px), the newer one is range syntax: (width >= 600px), (width < 600px), and even (600px <= width < 900px). They work the same, but ranges read like an ordinary inequality. You combine several conditions with the word and, write alternatives with a comma, and not negates the whole query.
A common pitfall: without <meta name="viewport" content="width=device-width, initial-scale=1">, a phone pretends to have a wide screen (usually about 980 px) and scales the page down, so breakpoints for small screens never apply. Also remember that @media asks about the whole window. When a component should adapt to the space it gets in the layout, use @container.
Syntax
#@media [<media type> and]? (<condition>) [and (<condition>)]* {
selector { property: value; }
}Media features
#(width >= 300px)
The style applies from the threshold upward, just like the classic(min-width: 300px). Each lamp has its own condition and lights up when the preview frame is at least as wide as its threshold. The current frame width is shown below the lamps.(width < 500px)
The style applies only below the threshold. The classic way is(max-width: 499px), which leaves a gap for fractional window widths such as 499.5 px, while the<form has no gap. This time the lamps light up when their threshold is larger than the frame width.(prefers-color-scheme: dark)
Asks about the theme chosen in the system or browser:lightordark. The card is light, and with a dark theme it switches to dark colors. Change the theme in your system settings and the card updates live.(prefers-reduced-motion: no-preference)
Checks whether the user has asked the system to reduce motion. Here the animation runs only with theno-preferencevalue, that is, for people who have not turned this option on. With reduced motion enabled (reduce), the tile stays still.(hover: hover)
Whether the primary pointing device can hover over elements. Hover over the button: the effect works with a mouse or trackpad, while on a phone the rule does not apply, so no highlight stays “stuck” after a tap.(pointer: coarse)
How precise the primary pointer is:fineis a mouse or stylus,coarseis a finger. On a touch screen the buttons grow to at least 44 px tall, so they are easy to hit. With a mouse they stay compact.(orientation: landscape)
Compares the window’s height and width:landscapewhen the window is wider than it is tall,portraitotherwise. The preview frame is landscape, so the image sits next to the text.print
This is a media type, not a feature: the styles apply when printing and saving to PDF. In print, the buttons disappear and the background turns white. The preview is a screen, so here you see the screen version.
Media features in practice
#min-width
(min-width: 300px). Each lamp has its own condition and lights up when the preview frame is at least as wide as its threshold. The current frame width is shown below the lamps.@media (width >= 300px) {
.a { background: #10b981; color: #fff; }
}
@media (width >= 500px) {
.b { background: #10b981; color: #fff; }
}
@media (width >= 700px) {
.c { background: #10b981; color: #fff; }
}max-width
(max-width: 499px), which leaves a gap for fractional window widths such as 499.5 px, while the < form has no gap. This time the lamps light up when their threshold is larger than the frame width.@media (width < 300px) {
.a { background: #f59e0b; color: #000; }
}
@media (width < 500px) {
.b { background: #f59e0b; color: #000; }
}
@media (width < 700px) {
.c { background: #f59e0b; color: #000; }
}prefers-color-scheme
light or dark. The card is light, and with a dark theme it switches to dark colors. Change the theme in your system settings and the card updates live.@media (prefers-color-scheme: dark) {
.card {
background: #18181b;
color: #f4f4f5;
border-color: #3f3f46;
}
}prefers-reduced-motion
no-preference value, that is, for people who have not turned this option on. With reduced motion enabled (reduce), the tile stays still.@media (prefers-reduced-motion: no-preference) {
.mover {
animation: slide 1.4s infinite alternate;
}
}hover
@media (hover: hover) {
.btn:hover {
background: #10b981;
translate: 0 -3px;
}
}pointer
fine is a mouse or stylus, coarse is a finger. On a touch screen the buttons grow to at least 44 px tall, so they are easy to hit. With a mouse they stay compact.@media (pointer: coarse) {
.btn {
min-height: 44px;
padding-inline: 20px;
}
}orientation
landscape when the window is wider than it is tall, portrait otherwise. The preview frame is landscape, so the image sits next to the text.@media (orientation: landscape) {
.orient {
display: flex;
align-items: center;
gap: 12px;
}
.orient p { margin: 0; }
}@media print {
.toolbar { display: none; }
.doc { background: #fff; color: #000; }
}More examples
#:root {
--bg: #ffffff;
--text: #18181b;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #18181b;
--text: #f4f4f5;
}
}
.panel {
background: var(--bg);
color: var(--text);
}<div class="panel" style="padding: 14px 16px; border: 1px solid #52525b; border-radius: 12px;">
<strong>Your progress</strong>
<p style="margin: 4px 0 0;">Lessons completed: 18 of 24</p>
</div>The panel takes the colors of the theme set in the system. You override the variables in one place, and all components that use them adapt on their own.
Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- Start with the phone layout and add
@media (width >= …)rules. Choose breakpoints based on the content: add a breakpoint where the layout starts to break, not for specific device models. - Check that
<head>contains<meta name="viewport" content="width=device-width, initial-scale=1">. Without it, phone breakpoints will not apply. - Wrap animations in
@media (prefers-reduced-motion: no-preference). People who are sensitive to motion will then see a calmer version of the page.
Related entries
#- @containerApplies styles based on the size of a container instead of the whole window. A component adapts to the space it gets in the layout.
- @supportsApplies styles only in browsers that support a given property, value, or selector. It lets you use new features safely.
- UnitsAn overview of length units in CSS: from fixed pixels, through font-relative em and rem, to viewport, container, and grid units.
- :hoverMatches the element the mouse cursor is currently over. The basis of hover effects on buttons, links, and cards.
- grid-template-columnsDefines the grid’s columns: their number and widths.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.