With the viewport tag in place, the phone now reports its real width. What's still missing is for the CSS to react to that width: for the gallery to go from three columns to one, for the home page offer to stop being a narrow strip and for the tables to stop breaking the page. The tool for this is the media query: a block of CSS rules that only applies when a condition about the device or the screen is met, such as "the viewport is at least 48rem wide" or "the user prefers dark mode". In this lesson you'll learn the @media syntax, media types, the most useful features (width, orientation, user preferences, pointer type), how to choose breakpoints based on the content rather than a list of phones, and why it's a good idea to write them in rem. In the project you'll rewrite the key parts of css/styles.css with the mobile-first approach and fix almost all the problems from the audit: the menu, the home page, the gallery, the tables and the forms.
Contents
- The
@mediarule - Media types:
screen,printandall - Width features:
min-width,max-widthand the range syntax - Other features: orientation, user preferences and pointer type
- Combining conditions:
and, comma andnot - Content-based breakpoints
- Why
rem(orem) in media queries - Where to write the queries
- Media queries outside CSS: the
mediaattribute - A print stylesheet
- Project: The Blue Cup, mobile-first
The @media rule
A media query is an at-rule, like @import, that wraps a group of normal rules:
.product-gallery {
grid-template-columns: 1fr;
}
@media (width >= 48rem) {
.product-gallery {
grid-template-columns: repeat(3, 1fr);
}
}Let's break it down:
@mediaopens the query.(width >= 48rem)is the condition: a media feature (width, the width of the viewport) compared with a value. It always goes in parentheses.- Inside the curly brackets go complete CSS rules, with their selector and declarations. Notice the two levels of brackets: those of
@mediaand those of each rule. - If the condition is met, the rules inside apply as if they were written at that point in the stylesheet. If it isn't met, they're ignored.
A query doesn't increase specificity. Inside and outside, .product-gallery is worth (0, 1, 0). When both rules apply, the one that appears later in the stylesheet wins, by source order. That's why the query always goes after the base rule it modifies.
The condition is evaluated continuously: if you narrow the computer's window or rotate the phone, the browser checks it again and applies or removes the rules instantly, without reloading.
Media types: screen, print and all
Before the condition, a query can specify the media type:
| Type | Applies to… |
|---|---|
all |
All devices (it's the default value if you don't write anything) |
screen |
Screens: computers, phones, tablets, televisions |
print |
Printing and print preview (also "Save as PDF") |
There used to be other types (handheld, tv, projection, speech…), but they're deprecated: browsers ignore them. In practice, you only need print for the print stylesheet, and in every other case you can leave out the type: @media (width >= 48rem) is equivalent to @media all and (width >= 48rem).
Width features: min-width, max-width and the range syntax
By far the most used feature is the viewport width. There are two ways of writing it:
The classic syntax: min-width and max-width
@media (min-width: 48rem) { … } /* the viewport is 48rem or wider */
@media (max-width: 47.99rem) { … } /* the viewport is narrower than 48rem */min-width: 48remreads as "minimum width 48rem": it's met when the viewport is at least 48rem wide. It's the typical query of the mobile-first approach: "from this width onwards, add…".max-width: 47.99remreads as "maximum width": it's met when the viewport is at most that value. It's the typical query of the desktop-first approach.- Both are inclusive:
min-width: 48remis met at exactly 48rem. That's why, so that amax-widthquery didn't overlap with themin-widthone for the same point, people used to write47.99rem, an ugly trick that's easy to get wrong.
The range syntax (modern)
Since 2023, all current browsers understand a more natural syntax, with comparison operators:
@media (width >= 48rem) { … } /* same as min-width: 48rem */
@media (width < 48rem) { … } /* less than 48rem, with no overlap */
@media (30rem <= width < 48rem) { … } /* between 30rem (inclusive) and 48rem (exclusive) */| Classic | Range | Meaning |
|---|---|---|
(min-width: 48rem) |
(width >= 48rem) |
48rem or more |
(max-width: 48rem) |
(width <= 48rem) |
48rem or less |
(max-width: 47.99rem) |
(width < 48rem) |
Less than 48rem |
(min-width: 30rem) and (max-width: 47.99rem) |
(30rem <= width < 48rem) |
From 30rem to less than 48rem |
The range syntax reads like maths, allows strict < and > (goodbye 47.99) and expresses an interval in a single condition. You'll use it in The Blue Cup's stylesheets from now on. You'll come across a huge amount of code with min-width and max-width: it's exactly the same thing.
There are also
height,min-heightandmax-heightfor the viewport's height. They're rarely used (for example, to shrink a very tall header when the phone is in landscape), because web content scrolls vertically and height is seldom a problem.
Other features: orientation, user preferences and pointer type
Media queries don't only measure width. These are the most useful ones:
| Feature | Values | It's met when… |
|---|---|---|
orientation |
portrait, landscape |
The viewport is taller than it is wide (portrait) or wider than it is tall (landscape) |
prefers-color-scheme |
light, dark |
The user has chosen the light or dark theme in their operating system |
prefers-reduced-motion |
no-preference, reduce |
The user has asked their system to reduce animations |
hover |
hover, none |
The primary input device can hover over elements (a mouse) or not (a finger) |
pointer |
fine, coarse, none |
The primary device is precise (mouse) or imprecise (finger) |
any-hover, any-pointer |
The same | The same, but with any connected device, not just the primary one |
orientation
Be careful: landscape doesn't mean "phone in landscape". A normal desktop window is also landscape. To decide the layout, it's almost always better to ask about the width.
prefers-color-scheme: dark mode
Thanks to The Blue Cup's CSS variables, offering a dark theme would be a matter of redefining the colours:
@media (prefers-color-scheme: dark) {
:root {
--color-text: #eeeeee;
--color-white: #1a1a1a;
--color-light-grey: #111111;
}
}- The query is met if the user's operating system is in dark mode.
- Inside, some of the
:rootvariables are redefined. Because all the rules in the stylesheet usevar(), they change automatically. - A complete dark theme requires reviewing all the colours and their contrast (a brand blue that reads well on white may not read well on black). That's why you won't add it to the project in this course, but the mechanism is clear.
prefers-reduced-motion: respecting people who get motion sickness
Some people experience dizziness or distraction from animations and smooth scrolling, and they say so in their system's accessibility settings. If your site had animations, you should reduce them for these users:
html {
scroll-behavior: smooth;
}
@media (prefers-reduced-motion: reduce) {
html {
scroll-behavior: auto;
}
}scroll-behavior: smoothmakes jumps to anchors (such as the menu page's "Back to top") scroll smoothly instead of jumping.- If the user has asked for reduced motion, it goes back to the instant jump.
The Blue Cup has no animations, so you don't need this rule; but if you ever add them, this query is a must. You'll come back to it in Web Accessibility Basics.
hover and pointer: mouse or finger
On a touchscreen there's no such thing as "hovering the mouse over". If some information only appears with :hover, people using their finger will never see it. And small targets, comfortable with a precise mouse, are hard to tap with a finger:
pointer: coarseis met when the primary device is imprecise, such as a finger.padding-blockis the top and bottom padding at the same time. It's increased so that each link is a taller touch target.
Golden rule:
:hovercan enhance the experience, but nothing essential should depend on it. And don't assume that "narrow screen" means "touch" or that "wide screen" means "mouse": there are huge tablets used with a finger and small laptops used with a mouse. For size, ask aboutwidth; for the input method, ask aboutpointerorhover.
Combining conditions: and, comma and not
| Operator | Example | It's met when… |
|---|---|---|
and |
(width >= 48rem) and (orientation: landscape) |
Both conditions are met |
, (comma) |
(width < 30rem), (pointer: coarse) |
Either of them is met (equivalent to "or") |
or |
(width < 30rem) or (pointer: coarse) |
The same as the comma, with the modern syntax |
not |
not print / (not (hover: hover)) |
The condition is not met |
only |
only screen and (…) |
A trick to hide the query from browsers of fifteen years ago; it isn't needed today |
Content-based breakpoints
A breakpoint is the width at which the layout changes: the value you write in (width >= …).
A very widespread mistake is to copy the widths of popular devices: "320 for the small iPhone, 375 for the regular iPhone, 768 for the iPad, 1024 for the iPad in landscape…". It has several problems:
- There are thousands of different screen sizes, and they change every year.
- The in-between widths (a half-screen window, an unfolded foldable) are left unaddressed.
- What matters isn't which device it is, but whether the content fits.
The professional alternative is to choose breakpoints based on the content:
flowchart TD
A["Start with the single-column design<br/>on the narrowest screen (320 px)"] --> B["Widen the window little by little<br/>in Responsive mode"]
B --> C{"Does the content look bad?<br/>Lines too long, huge gaps,<br/>wasted space"}
C -- No --> B
C -- Yes --> D["Note down that width:<br/>that's where a breakpoint goes"]
D --> E["Add a query with the smallest change<br/>(one more column, the menu in a row…)"]
E --> B
In other words: the content tells you where to break. If the single-column gallery starts showing huge photos from about 30rem wide, a second column fits there; if at 48rem the home page's side column already has room for about 14rem of readable text, the two-column design fits there.
Some practical guidelines:
- Few breakpoints: two or three are usually enough for a site like The Blue Cup. Every extra query is more code to maintain.
- Shared breakpoints: although each component could have its own breakpoint, using the same ones throughout the stylesheet makes the design more consistent and easier to remember.
- Grids that adapt by themselves: with Grid and
repeat(auto-fit, minmax(14rem, 1fr)), a grid creates as many columns as will fit, without any queries. You'll practise this in the exercises.
As a rough guide (not as a rule), many sites end up with breakpoints similar to these:
| Breakpoint | ≈ pixels (at 16 px per rem) | What usually happens from that point |
|---|---|---|
| No query | From 320 | One column, stacked menu |
30rem |
480 | Two columns in card grids, menu in a row |
48rem |
768 | Side columns, header on one line |
64rem |
1024 | Three or four columns, generous margins |
Why rem (or em) in media queries
Breakpoints can be written in px, but in a design based on rem it's a good idea to write them in relative units as well:
- In a media query,
1remand1emare equal to the browser's default font size (usually 16 px), not that of any element on the page. Both units behave the same here. - If a user increases their browser's default font size to 24 px, a
(width >= 48rem)query kicks in from 1152 px instead of 768: the two-column design only appears when there's room for the larger text. With768px, the two columns would appear anyway and the enlarged text wouldn't fit properly. - This way, the breakpoint is tied to the content (the text) and not to the screen, consistent with what you've just seen.
One limitation: CSS variables don't work inside the condition of a media query.
@media (width >= var(--medium-width))isn't valid. That's why breakpoints are written as numbers and documented with a comment at the top of the stylesheet.
Where to write the queries
There are two habits:
- All together at the end of the stylesheet, in a "Responsive" section.
- Each query next to the rule it modifies, inside its component's section.
The second is the more practical one in a stylesheet organised into sections like The Blue Cup's: when you open section 9, you can see at a glance how the gallery behaves at every width, without jumping to the end of the file. It's the convention you'll follow. You can repeat the same query (@media (width >= 48rem)) in several sections without any problem.
Media queries outside CSS: the media attribute
The same syntax appears in HTML, in the media attribute:
<link rel="stylesheet" href="css/print.css" media="print">
<meta name="theme-color" content="#1f4e79" media="(prefers-color-scheme: light)">- On
link, the stylesheet is only applied when the query is met. Note: the browser downloads it anyway (with lower priority, without blocking the page from rendering). You saw this in passing in Linking CSS to HTML. - On
meta name="theme-color", it lets you give a different bar colour for the light and the dark theme. - On
source, insidepicture, it lets you choose a different image depending on the width. This is the "art direction" you'll see in the next lesson, Responsive Images and Videos.
A print stylesheet
Someone might want to print The Blue Cup's menu to take it to the office. With @media print you can adapt the page to paper:
@media print {
body > header nav,
body > footer,
form {
display: none;
}
body {
max-width: none;
color: #000000;
background-color: #ffffff;
}
a[href^="http"]::after {
content: " (" attr(href) ")";
}
}- The menu, the footer and the forms are hidden: you can't click on paper.
- The maximum width is removed and black text on white is forced, to save ink.
a[href^="http"]::afteradds each external link's address in brackets after it: on paper you can't see where a link goes. You'll rememberattr()from Data Attributes.
To test it without wasting paper, use the browser's print preview (Ctrl+P) or, in DevTools, the Rendering panel (three-dot menu › More tools › Rendering) › Emulate CSS media type › print. You won't add it to the project now; there's an exercise for that.
Project: The Blue Cup, mobile-first
You're going to rewrite the parts of css/styles.css that cause the problems from the audit in Introduction to Responsive Design. The strategy, in each section:
- The rules without a query describe the phone: one column, smaller padding.
- The
(width >= …)queries add the layout for wider screens.
The project's breakpoints
Following the content-based method, when you widen the single-column version you observe that:
- From about 30rem (480 px), the gallery photos in a single column are already too big and the six stacked menu links waste space: two columns of products and the menu in a row fit.
- From about 48rem (768 px), the home page's side column would be close to 14rem, enough to read the offer comfortably; the logo and the menu fit on a single line; and there's room for three columns of products and for more generous padding.
Document this at the top of the stylesheet, before section 1:
/*
Breakpoints (mobile-first; rules without a query are for mobile):
- (width >= 30rem): menu in a row, gallery in two columns
- (width >= 48rem): header on one line, home page in two columns,
gallery in three columns, generous padding
Each query is written right after the base rule it modifies.
*/
- Header and menu (section 4)
Replace the body > header, .menu and .menu a rules with these (the .logo, :hover and [aria-current="page"] rules stay as they were):
/* ---------- 4. Header and menu ---------- */
body > header {
display: flex;
flex-direction: column;
gap: 1rem;
padding: 1rem;
border-bottom: 4px solid var(--color-brand);
}
@media (width >= 48rem) {
body > header {
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
padding: 1rem 2rem;
}
}
.menu {
display: flex;
flex-direction: column;
gap: 0.25rem;
margin: 0;
padding-left: 0;
list-style: none;
}
@media (width >= 30rem) {
.menu {
flex-direction: row;
flex-wrap: wrap;
}
}
.menu a {
display: block;
padding: 0.75rem;
color: var(--color-brand);
font-weight: bold;
text-decoration: none;
}
@media (width >= 30rem) {
.menu a {
padding: 0.5rem 0.75rem;
}
}Explanation:
- On mobile, the header is a flex column: the logo at the top and the
navbelow. In a flex column, the children stretch to the full width (align-items: stretchby default), so the menu takes up the whole screen. - The menu stacks: with
flex-direction: column, each link is on its own line. Withdisplay: blockandpadding: 0.75rem, the tappable area is the whole row, about 50 pixels tall: a comfortable target for a finger. - From 30rem, the menu switches to a row that wraps onto several lines if it doesn't fit, and the vertical padding is reduced: such tall targets are no longer needed in a row.
- From 48rem, the header gets back the Module 8 design: logo and menu at either end, vertically centred, with 2rem of padding.
- Six stacked links take up a good part of the phone's first screen. It's a simple, accessible solution that only uses HTML and CSS. The "menu button" pattern (the hamburger) that shows and hides the menu requires taking the keyboard and screen readers into account, and you won't see it in this course; the skip link you'll add in Web Accessibility Basics will help keyboard users skip past the menu.
- Padding for
main and the footer (section 5)
main and the footer (section 5)main {
padding: 1rem;
}
@media (width >= 48rem) {
main {
padding: 2rem;
}
}
/* … main > section, main > article, main > aside, unchanged … */
body > footer {
padding: 1.5rem 1rem;
background-color: var(--color-brand);
color: var(--color-white);
}
@media (width >= 48rem) {
body > footer {
padding: 2rem;
}
}On a 375-pixel screen, 2rem of padding on each side is 64 pixels: almost 20% of the width. With 1rem, the content gains space, and the generous padding comes back when there's room to spare.
- The home page (section 5)
Replace the whole .home block with this one:
.home {
display: grid;
gap: 1.5rem;
}
.home > * {
margin-bottom: 0;
}
@media (width >= 48rem) {
.home {
grid-template-columns: 2fr 1fr;
grid-template-areas:
"welcome welcome"
"bakery offer"
"book offer"
"opening-hours opening-hours"
"reviews reviews";
gap: 1.5rem 2rem;
}
.home > .welcome {
grid-area: welcome;
}
.home > #bakery {
grid-area: bakery;
}
.home > .featured {
grid-area: offer;
align-self: start;
}
.home > #book {
grid-area: book;
}
.home > #opening-hours {
grid-area: opening-hours;
}
.home > #reviews {
grid-area: reviews;
}
}- On mobile, the home page is a single-column grid (
grid-template-columnsisn't defined, and by default there's one). The blocks appear in HTML order: welcome, bakery, offer, booking, opening hours and reviews. It's the logical order you looked after in the semantics module, and now it's the visual order too.display: gridis kept so thatgapprovides the spacing, just as on the desktop. - From 48rem, the Module 8 two-column
2fr 1frplan is drawn. - The
grid-arearules go inside the query. This is important: if a rule asks forgrid-area: offerand the grid has no area with that name, the browser doesn't ignore it; instead, it creates implicit rows and columns to place it, and the block ends up in a strange position. The areas and their assignments must be activated together.
- Scrolling tables (section 7 and HTML)
An eight-column table can't be rearranged without losing its meaning: its rows and columns are the data. The standard solution is to let the table keep its natural width and have only the table scroll horizontally, inside a container, without dragging the rest of the page along with it.
In the HTML, wrap each table in a div. In index.html, the opening hours table:
<div class="table-scroll" tabindex="0" role="region" aria-label="Weekly opening hours by service">
<table>
<caption>Weekly opening hours by service</caption>
…
</table>
</div>Do the same in menu.html with the price table (aria-label="Price list") and with the lunch menu table (aria-label="Lunch menu"), using the text of its caption in each case.
Let's look at each attribute:
div class="table-scroll": adivis correct here, because it's a container with no meaning that exists only for presentation purposes (the convention from Introduction to Semantic HTML). The class describes its function.tabindex="0": adivdoesn't normally receive focus. If the table scrolls, keyboard users need to be able to focus the container to move it with the arrow keys; without focus, the hidden part of the table would be unreachable without a mouse. The value0adds the element to the tab order at its natural position in the HTML. (Some recent browsers already make scrollable areas focusable automatically, but not all of them.)role="region": an ARIA attribute that tells screen readers that thisdivis a region of the page with a name of its own. Without it, when it received focus, the screen reader would announce a meaningless generic element.aria-label="…": gives the region a name. When the user tabs to it, the screen reader will announce something like "Weekly opening hours by service, region", and the user will know what that focused element contains.
This is the first time you've used role and aria-label in the project. It's a textbook case: ARIA complements HTML where HTML isn't enough. You'll study the rules for using it properly in Using ARIA Roles and Attributes.
In the CSS, in section 7, before the table rule:
/* ---------- 7. Tables ---------- */
.table-scroll {
overflow-x: auto;
margin-bottom: 2rem;
}
.table-scroll > table {
margin-bottom: 0;
}
/* … table, caption, th, td, etc., unchanged … */overflow-x: autoshows a horizontal scroll bar only if the table is wider than the container. On a wide screen, nothing appears. You already usedoverflow-xwithpreblocks in The Box Model.- The bottom margin moves to the container, so that the scroll bar doesn't end up stuck to the following text.
- The container's focus will be visible thanks to the
:focus-visiblerule in section 2: nothing else is needed.
- Forms (section 8)
The fields have taken up 100% of the available width (up to 24rem) since Styling HTML Elements: on mobile they fill the screen without you doing anything. The problem was the fieldset padding, which piles up in the nested groups in bookings.html. Adjust the rule:
fieldset {
margin: 0 0 1.5rem;
padding: 0.75rem;
border: 1px solid var(--color-border);
border-radius: var(--radius);
}
@media (width >= 48rem) {
fieldset {
padding: 1rem 1.5rem;
}
}On mobile, each level of fieldset takes away only 0.75rem on each side, and the fields in the nested groups keep a comfortable width.
- The gallery (section 9)
.product-gallery {
display: grid;
grid-template-columns: 1fr;
gap: 1.5rem;
list-style: none;
padding-left: 0;
}
@media (width >= 30rem) {
.product-gallery {
grid-template-columns: repeat(2, 1fr);
}
}
@media (width >= 48rem) {
.product-gallery {
grid-template-columns: repeat(3, 1fr);
}
}- Mobile: one column; each tile takes up the full width and the photo looks large.
- From 30rem: two columns.
- From 48rem: the three columns from Module 8.
- The two queries are compatible: at 60rem both are met, and the second one wins because it appears later. That's why, in mobile-first, queries are ordered from smallest to largest.
- Check it
- Open
index.htmlin device mode, in "Responsive", and turn on Show media queries in the bar's three-dot menu. Two coloured strips appear above the page, one for each breakpoint. Click them to jump to each width. - At 320 and 375 px: stacked menu with tall links; single-column home page with the offer at full width between the bakery and the booking section; the opening hours table scrolls inside its box and the page doesn't.
- Press
Tabuntil you reach the opening hours table: the container shows the focus outline and scrolls with the left and right arrow keys. - At 30rem (480 px): the menu switches to a row; in
gallery.html, two columns of products. - At 48rem (768 px): the header on one line, the home page with its side column, the gallery in three columns.
- In the DevTools Styles panel, select
ul.product-galleryand change the width: you'll see how each query's rules are applied or appear struck through, with the query shown above each rule. - Go through
menu.html,bookings.htmlandcontact.htmlat 320 px: no horizontal scrolling should appear on the page. The only remaining exceptions are the map on the contact page and the YouTube video on the news page, with their fixed widths in pixels: you'll fix them in the next lesson.
Project status after the lesson
| File | Changes |
|---|---|
css/styles.css |
Comment with the breakpoints; sections 4, 5, 7, 8 and 9 rewritten mobile-first with (width >= 30rem) and (width >= 48rem) queries; new .table-scroll component |
index.html |
Opening hours table inside div.table-scroll |
menu.html |
Price and lunch menu tables inside div.table-scroll |
Common Mistakes and Tips
- Writing the query before the base rule: since the query doesn't increase specificity, the base rule, which comes later, overrides it. The query always goes after.
- Ordering mobile-first queries incorrectly: from smallest to largest (
30rem, then48rem). The other way round, the 30rem query would override the 48rem one on wide screens. - Forgetting the parentheses:
@media width >= 48remisn't valid; it must be@media (width >= 48rem). - Mixing mobile-first and desktop-first in the same stylesheet (some
>=queries and some<queries on the same properties): the result is hard to follow. Pick one approach. - Breakpoints for specific devices (
375px,414px,768px…): choose the widths at which your content calls for them. - Using variables in the condition:
@media (width >= var(--width))doesn't work. - Hiding content with
display: noneon mobile so that it "fits": mobile users lose it, and so does Google. - Making the table scrollable without
tabindex: keyboard users can't see the hidden columns. - Tip: width queries are for layout (columns, padding, the menu in a row or a column). Text already adapts by itself: you rarely need queries to change font sizes.
Exercises
Exercise 1. Rewrite this desktop-first stylesheet as mobile-first, with the same visual result at every width:
.review-list {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 2rem;
}
@media (max-width: 47.99rem) {
.review-list {
grid-template-columns: 1fr;
gap: 1rem;
}
}Exercise 2. The product gallery uses two queries to go from one column to two and then three. Write a version without any queries in which the grid automatically creates as many columns as will fit, with none narrower than 14rem. Then explain how its behaviour differs from that of the queries.
Exercise 3. Create The Blue Cup's print stylesheet: a css/print.css file linked from every page for printing only, which hides the menu, the footer, the forms, the videos, the audio and the maps; removes the grey background and the maximum width; and shows the address of external links. Test the result with the print preview of menu.html.
Solutions
Solution 1.
.review-list {
display: grid;
grid-template-columns: 1fr;
gap: 1rem;
}
@media (width >= 48rem) {
.review-list {
grid-template-columns: repeat(2, 1fr);
gap: 2rem;
}
}- The base rule now describes the phone: one column with 1rem of spacing.
- The query is inverted:
max-width: 47.99rem("less than 48rem") becomeswidth >= 48rem("48rem or more"), and inside goes what used to be the base rule. - Notice that the
47.99has disappeared: the range syntax and the mobile-first approach avoid that trick.
Solution 2.
.product-gallery {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr));
gap: 1.5rem;
list-style: none;
padding-left: 0;
}minmax(14rem, 1fr)defines columns that are at least 14rem wide and, at most, one fraction of the free space.repeat(auto-fit, …)creates as many columns as will fit with that minimum. At 320 px, one fits; at about 30rem, two; at about 46rem, three; on a very wide monitor without thebodylimit, four or more would fit.- The difference: with queries, you decide the number of columns at each breakpoint (and that's why there will never be four). With
auto-fit, the number depends on the container's space, not the viewport, and there are no queries to maintain. It's a very useful technique for card grids; if you want an exact limit on the number of columns, queries give you more control. Below a 14rem container (screens narrower than about 280 px) the column would overflow; you can prevent this withminmax(min(14rem, 100%), 1fr).
Solution 3. In the head of all seven pages, after the link to styles.css:
And the css/print.css file:
/* The Blue Cup's print stylesheet: only applies when printing */
body > header nav,
body > footer,
form,
video,
audio,
iframe {
display: none;
}
html,
body {
background-color: #ffffff;
}
body {
max-width: none;
color: #000000;
}
a[href^="http"]::after {
content: " (" attr(href) ")";
font-size: 0.875em;
}- The
media="print"attribute means the stylesheet only applies when printing. Because it comes afterstyles.css, its rules win when specificity is tied. - The elements that make no sense on paper are hidden. The logo is kept (only the header's
navis hidden) so that the printed menu carries the brand. - The
a[href^="http"]selector only affects links that start withhttp, that is, external ones: relative ones (menu.html) would add nothing on paper. - An equivalent alternative: an
@media print { … }block at the end ofstyles.css. A separate stylesheet is easier to find; a block inside avoids one extra download.
Conclusion
Media queries are conditional CSS rules: the @media rule applies a group of rules only when a condition is met, without increasing their specificity. You've seen the media types (screen, print, all), the width features in their classic form (min-width, max-width) and in the range syntax (width >= 48rem), and other very useful features: orientation, prefers-color-scheme, prefers-reduced-motion, hover and pointer. You know how to combine conditions, choose breakpoints based on the content, write them in rem to respect the user's font size and place each query next to the rule it modifies. You also know HTML's media attribute and how to get a page ready for printing. The Blue Cup is now mobile-first: the menu stacks on mobile and switches to a row from 30rem; the home page has a single column and adopts its 2fr 1fr plan from 48rem; the gallery goes from one to two and then three columns; the tables scroll inside a keyboard-accessible region; and the forms make use of the full width. There are only two frames left that overflow the screen, and one open question: why should a phone have to download the same large photo as a monitor? In the next lesson, Responsive Images and Videos, you'll learn to give each screen the right image with srcset, sizes and picture, and to make videos and frames adapt to the available width.
HTML Course
Module 1: Introduction to HTML
- What Is HTML?
- Setting Up Your Work Environment
- Basic Structure of an HTML Document
- Tags, Elements and Attributes
- Creating Your First HTML Page
- File Organisation and Paths
Module 2: Text Formatting in HTML
- Headings and Paragraphs
- Text Formatting Tags
- Lists: Ordered, Unordered and Description Lists
- Block Quotes and Preformatted Text
- Block and Inline Elements: div and span
Module 3: Links and Media in HTML
- Creating Hyperlinks
- Special Links: Anchors, Email, Phone and Downloads
- Adding Images
- Embedding Videos
- Using the Audio Element
Module 4: Tables in HTML
- Basic Table Structure
- Table Head, Body and Foot
- Merging Cells: colspan and rowspan
- Accessible Tables: caption, scope and headers
- Styling Tables
Module 5: Forms in HTML
- Creating a Basic Form
- Form Elements: input, textarea and select
- HTML5 Input Types
- Labels and Grouping: label, fieldset, legend and datalist
- Form Attributes and Validation
- Submitting Forms
Module 6: HTML5 Semantic Elements
- Introduction to Semantic HTML
- header, nav and footer
- article, section and aside
- main and figure
- Other Semantic Elements: time, address, details and dialog
Module 7: Advanced HTML Techniques
- HTML Entities
- Using iframes
- Data Attributes
- Metadata: meta, SEO and Open Graph
- Microdata and Structured Data
- Graphics in HTML: SVG and canvas
Module 8: Integrating HTML and CSS
- Linking CSS to HTML
- Inline, Internal and External CSS
- Basic CSS Selectors
- The Box Model
- Styling HTML Elements
- Introduction to Layout with Flexbox and Grid
Module 9: Responsive Web Design
Module 10: Best Practices and Accessibility
- HTML Best Practices
- Validating HTML Code
- Web Accessibility Basics
- Using ARIA Roles and Attributes
- Accessibility Testing
- Performance and Resource Loading
