Sub topic 5
Enhancing Larger Screens with Min-Width Media Queries
This chapter assumes you can write basic HTML and CSS, know how rem, em and px differ, and have a single-column base layout that already works at phone width. The previous chapters covered the viewport meta tag and the fluid base layout. Here you add layout changes for wider screens without rewriting that base.
The idea: start with the base, then add
In a mobile-first stylesheet, the rules outside any media query are the small-screen design. They apply to every device, including large ones. A min-width media query then says, "from this width upward, also apply these rules."
Each query only adds or overrides a few properties. The browser still reads the base rules first, so the small-screen design is the starting point on every device.
A min-width query looks like this:
@media (min-width: 40em) {
/* rules that apply when the viewport is at least 40em wide */
}The condition is true when the viewport width is greater than or equal to the value. At the browser's default font size, 40em is 640 CSS pixels. Because the condition is inclusive, a viewport of exactly 640px matches.
A worked example: the card list
Here is a complete page you can save as cards.html and open in a browser. It includes the viewport meta tag from the earlier chapter, because without it a phone ignores your queries.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Card list</title>
<style>
body {
font-family: system-ui, sans-serif;
margin: 0;
}
.card-list {
display: grid;
gap: 1rem;
padding: 1rem;
}
.card {
border: 1px solid #ccc;
padding: 1rem;
}
@media (min-width: 40em) {
.card-list {
grid-template-columns: repeat(2, 1fr);
}
}
@media (min-width: 64em) {
.card-list {
grid-template-columns: repeat(3, 1fr);
}
}
</style>
</head>
<body>
<main class="card-list">
<article class="card">One</article>
<article class="card">Two</article>
<article class="card">Three</article>
<article class="card">Four</article>
<article class="card">Five</article>
<article class="card">Six</article>
</main>
</body>
</html>The base rules create a grid with no column template, so each card takes a full-width row. That is a single column. Below 40em nothing else applies.
Widen the window past 40em and the first query matches. It sets grid-template-columns to two equal columns. Past 64em both queries match, and the second one wins because it comes later and has the same specificity. The result is three columns.
The gap and padding are never repeated. The queries contain only the properties that change.
Why order and specificity matter
A media query adds no specificity to the rules inside it. A rule inside @media has the same weight as the same selector outside it. When both match, the one that appears later in the stylesheet wins.
For this reason, put each min-width query after the base rules it overrides, and list queries from smallest to largest. If you place the base .card-list rule below the media queries, it overrides them. The layout then stays at one column on every screen, which is a common mistake.
/* Wrong order: the base rule comes last and cancels the query */
@media (min-width: 40em) {
.card-list {
grid-template-columns: repeat(2, 1fr);
}
}
.card-list {
grid-template-columns: none;
}Here the base rule matches at every width and appears later, so the query never takes effect.
Why min-width suits mobile-first
You can write the same breakpoints with max-width. That approach is desktop-first, and the stylesheet ends up shaped differently.
| Aspect | Mobile-first (`min-width`) | Desktop-first (`max-width`) |
|---|---|---|
| Base rules describe | The smallest screen | The largest screen |
| Queries add | Complexity for more space | Simplifications for less space |
| Phone must process | Few rules | Many rules, then undo some |
| Typical bug | A rule placed in the wrong query | A desktop rule leaks onto a phone |
| Failure if CSS partly fails | Usable single column | Cramped desktop layout |
With max-width, a phone downloads and evaluates desktop rules, such as multi-column grids and fixed widths, and then cancels them. With min-width, the phone never sees them. The base is simple, and anything that fails to apply leaves a usable page.
Choosing units for the query
You can write the width in px, em or rem. Prefer em for breakpoints.
Inside a media query, em and rem both refer to the browser's default font size, usually 16px. Your own html { font-size: ... } rule does not change them. A user who raises their default text size to 24px sees the breakpoint move from 640px to 960px. The layout then switches to columns later, which gives larger text room before it is split into narrow columns.
A breakpoint written in px does not respond to the user's text size. That is the main reason to avoid it.
/* Same breakpoint, two units */
@media (min-width: 40em) { /* 640px at the default size, grows with user text size */ }
@media (min-width: 640px) { /* fixed at 640px */ }Layering queries on one component
A query can change several properties at once, and one component can have several queries. This example reorders a page into a main column and a sidebar, then adds more spacing on large screens.
.page {
display: grid;
gap: 1rem;
padding: 1rem;
}
@media (min-width: 48em) {
.page {
grid-template-columns: 2fr 1fr;
gap: 1.5rem;
}
}
@media (min-width: 64em) {
.page {
padding: 2rem;
max-inline-size: 70rem;
margin-inline: auto;
}
}At 48em the layout becomes two columns and the gap grows. At 64em the container gets more padding and a maximum width and is centred. The 64em query does not repeat grid-template-columns, because the 48em rule is still in effect at that width. Each query builds on the ones below it.
Combining conditions
Use and to require several conditions. This is occasionally useful, for example to apply a rule only on wide, landscape viewports.
@media (min-width: 48em) and (orientation: landscape) {
.hero {
min-block-size: 60vh;
}
}Both conditions must be true. Avoid combining many features, because every extra condition adds a case to test.
Hover and pointer
Width does not tell you whether the user has a mouse. A tablet can be wider than 64em and still be touch-only. To enhance hover effects, test for the capability instead:
@media (hover: hover) {
.card:hover {
border-color: #333;
}
}Keep the touch-friendly sizes from the base styles at every width. A wide screen does not make tap targets smaller.
Common errors
- Missing viewport meta tag. The phone renders the page at a wide layout width, around 980px, so your 40em query matches and the phone shows the multi-column layout shrunk down. Add the viewport tag first.
- Base rules after the queries. As shown above, the later base rule wins and the queries have no effect.
- Mixing
min-widthandmax-widthfor the same breakpoint. Writingmin-width: 40emin one place andmax-width: 40emin another means both match at exactly 40em. Use one direction throughout. - Repeating the whole component in each query. Copying the full rule block into every query makes the stylesheet long and hard to change. Put only the changed properties in each query.
- Using only large
pxvalues. A fixedwidth: 600pxin the base rules overflows a 360px phone. Keep the base fluid, with widths such asmax-inline-size, percentages or grid tracks. - Choosing breakpoints from device lists. Widths such as 375 or 768 match particular devices that are replaced often. The next chapter explains how to find breakpoints from where the content itself breaks.
A quick checklist
- The base rules sit outside any query and look right at 320px wide.
- Every
min-widthquery comes after the rules it overrides. - Queries run from smallest to largest.
- Each query contains only properties that change.
- Breakpoints use
em. - Hover effects sit inside
@media (hover: hover).
Exercise
A page has this CSS, and the viewport is 700px wide at the default 16px font size. How many columns does .list show, and why? Assume .list has display: grid.
.list { grid-template-columns: repeat(3, 1fr); }
@media (min-width: 40em) {
.list { grid-template-columns: repeat(2, 1fr); }
}
Show answer
It shows two columns. At 700px the query matches (40em is 640px), and its rule has the same specificity as the base rule but appears later, so it wins. The CSS is also in the wrong mobile-first shape: the base rule describes three columns, so a 320px phone gets three cramped columns. The base should be a single column, with the multi-column rules inside queries.
Exercise
Write mobile-first CSS for a .gallery grid that shows one column by default, two columns from 36em and four columns from 60em. Use gap: 0.5rem in the base rules and do not repeat it in the queries.
Show answer
.gallery { display: grid; gap: 0.5rem; } @media (min-width: 36em) { .gallery { grid-template-columns: repeat(2, 1fr); } } @media (min-width: 60em) { .gallery { grid-template-columns: repeat(4, 1fr); } }