Sub topic 3
Anatomy of a media query
This chapter assumes you know basic HTML structure, CSS selectors and the cascade, and units such as px, em, and rem. It also assumes you have set the viewport meta tag from the previous chapter. Here you will learn what each part of a media query does, so you can read and write one without guessing.
The parts of an @media rule
A media query is a condition that the browser evaluates. If the condition is true, the CSS rules inside the block apply. If it is false, the browser ignores them.
@media screen and (min-width: 40em) {
.nav {
display: flex;
}
}This rule has four parts:
@mediais the at-rule keyword that starts the query.screenis the media type. It says what kind of device the rule is for.(min-width: 40em)is a media feature in parentheses. It is a test with a name and a value.- The curly braces hold ordinary CSS rules, which can include as many selectors as you like.
The word and joins the type and the feature. The next chapters cover width features and combining conditions in detail. Here the focus is on the structure.
Media types
The media type is optional. If you leave it out, the type defaults to all.
| Type | Matches |
|---|---|
| `all` | Every device (the default) |
| `screen` | Computer, tablet, and phone screens |
| `print` | Printed pages and print preview |
Older specifications listed more types, such as tv and handheld. They are deprecated and no longer match anything useful. Use only these three.
A query with no type, such as @media (min-width: 40em), is the most common form in modern stylesheets.
Media features
A media feature always sits inside parentheses. Each feature tests one property of the device or the user's settings. For example, width tests the viewport width, and orientation tests whether the viewport is taller than it is wide.
@media (orientation: landscape) {
.hero {
min-height: 50vh;
}
}Some features also work as a plain boolean test with no value. (hover) is true if the primary input can hover at all.
The not and only keywords
You can put not at the start of a query to invert the whole thing. @media not print applies to everything except printing.
only was designed to hide a query from very old browsers that did not understand features. Current browsers do not need it, so most code leaves it out.
Query lists
A comma separates several queries. The rules apply if any one of them is true.
@media print, (orientation: portrait) {
.sidebar {
display: none;
}
}Common errors
- Missing parentheses.
@media min-width: 40emis invalid. The feature needs parentheses:(min-width: 40em). - Missing space after
and.screen and(min-width: 40em)is invalid because the browser readsand(as a function name. Writeand (. - Expecting extra specificity. A rule inside
@mediahas the same specificity as one outside it. If two rules tie, the one later in the file wins. Put your media queries after the base rules they override. - Unitless values.
(min-width: 40)is invalid. Lengths need a unit, such as40em.
Exercise
This rule never applies. Name the two syntax errors and write the corrected rule: @media screen and(min-width 600px) { .box { color: red; } }
Show answer
There is no space between and and the opening parenthesis, and the feature has no colon between its name and value. The corrected rule is @media screen and (min-width: 600px) { .box { color: red; } }.