Explain it simply
Explain it simply
This lesson is a step up from the first exercise set. Instead of one layout system, you combine CSS Grid, positioning, custom properties (variables), pseudo-classes and pseudo-elements, and a responsive breakpoint at once. It simulates a situation you will meet in real projects: "make a product grid with badges responsive".
The tasks build on each other, so doing them in order makes everything easier, much like cooking, where the oil has to be hot before the onions go in. If you can finish this exercise, you can be confident that you can apply most concepts from the Intermediate and Advanced chapters on your own.
Problem → Why → Solution
The problem: all six badges pile up in the page corner
A learner wrote Task 2 as below. Instead of a "NEW" badge in the top-right corner of each card, all six badges are stacked on top of each other in the top-right corner of the browser window, so it looks as if there is only one.
The learner tries different top and right values, but the badges just move together. The bug is not in the badge's properties at all; it is a line missing from the card.
.grid-item {
padding: 20px; /* no position here */
background: #f3f4f6;
}
.grid-item::before {
content: "NEW";
position: absolute;
top: 8px;
right: 8px;
}What is absolute measured from?
The top and right of a position: absolute element are measured from its containing block: the nearest ancestor whose position is not static. With no such ancestor, the initial containing block (the size of the page's first screen) is used, so every badge lands in the same page corner. Adding position: relative to .grid-item makes each card the containing block for its own badge.
Core concept: the four tasks and what to predict
Here are the four original tasks, each with a prediction to make. Read them and settle on your own prediction before you write any code. Check each task in the browser as soon as it is done, before moving on to the next.
Task 1: tokens and the grid
Declare --gap: 16px and --primary: #2563eb in :root, then give .grid display: grid, grid-template-columns: repeat(3, 1fr) and gap: var(--gap). Prediction: the three 1fr tracks share the space left after the two gaps, so in a 1000px container each column is (1000 - 32) / 3 ≈ 322.67px.
Task 2: a ::before badge
Give .grid-item position: relative, then place a "NEW" badge in the top-right corner with ::before, position: absolute, top/right and background: var(--primary). Prediction: without the content property, the pseudo-element does not render at all, and because it is absolute it floats over the card's text instead of pushing it.
Task 3: nth-child(odd)
Use .grid-item:nth-child(odd) to give odd items a different background. Prediction: with six items, numbers 1, 3 and 5 change colour. :nth-child counts every sibling, so if non-article elements were mixed into the grid, the counting would shift.
Task 4: the responsive breakpoint
The original task switches to one column with @media (max-width: 600px). The worked solution rewrites that desktop-first rule mobile-first: one column at the base and three at @media (min-width: 600px). Prediction: the result is the same except at exactly 600px.
Pick one style
There are two ways to write media queries, mobile-first with min-width and desktop-first with max-width. Pick one per project and stay consistent instead of mixing them.
- Tokens: collect the values
- Grid: build the columns
- Position: pin badge inside card
- Selector: pick the odd items
- Query: change columns by width
Each layer does one job. You never need to look in the grid for a badge bug, or in ::before for a column bug.
General pattern
:root { --space: VALUE; --brand: COLOR; }
.list { display: grid; grid-template-columns: 1fr; gap: var(--space); }
@media (min-width: BREAKPOINT) { .list { grid-template-columns: repeat(N, 1fr); } }
.item { position: relative; }
.item::before { content: "LABEL"; position: absolute; top: OFFSET; right: OFFSET; }
.item:nth-child(odd) { background: ALT-COLOR; }Tokens, a mobile-first grid, the relative/absolute pair for a badge, a pseudo-element and a structural pseudo-class, all in one place.
Worked solution for Tasks 1-4 (mobile-first)
Open this solution only after trying the tasks yourself. There are six products, so a wide screen shows two rows of three. Run it and resize the preview around 600px to check within five minutes that the breakpoint really works.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Product grid</title>
<style>
/* Task 1: design tokens */
:root {
--gap: 16px;
--primary: #2563eb;
}
body { margin: 0; padding: var(--gap); font-family: system-ui, sans-serif; }
/* Task 1 + Task 4: one column first, three columns from 600px */
.grid {
display: grid;
grid-template-columns: 1fr;
gap: var(--gap);
}
@media (min-width: 600px) {
.grid { grid-template-columns: repeat(3, 1fr); }
}
.grid-item {
position: relative;
padding: 20px;
border-radius: 8px;
background: #f3f4f6;
}
/* Task 2: a badge pinned to each item's corner */
.grid-item::before {
content: "NEW";
position: absolute;
top: 8px;
right: 8px;
padding: 2px 8px;
border-radius: 999px;
background: var(--primary);
color: #fff;
font-size: 0.75rem;
font-weight: 700;
}
/* Task 3: alternate background on odd items */
.grid-item:nth-child(odd) { background: #e0e7ff; }
</style>
</head>
<body>
<div class="grid">
<article class="grid-item">Keyboard</article>
<article class="grid-item">Mouse</article>
<article class="grid-item">Monitor</article>
<article class="grid-item">Headset</article>
<article class="grid-item">Webcam</article>
<article class="grid-item">Desk lamp</article>
</div>
</body>
</html>Code breakdown
- Tokens
--primary: #2563eb;The badge colour lives in a token, so changing the brand colour means editing this one line.
- Mobile-first Task 4
@media (min-width: 600px) {The base is one column, switching to three from 600px. With the original max-width: 600px, exactly 600px gave one column; here it gives three. That single pixel is the only difference.
- Containing block
position: relative;Makes the card the containing block for absolute children without moving the card itself. This is the line that fixes the problem section's bug.
- The badge
content: "NEW";This line makes ::before exist. The badge is decorative content from CSS: some screen readers announce it, but it cannot be selected and copied. If the information matters, put it in the HTML.
- Zebra items
.grid-item:nth-child(odd) { background: #e0e7ff; }Its specificity (0,2,0) is higher than .grid-item at (0,1,0), so it overrides the #f3f4f6 background on the odd items.
Up to 599px: the six items are stacked.
From 600px: two rows of three.
Every item has a blue "NEW" badge in its top-right corner; items 1, 3 and 5 are light indigo, the others light grey.Bonus tasks
Once the four tasks work, challenge yourself with these extras. Each one reuses a concept from an earlier chapter.
- Replace repeat(3, 1fr) and the media query with a single repeat(auto-fit, minmax(180px, 1fr)) and compare
- Lift an item on hover with transform: translateY(-4px), and switch it off under prefers-reduced-motion
- Mark one item .is-sold-out and change its badge text with content: "SOLD OUT"
- Override --primary with a lighter colour inside prefers-color-scheme: dark
In real projects
In real projects
"New" and "Sale -20%" badges in online shops, red count bubbles on notification icons and bookmark buttons in card corners are all built with the relative/absolute pattern. Before shipping a product listing or a dashboard, many teams test at 320px, at 768px, on a wide screen, at 200% browser zoom, keyboard-only and with reduced motion, and log every issue they find.
Grid's power to reorder items (order, grid-row, grid-area) needs care in practice. When the on-screen order differs from the HTML order, the Tab key and screen readers still follow the HTML, which confuses users. Experienced developers therefore get the HTML source order right first and use CSS only for the visual layout.
Common mistakes
Common mistakes
Forgetting content on ::before
You can write the background, padding and position for a badge, but without content the ::before is never generated, and DevTools shows no ::before node. Even a purely decorative shape with no text needs content: "".
Mixing max-width and min-width queries
Writing Task 4 with max-width and then adding the next breakpoint with min-width makes it hard to work out which rule wins at which width, and two queries often overlap at a boundary. Use mobile-first min-width throughout the project.
Reordering visually without checking DOM order
Giving a featured product order: -1 to show it first puts it first on screen, but the Tab key still reaches it last, following the HTML. Whenever possible, move it in the HTML instead.
Setting grid-area outside the media query
With grid-template-areas inside the 768px query but grid-area: header/nav/main in the base styles, the phone layout points at areas that do not exist. The browser creates implicit grid lines for those names and stacks all three elements in the same cell, on top of each other. Keep the area template and the grid-area declarations together in one query.
Mini challenge
Dashboard layout challenge
Build a dashboard with a header, a sidebar nav and a main area of stat cards. On phones everything stacks; from 768px, grid-template-areas puts the sidebar on the left. One "live" stat card gets an ::after badge.
- Keep --gap and --primary tokens in :root
- The header uses position: sticky; top: 0 and stays at the top while scrolling
- From 768px: grid-template-areas "header header" "nav main" with 200px 1fr columns
- The stat cards form a repeat(auto-fit, minmax(160px, 1fr)) grid
- .stat--live::after shows a "LIVE" badge in the card's top-right corner
/* Markup: .app > header.top, nav.side, main.content > div.stats > div.stat*4
(the second stat also has .stat--live) */
:root {
/* TODO: --gap, --primary */
}
.app {
/* TODO: grid, gap; single column by default */
}
.top {
/* TODO: stick to the top while the page scrolls */
}
.stats {
/* TODO: auto-fit cards, at least 160px each */
}
.stat {
/* TODO: become the containing block for the badge */
}
/* TODO: .stat--live::after "LIVE" badge in the top-right corner */
/* TODO: from 768px, grid-template-areas with nav on the left (200px) */Expected: At 375px the header, nav and main stack and the stat cards sit two per row. At 1024px the header spans the full width, the nav is a 200px column on the left and the four stat cards fill one row in main. The second card has a "LIVE" badge in its corner.
Show hint
Write both the grid-template-areas on .app and each element's grid-area (header, nav, main) inside the media query. Left outside it, grid-area points at areas the phone layout never defines, and the three elements end up overlapping. The HTML order (header, nav, main) matches the visual order, so keyboard users are not confused. You will only see the sticky header stick once there is enough content to scroll.
Show solution
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
:root { --gap: 16px; --primary: #2563eb; }
* { box-sizing: border-box; }
body { margin: 0; font-family: system-ui, sans-serif; }
.app { display: grid; gap: var(--gap); padding: var(--gap); }
.top {
position: sticky;
top: 0;
padding: 12px var(--gap);
background: #0f172a;
color: #fff;
}
.side { display: flex; flex-direction: column; gap: 8px; }
.stats {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
gap: var(--gap);
}
.stat { position: relative; padding: 20px; border-radius: 8px; background: #f1f5f9; }
.stat--live::after {
content: "LIVE";
position: absolute;
top: 8px;
right: 8px;
padding: 2px 6px;
border-radius: 4px;
background: var(--primary);
color: #fff;
font-size: 0.7rem;
}
@media (min-width: 768px) {
.app {
grid-template-columns: 200px 1fr;
grid-template-areas: "header header" "nav main";
}
.top { grid-area: header; }
.side { grid-area: nav; }
.content { grid-area: main; }
}
</style>
</head>
<body>
<div class="app">
<header class="top">Shop dashboard</header>
<nav class="side"><a href="#">Overview</a><a href="#">Orders</a></nav>
<main class="content">
<div class="stats">
<div class="stat">Orders: 128</div>
<div class="stat stat--live">Visitors: 42</div>
<div class="stat">Revenue: $3,410</div>
<div class="stat">Refunds: 3</div>
</div>
</main>
</div>
</body>
</html>Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- Declare tokens in :root and reuse them with var() for the grid gap and badge colour
- An absolute badge needs position: relative on its parent and content on ::before
- :nth-child(odd) counts from 1 and selects items 1, 3 and 5
- max-width and min-width versions match except at the boundary pixel, so use one style per project
- Keep the HTML source order meaningful and use CSS only for the visual layout
CSS reference — MDN Web Docs