CSS Exercises: Beginner (Selectors, Cascade, Box Model and Color)
Fourteen beginner CSS exercises with a target screenshot, a starting playground, a hint and an explained answer each: selectors and :has(), the cascade and layers, finding invalid declarations, inheritance, box sizing, margin collapsing, color-mix() and text.
kvi5hnuCode Sprout team member15 min readOn this page
This is the first of three graded exercise sets. Each exercise gives you a task, a screenshot of the result to aim for, and a playground with the starting HTML and CSS. Hints and explained answers are hidden until you open them.
| Set | Covers | Articles |
|---|---|---|
| Beginner (this one) | selectors, the cascade and specificity, inheritance, the box model, margins, color, text and units | 1–48 |
| Intermediate | flexbox, grid, positioning, responsive design | 49–80 |
| Advanced | modern layout, container queries, motion, architecture | 75–122 |
How to use these exercises
- Read the task and look at the target screenshot. Each one was rendered in Chrome from the answer, 350px wide like the playground preview.
- Edit the CSS in the playground until the preview matches. Change only the CSS unless the task says otherwise.
- Open the hint if you're stuck, and the answer once your preview matches or after a real attempt.
Your CSS doesn't need to match the answer exactly. If the preview matches the target and you followed the task's rules, it's correct. Every answer was checked in Chrome: the computed values described in each explanation are what the browser actually reports.
Selectors
1. Style only the navigation links
The page has links in a navigation bar and in the text. Make only the navigation links white, without an underline, and with 0.3rem 0.6rem of padding. When hovered, they should get a #a84a1c background. Don't add classes to the HTML.

body { font: 15px/1.5 system-ui, sans-serif; margin: 12px; color: #2a2118; }
.site-nav { background: #5b2a0e; padding: 0.4rem; border-radius: 8px; }
/* your CSS here */
Hint
A descendant selector, .site-nav a, matches links anywhere inside the navigation and nowhere else.
Answer
.site-nav a {
color: white;
text-decoration: none;
padding: 0.3rem 0.6rem;
border-radius: 6px;
}
.site-nav a:hover {
background: #a84a1c;
}
.site-nav a matches only links inside the nav, so the paragraph's link keeps the browser's default blue and underline. The :hover rule repeats the same descendant selector with a pseudo-class added. The padding makes each link a larger target; links stay inline, which is fine here because the nav's own padding leaves room for it.
2. Zebra stripes and a header row
Style the table so that:
- the header row has a
#1d3557background and white text, aligned left; - every second body row has a
#f1ece4background; - cells have
0.4rem 0.6rempadding and the table has no gaps between cells.
Use structural pseudo-classes, not classes.

body { font: 15px/1.5 system-ui, sans-serif; margin: 12px; color: #2a2118; }
table { width: 100%; }
/* your CSS here */
Hint
:nth-child(even) counts siblings, so put it on tbody tr to count only body rows. Gaps between cells come from border-spacing, which border-collapse: collapse removes.
Answer
table { width: 100%; border-collapse: collapse; }
th, td { padding: 0.4rem 0.6rem; text-align: left; }
thead th { background: #1d3557; color: white; }
tbody tr:nth-child(even) { background: #f1ece4; }
tbody tr:nth-child(even) picks the second and fourth body rows. Writing tr:nth-child(even) alone would also work here, because the header row is the only row in thead and is counted separately, but scoping it to tbody makes the intent clear. border-collapse: collapse removes the 2px default spacing between cells.
3. Label links by file type
Some links point to PDF files. Using attribute selectors only, add the text (PDF) after every link whose href ends in .pdf, in a smaller, gray font (0.8em, #6b6259). Links to other files must not change.

body { font: 15px/1.5 system-ui, sans-serif; margin: 12px; color: #2a2118; }
/* your CSS here */
Hint
[href$="…"] matches values that end with a string. Notice that one file name is in capitals: attribute selectors can be made case-insensitive.
Answer
a[href$=".pdf" i]::after {
content: " (PDF)";
font-size: 0.8em;
color: #6b6259;
}
$= matches the end of the attribute value, and the i flag makes the comparison case-insensitive, so visa-guide.PDF matches too. Without i, the third link would get no label. The text is generated with ::after. Most browsers include it in the link's accessible name, but it can't be selected or copied, and it disappears if the CSS fails to load; for important information, put it in the HTML instead.
4. Highlight the selected option with ()
Each plan is a label containing a radio button. Give the label of the checked option a 2px solid #8a3b12 border and a #fbf3ea background. Other labels keep a 2px solid #d8cfc0 border. Change only the CSS; the target shows the starting state, with Standard checked.

body { font: 15px/1.5 system-ui, sans-serif; margin: 12px; color: #2a2118; }
fieldset { border: 0; padding: 0; display: grid; gap: 0.5rem; }
legend { font-weight: 700; margin-bottom: 0.4rem; }
.plan { display: block; padding: 0.6rem 0.8rem; border-radius: 8px; border: 2px solid #d8cfc0; }
/* your CSS here */
Hint
A selector can't go "up" from the radio button to its label, but :has() lets the label match based on what it contains.
Answer
.plan:has(:checked) {
border-color: #8a3b12;
background: #fbf3ea;
}
.plan:has(:checked) matches a label that contains a checked input. Because the base rule already sets the border style and width, the answer only changes border-color. Clicking another plan in the playground moves the highlight, with no JavaScript.
Cascade, specificity and inheritance
5. Make the button win without !important
The button labeled Book should have a white label on a #8a3b12 background, but inside the sidebar it turns blue. Don't change the HTML, the #sidebar a rule, or use !important. Change the CSS so that .button wins everywhere.

body { font: 15px/1.5 system-ui, sans-serif; margin: 12px; color: #2a2118; }
#sidebar { background: #f4efe6; padding: 0.8rem; border-radius: 8px; display: flex; gap: 1rem; align-items: center; }
#sidebar a { color: #1d4ed8; }
.button { color: white; background: #8a3b12; padding: 0.4rem 0.9rem; border-radius: 6px; text-decoration: none; }
Hint
#sidebar a has specificity 1,0,1 and .button only 0,1,0. Either raise the button's specificity, or use something the cascade compares before specificity.
Answer
@layer base, components;
@layer base {
#sidebar a { color: #1d4ed8; }
}
@layer components {
.button { color: white; background: #8a3b12; padding: 0.4rem 0.9rem; border-radius: 6px; text-decoration: none; }
}
Cascade layers are compared before specificity, so .button in the later components layer beats the ID selector in base. Moving the rule into a layer counts as changing its position, not the rule itself. Other valid answers: #sidebar .button (1,1,0, which escalates specificity), or rewriting the link rule as :where(#sidebar) a (0,0,1) if you're allowed to touch it. The layer solution scales best, since components never have to know where they're placed.
6. Find the three bugs
The card should have a 1rem padding, a #2f7d32 heading and a 10px corner radius, but none of these work. The CSS contains three bugs. Fix them without adding new rules. Use DevTools in your browser, or read carefully.

body { font: 15px/1.5 system-ui, sans-serif; margin: 12px; color: #2a2118; }
.card {
border: 1px solid #d8cfc0;
padding: 16;
border-raduis: 10px;
}
.card-title { margin: 0 0 0.3rem color: #2f7d32; }
.card p { margin: 0; color: #6b6259; }
Hint
Browsers silently drop declarations they can't parse. Look for a length without a unit, a misspelled property, and a declaration that runs into the next one.
Answer
.card {
border: 1px solid #d8cfc0;
padding: 1rem; /* 16 without a unit is invalid */
border-radius: 10px; /* the property name was misspelled */
}
.card-title { margin: 0 0 0.3rem; color: #2f7d32; } /* missing semicolon */
Each bug makes the browser drop a declaration, and nothing else on the page breaks:
padding: 16is invalid because only0may omit its unit.border-raduisis an unknown property.- Without the semicolon,
margin: 0 0 0.3rem color: #2f7d32is parsed as onemargindeclaration with an invalid value, so both the margin and the color are lost.
In Chrome DevTools, all three are struck through with a warning icon: "Invalid property value" for the two bad values and "Unknown property name" for the misspelling.
7. Make form controls use the page font
The page font is set on body, but the input and button still use the browser's small default font. Make all form controls inherit the font of their parent, with one short rule. The button should also inherit the text color.

body { font: 15px/1.5 system-ui, sans-serif; margin: 12px; color: #2a2118; }
.search { display: grid; grid-template-columns: 1fr auto; gap: 0.4rem; }
.search label { grid-column: 1 / -1; font-weight: 700; }
input, button { padding: 0.4rem 0.6rem; border: 1px solid #b9ad9c; border-radius: 6px; background: white; }
/* your CSS here */
Hint
Form controls don't inherit font by default: the browser's own stylesheet sets their font explicitly. The keyword inherit overrides that.
Answer
input, button, select, textarea {
font: inherit;
color: inherit;
}
User-agent styles give controls their own font, around 13.33px in Chrome, so the body's font doesn't reach them. font: inherit sets every font longhand to the parent's value; Chrome reports the input's font-size as 15px afterward. Most CSS resets include this rule. color: inherit matters mainly for buttons, whose default text color is the system's ButtonText.
The box model and spacing
8. Two columns that actually fit
The two cards should sit side by side, each exactly half the width of the container, but the second one wraps to a new line. Fix it without changing the widths, padding or borders.

body { font: 15px/1.5 system-ui, sans-serif; margin: 12px; color: #2a2118; }
.col {
display: inline-block;
width: 50%;
padding: 0.8rem;
border: 2px solid #8a3b12;
border-radius: 8px;
}
/* your CSS here */
Hint
With the default box sizing, width sets the content box only. How wide is each card really?
Answer
.col {
box-sizing: border-box;
}
By default, box-sizing: content-box adds padding and borders outside the width: each card is 50% + 2 × 0.8rem + 2 × 2px wide, so two don't fit. With border-box, the 50% includes padding and border, so each card is exactly 163px in the 326px-wide body. Many stylesheets apply *, *::before, *::after { box-sizing: border-box; } everywhere. The HTML has no space between the two divs on purpose: inline-block elements are separated by any whitespace between them.
9. Center a readable column
Make the article at most 30ch wide and centered horizontally, with 1rem of padding on each side that stays inside the max width. It should still shrink on narrow screens.

body { font: 15px/1.5 system-ui, sans-serif; margin: 12px; color: #2a2118; }
body { background: #f1ece4; }
.prose { background: white; border-radius: 8px; }
.prose h2 { margin-top: 0; }
.prose p { margin-bottom: 0; }
/* your CSS here */
Hint
max-width limits the width without forcing it. Automatic margins on both inline sides share the remaining space equally.
Answer
.prose {
box-sizing: border-box;
max-width: 30ch;
margin-inline: auto;
padding: 1rem;
}
margin-inline: auto splits the leftover space between the left and right margins, which centers a block box with a width smaller than its container. max-width instead of width means the box can still shrink below 30ch. With border-box, the padding is included in the 30ch; without it, the column would be 2rem wider than you asked for. In Chrome, the left and right margins come out equal.
10. Stop the heading margin from escaping
The card has a beige background, but a gap of white appears above it, and the heading touches the card's top edge. Keep the heading's 1.2rem top margin, and make it push the heading down inside the card instead.

body { font: 15px/1.5 system-ui, sans-serif; margin: 12px; color: #2a2118; }
.before { margin: 0; font-size: 0.8rem; color: #6b6259; }
.card { background: #f1e3d3; border-radius: 8px; padding: 0 1rem; }
.card h2 { margin: 1.2rem 0 0.4rem; }
.card p { margin: 0 0 1rem; }
/* your CSS here */
Hint
This is margin collapsing: the heading's top margin combines with the card's, because nothing separates them. What stops margins from collapsing through a box?
Answer
.card {
display: flow-root;
}
A child's top margin collapses with its parent's when the parent has no top border, padding or other content in between, so the margin ends up outside the card. display: flow-root makes the card a new block formatting context, and margins don't collapse through it. In Chrome, the card's top then sits directly under the "Featured" line, and the heading starts 19.2px below the card's edge. Adding padding-top or a border would also work, but changes the spacing.
11. A pill badge with outline, radius and shadow
Turn the "New" span into a pill badge: 0.15rem 0.6rem padding, fully rounded ends, a #2f7d32 background with white, bold, uppercase text at 0.7rem, letter spacing of 0.05em, and a soft shadow 0 1px 3px rgb(0 0 0 / 0.3). Keep it inline in the heading and vertically centered with the text.

body { font: 15px/1.5 system-ui, sans-serif; margin: 12px; color: #2a2118; }
/* your CSS here */
Hint
A border-radius larger than half the box height makes perfectly round ends, whatever the badge's size. Inline elements align on the baseline unless you change vertical-align.
Answer
.badge {
padding: 0.15rem 0.6rem;
border-radius: 999px;
background: #2f7d32;
color: white;
font-weight: 700;
font-size: 0.7rem;
text-transform: uppercase;
letter-spacing: 0.05em;
box-shadow: 0 1px 3px rgb(0 0 0 / 0.3);
vertical-align: middle;
}
border-radius: 999px is clamped to half the height, so the ends are always semicircles. Percentages would make elliptical corners on a wide box instead. Vertical padding on an inline element doesn't change the line height, but it is painted, which is fine for a badge. vertical-align: middle lines the badge's middle up with the middle of the heading's lowercase letters.
Color, text and units
12. A palette with custom properties and color-mix()
Define three custom properties on :root: --brand: oklch(48% 0.13 45), --surface: oklch(97% 0.01 80) and --text: oklch(25% 0.02 60). Use them for the page and the button, and make the button's hover color 20% black mixed into --brand, using color-mix() in oklab. The target shows the button's normal state.

body { font: 15px/1.5 system-ui, sans-serif; margin: 12px; color: #2a2118; }
.cta { font: inherit; font-weight: 600; border: 0; border-radius: 8px; padding: 0.5rem 1rem; color: white; }
/* your CSS here */
Hint
color-mix(in oklab, A, B 20%) gives 80% of A and 20% of B. Custom properties are read with var().
Answer
:root {
--brand: oklch(48% 0.13 45);
--surface: oklch(97% 0.01 80);
--text: oklch(25% 0.02 60);
}
body { background: var(--surface); color: var(--text); }
.cta { background: var(--brand); }
.cta:hover { background: color-mix(in oklab, var(--brand), black 20%); }
Custom properties keep the palette in one place, and color-mix() derives the hover shade from the brand color, so changing --brand updates both. Mixing in Oklab darkens evenly without shifting the hue the way some sRGB mixes do. Chrome reports the button's background as oklch(0.48 0.13 45). Run your mouse over the button in the playground to see the hover color.
13. Readable text with rem and unitless line height
Set the base font size on html to 100%. Give h1 a size of 1.75rem, paragraphs 1rem, and .note 0.85rem. Set line-height: 1.5 on body so that every element gets 1.5 times its own font size, including .note. Limit the text to 60ch.

body { font-family: system-ui, sans-serif; margin: 12px; color: #2a2118; line-height: 24px; }
h1 { margin: 0 0 0.5rem; }
p { margin: 0 0 0.6rem; }
.note { color: #6b6259; }
/* your CSS here */
Hint
A line height with a unit (24px, or 1.5em) is computed once and inherited as a fixed length. A number without a unit is inherited as a factor.
Answer
html { font-size: 100%; }
body { line-height: 1.5; max-width: 60ch; }
h1 { font-size: 1.75rem; }
p { font-size: 1rem; }
.note { font-size: 0.85rem; }
html { font-size: 100% } keeps the user's browser setting, 16px by default, so every rem respects it. With line-height: 24px, every element inherits 24px, which is too much for .note and too little for the heading. The unitless 1.5 is inherited as a number and multiplied by each element's own size: Chrome computes 13.6px × 1.5 = 20.4px for .note and 28px × 1.5 = 42px for the heading.
14. Colored list markers and a drop cap
Make the list's bullets #c2410c and 1.2em in size, without changing the item text color. Then give the intro paragraph a drop cap: its first letter should be 3.2em, bold, #8a3b12, floated left with a small right margin, so it spans two lines.

body { font: 15px/1.5 system-ui, sans-serif; margin: 12px; color: #2a2118; }
.intro { margin: 0 0 0.6rem; }
.tips { margin: 0; }
/* your CSS here */
Hint
Two pseudo-elements do this: one for list markers and one for the first letter of a block.
Answer
.tips li::marker {
color: #c2410c;
font-size: 1.2em;
}
.intro::first-letter {
float: left;
font-size: 3.2em;
line-height: 1;
font-weight: 700;
color: #8a3b12;
margin-right: 0.08em;
}
::marker accepts only a few properties, including color and font properties, which is exactly what's needed here; the item text keeps its own color. ::first-letter with float: left and a large font size makes the drop cap; line-height: 1 keeps its box from being much taller than the letter. Newer browsers also support initial-letter: 3 for this, which aligns the letter with the lines exactly, but it isn't in every browser yet.
Common questions
My preview looks right, but my CSS is different from the answer. Is it wrong?
Usually not. CSS often has several correct solutions. Check that you followed the task's rules, for example not editing the HTML or not using !important, and that the result also holds at other widths.
How do I compare my result with the target more precisely? Open the playground preview in DevTools and compare computed values, such as widths, colors and line heights, with the ones in the explanation. Every number there was measured in Chrome.
Summary
- Descendant, attribute and structural selectors, plus
:has(), target elements without extra classes. - Cascade layers beat specificity; invalid declarations are dropped silently; form controls need
font: inherit. box-sizing: border-box,margin-inline: autoanddisplay: flow-rootsolve the most common box model surprises.- Custom properties and
color-mix()keep a palette consistent; unitless line heights scale with each element's font size.
Practice questions
In exercise 6, why does one missing semicolon lose two declarations, while a misspelled property loses only one?
Answer
A declaration runs until the next semicolon. Without the semicolon after margin: 0 0 0.3rem, the text color: #2f7d32 becomes part of the margin's value, which is then invalid, so the whole thing is dropped. A misspelled property is still a complete declaration of its own, so only it is ignored.
In exercise 8, how wide is each card with the default content-box sizing?
Answer
50% of 326px is 163px of content, plus 2 × 12.8px padding and 2 × 2px border: 192.6px. Two of those, 385.2px, don't fit in the 326px body.
What's next
The intermediate set covers flexbox, grid, positioning and responsive layout.
Previous: Debugging CSS and Best Practices: A Troubleshooting Guide
Next: CSS Exercises: Intermediate (Flexbox, Grid, Positioning and Responsive Design)

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.