CSS Exercises: Advanced (Modern Layout, Motion, Container Queries and Architecture)
Eleven advanced CSS exercises with target screenshots and Chrome-checked answers: subgrid, container query units, :has() validation, anchor positioning, scroll snap, reduced motion, cascade layers, @scope, light-dark() theming, logical properties and line clamping.
kvi5hnuCode Sprout team member13 min readOn this page
The last exercise set uses the newer parts of CSS and the architecture articles. Like the beginner and intermediate sets, each exercise has a task, a target screenshot, a playground with the starting code, a hint and an explained answer.
These exercises need a current browser. Everything here works in current versions of Chrome, Firefox and Safari, except where an exercise says otherwise. Some targets show two widths, or the result with reduced motion turned on in the operating system, which the playground can't switch for you. Every answer was checked in Chrome, including under emulated reduced motion where it matters.
Modern layout
1. Align card parts across cards with subgrid
The three cards sit in a grid, but their titles have different lengths, so the descriptions and prices don't line up. Make each card span three rows of the outer grid and use subgrid, so that every title, description and price starts at the same height as in the neighbouring cards.

body { font: 14px/1.45 system-ui, sans-serif; margin: 12px; color: #2a2118; }
.cards { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.5rem; }
.card { border: 1px solid #d8cfc0; border-radius: 8px; padding: 0.5rem; }
.card h3 { margin: 0; font-size: 0.95rem; line-height: 1.2; }
.card p { margin: 0; font-size: 0.8rem; color: #6b6259; }
/* your CSS here */
Hint
A subgrid uses its parent's tracks instead of creating its own. The card must first span as many parent rows as it has children.
Answer
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
gap: 0.3rem;
}
grid-row: span 3 makes each card cover three rows of .cards, which creates those rows implicitly. grid-template-rows: subgrid then puts the card's title, description and price into the parent's rows, so the first row is as tall as the longest title across all cards. A subgrid inherits the parent's gaps unless it sets its own, so gap: 0.3rem here overrides the parent's 0.5rem between the rows inside the cards.
2. Type that scales with its container
The same promo component appears in a narrow sidebar and in a wide main area. Make each .slot an inline-size container, and size the heading with container query units: clamp(1rem, 4cqi + 0.5rem, 2.2rem). The heading should then stay small in the sidebar and grow with the main area, on the same page; the difference shows at the wider target.

body { font: 14px/1.45 system-ui, sans-serif; margin: 12px; color: #2a2118; }
.layout { display: grid; grid-template-columns: 100px 1fr; gap: 0.5rem; }
.promo { background: #fbe3d0; border-radius: 8px; padding: 0.5rem; }
.promo h2 { margin: 0; line-height: 1.1; }
.promo p { margin: 0.2rem 0 0; font-size: 0.8rem; }
/* your CSS here */
Hint
1cqi is 1% of the inline size of the nearest query container. Without a container, the units fall back to the small viewport units.
Answer
.slot {
container-type: inline-size;
}
.promo h2 {
font-size: clamp(1rem, 4cqi + 0.5rem, 2.2rem);
}
Each .slot is a container, so cqi in each heading refers to that slot's width, not the viewport's. In Chrome, the 100px slot gives 4 + 8 = 12px, clamped up to 16px; the 218px slot gives 8.72 + 8 = 16.72px. At a 700px viewport, the wide slot is 568px and the heading 30.72px, and it keeps growing up to the 35.2px cap. A media query couldn't do this, because both headings are on the same page at the same viewport width.
3. Anchor a tooltip to its button
Position the tooltip centered below the button using CSS anchor positioning, without a wrapper or position: relative, with a 6px gap. If there isn't room below, it should flip above. The button already has an anchor-name.

body { font: 14px/1.45 system-ui, sans-serif; margin: 12px; color: #2a2118; }
body { padding: 0.5rem 4rem 3rem; }
.help { font: inherit; padding: 0.35rem 0.8rem; anchor-name: --help; }
.tip { background: #1f2328; color: white; font-size: 0.8rem; padding: 0.3rem 0.5rem; border-radius: 5px; width: max-content; max-width: 12rem; }
.text { margin-top: 4.5rem; color: #6b6259; }
/* your CSS here */
Hint
position-area places an absolutely positioned element in a 3 × 3 grid around its anchor. flip-block mirrors the area when it overflows.
Answer
.tip {
position: absolute;
position-anchor: --help;
position-area: bottom center;
margin-top: 6px;
position-try-fallbacks: flip-block;
}
position-anchor links the tooltip to the element named --help, wherever it is in the DOM. position-area: bottom center puts it in the cell below the anchor, centered on it, and the margin adds the gap. position-try-fallbacks: flip-block tries the mirrored area, above the button, when the tooltip would overflow the bottom of its containing block. Chrome reports the tooltip's center within a pixel of the button's center, 6px below it. Anchor positioning is in Chrome, Edge and Safari, and in Firefox from version 147 (article 59).
4. A snapping horizontal carousel
Turn the list into a horizontal carousel: slides are 80% of the scroller's width so the next one peeks in, 0.6rem apart, and scrolling always snaps a slide to the start edge with 1rem of inset. Hide nothing: the scrollbar may stay.

body { font: 14px/1.45 system-ui, sans-serif; margin: 12px; color: #2a2118; }
.carousel { list-style: none; margin: 0; padding: 0.5rem 0; }
.carousel li { background: var(--c); color: white; font-weight: 700; border-radius: 10px; height: 7rem; display: grid; place-items: center; }
/* your CSS here */
Hint
A grid with grid-auto-flow: column puts each new item in a new column. Snapping is configured on the scroller and on each item.
Answer
.carousel {
display: grid;
grid-auto-flow: column;
grid-auto-columns: 80%;
gap: 0.6rem;
overflow-x: auto;
scroll-snap-type: x mandatory;
scroll-padding-inline: 1rem;
padding-inline: 1rem;
}
.carousel li {
scroll-snap-align: start;
}
grid-auto-columns: 80% sizes every column relative to the scroller's content box, inside its padding, so part of the next slide is always visible as a cue that the row scrolls. scroll-snap-type: x mandatory on the scroller and scroll-snap-align: start on the slides make every scroll end with a slide aligned to the start, and scroll-padding-inline moves that alignment line 1rem inward, matching the padding. Article 115 adds buttons and markers with ::scroll-button() and ::scroll-marker.
Interaction and motion
5. Flag a field only when it has a wrong value
When the email input contains an invalid value, give its .field a #c62828 left border and show the .error message. Don't flag an empty field that hasn't been filled in yet: the placeholder is showing then. The target shows the second field, which contains "ana@".

body { font: 14px/1.45 system-ui, sans-serif; margin: 12px; color: #2a2118; }
.field { display: grid; gap: 0.2rem; padding: 0.3rem 0 0.3rem 0.6rem; border-left: 4px solid transparent; margin-bottom: 0.6rem; }
label { font-weight: 600; }
input { font: inherit; padding: 0.3rem 0.4rem; border: 1px solid #b9ad9c; border-radius: 5px; }
.error { display: none; margin: 0; color: #c62828; font-size: 0.8rem; }
/* your CSS here */
Hint
:invalid alone matches as soon as the value is wrong, but an empty optional email field is valid. What tells you the user has typed something?
Answer
.field:has(input:invalid:not(:placeholder-shown)) {
border-left-color: #c62828;
}
.field:has(input:invalid:not(:placeholder-shown)) .error {
display: block;
}
:has() lets the field react to the state of its input. :not(:placeholder-shown) excludes empty fields, which show their placeholder. In real forms, :user-invalid is often better still: it matches only after the user has interacted with the field, so a field with a server-filled wrong value isn't flagged before the user touches it. It isn't used here because the target needs to show the state without interaction.
6. A loading skeleton that respects reduced motion
Animate the skeleton lines with a shimmer: a lighter band moving across them every 1.4s, forever. When the user has asked for reduced motion, don't animate; instead show the lines at a constant, slightly lighter color, #e9e4dc. The target shows both states, the animated one as a single frame.

body { font: 14px/1.45 system-ui, sans-serif; margin: 12px; color: #2a2118; }
.skeleton { display: flex; gap: 0.6rem; padding: 0.7rem; border: 1px solid #e0d8cb; border-radius: 10px; }
.dot { width: 40px; height: 40px; border-radius: 50%; background: #ddd5c9; flex: none; }
.lines { flex: 1; display: grid; gap: 0.4rem; }
.line { height: 0.7rem; border-radius: 4px; background: #ddd5c9; }
.short { width: 60%; }
/* your CSS here */
Hint
Make the gradient three times wider than the line, then animate background-position so the band slides through. The media query overrides both the animation and the background.
Answer
.line {
background: linear-gradient(100deg, #ddd5c9 40%, #f4efe8 50%, #ddd5c9 60%) 0 0 / 300% 100%;
animation: shimmer 1.4s linear infinite;
}
@keyframes shimmer {
from { background-position: 100% 0; }
to { background-position: 0 0; }
}
@media (prefers-reduced-motion: reduce) {
.line {
animation: none;
background: #e9e4dc;
}
}
The gradient is 300% of the line's width, so moving background-position from 100% to 0 slides the light band across the visible part. In the reduced-motion block, animation: none stops it, and a flat color replaces the gradient, because a frozen band would look like a glitch. Chrome reports animation-name: shimmer normally and none under emulated reduced motion. The target's animated frame was paused 0.6s into the cycle.
Architecture and theming
7. Put a stylesheet into cascade layers
The utility .mt-0 should remove the card heading's top margin, and .text-accent should color it, but the component rule wins. Without changing any selector, wrap the three groups of rules in cascade layers reset, components and utilities, declared in that order, so that utilities beat components and components beat the reset.

body { font: 14px/1.45 system-ui, sans-serif; margin: 12px; color: #2a2118; }
/* reset */
h2 { margin: 0; }
/* components */
.card { border: 1px solid #d8cfc0; border-radius: 8px; padding: 0.8rem; }
.card .card__title { margin-top: 1.5rem; color: #1d3557; }
/* utilities */
.mt-0 { margin-top: 0; }
.text-accent { color: #c2410c; }
Hint
Declare the layer order once at the top with @layer a, b, c;. For normal declarations, later layers win regardless of specificity.
Answer
@layer reset, components, utilities;
@layer reset {
h2 { margin: 0; }
}
@layer components {
.card { border: 1px solid #d8cfc0; border-radius: 8px; padding: 0.8rem; }
.card .card__title { margin-top: 1.5rem; color: #1d3557; }
}
@layer utilities {
.mt-0 { margin-top: 0; }
.text-accent { color: #c2410c; }
}
Unlayered, .card .card__title (0,2,0) beats .mt-0 (0,1,0). In layers, the cascade compares layer order first: utilities is declared last, so its rules win over components whatever their specificity, and the heading's margin computes to 0px. The body rule stays unlayered on purpose, which is fine because it doesn't conflict with anything here; remember, though, that unlayered styles beat every layer (article 117).
8. Scope card styles so they stop at a nested ad
Style the paragraphs and links inside cards (color: #57606a and color: #8a3b12 respectively), but not inside the .ad block nested in the second card, which has its own styles. Use @scope with a lower boundary instead of :not() chains.

body { font: 14px/1.45 system-ui, sans-serif; margin: 12px; color: #2a2118; }
.card { border: 1px solid #d8cfc0; border-radius: 8px; padding: 0.6rem; margin-bottom: 0.5rem; }
.card p { margin: 0; }
.ad { background: #1d4ed8; color: white; border-radius: 6px; padding: 0.4rem; margin-top: 0.4rem; }
.ad a { color: white; }
/* your CSS here */
Hint
@scope (root) to (limit) matches elements inside the root but stops at the limit, which is excluded along with its contents.
Answer
@scope (.card) to (.ad) {
p { color: #57606a; }
a { color: #8a3b12; }
}
Inside the @scope block, p and a only match between a .card and any .ad below it. The ad's paragraph keeps inheriting white from .ad, and its link keeps the .ad a rule. Without the boundary, the scoped p rule would match the ad's paragraph directly and beat the white it inherits from .ad, turning it gray. The link would stay white only because .ad a (0,1,1) is more specific than the scoped a (0,0,1); scope proximity is compared only after specificity. @scope is in every current engine; Firefox added it in version 146 (article 22).
9. Light and dark themes in one declaration each
The page is light, and the .promo section should be dark. Set color-scheme: light on :root and color-scheme: dark on .promo, and define each token once with light-dark(): surface #ffffff / #1f2328, text #2a2118 / #f0ebe4, accent #8a3b12 / #f4a261. Use the tokens in .box and the link.

body { font: 14px/1.45 system-ui, sans-serif; margin: 12px; color: #2a2118; }
.box { border: 1px solid #d8cfc0; border-radius: 8px; padding: 0.6rem; margin-bottom: 0.5rem; }
.box p { margin: 0.2rem 0 0; }
/* your CSS here */
Hint
light-dark() picks its first or second value depending on the used color scheme of the element it ends up on, not where the custom property is defined.
Answer
:root {
color-scheme: light;
--surface: light-dark(#ffffff, #1f2328);
--text: light-dark(#2a2118, #f0ebe4);
--accent: light-dark(#8a3b12, #f4a261);
}
.promo {
color-scheme: dark;
}
.box {
background: var(--surface);
color: var(--text);
}
.box a {
color: var(--accent);
}
The custom properties store the light-dark() function unresolved, and it's evaluated on each element that uses it. Inside .promo, color-scheme: dark makes the same tokens produce the dark values, so one set of definitions themes any subtree. Chrome computes the second box's background as rgb(31, 35, 40). Unlike the derived-token trap (article 118), this works because colors are resolved at the point of use.
10. A component that mirrors itself in right-to-left text
Rewrite the quote's CSS with logical properties, so that in the Arabic version (dir="rtl") the accent border and the larger padding move to the right, with no extra rules for RTL.

body { font: 14px/1.45 system-ui, sans-serif; margin: 12px; color: #2a2118; }
.quote {
margin: 0 0 0.6rem;
padding: 0.5rem 0.6rem 0.5rem 1rem;
border-left: 5px solid #e76f51;
background: #fbf3ea;
border-radius: 0 8px 8px 0;
text-align: left;
}
Hint
Replace left with inline-start and right with inline-end. Corner radii have logical names too, such as border-start-end-radius.
Answer
.quote {
margin: 0 0 0.6rem;
padding-block: 0.5rem;
padding-inline: 1rem 0.6rem;
border-inline-start: 5px solid #e76f51;
background: #fbf3ea;
border-start-end-radius: 8px;
border-end-end-radius: 8px;
text-align: start;
}
Logical properties follow the writing direction: inline-start is the left in English and the right in Arabic. padding-inline: 1rem 0.6rem sets start and end in one declaration, and text-align: start aligns to the start side. The corner names combine a block side and an inline side, so border-start-end-radius is the top-right corner in LTR and the top-left in RTL. Chrome reports border-right-width: 5px for the Arabic quote.
11. Balanced headlines and clamped descriptions
Make the headlines wrap into lines of similar length instead of leaving a single word on the last line, and cut each description off after two lines with an ellipsis. Cards must keep their full text in the HTML.

body { font: 14px/1.45 system-ui, sans-serif; margin: 12px; color: #2a2118; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 0.5rem; }
.card { border: 1px solid #d8cfc0; border-radius: 8px; padding: 0.5rem; }
.card h3 { margin: 0 0 0.3rem; font-size: 0.95rem; line-height: 1.25; }
.card p { margin: 0; color: #6b6259; font-size: 0.8rem; }
/* your CSS here */
Hint
One property balances line lengths. Clamping to a number of lines still uses an old prefixed syntax that works in every browser.
Answer
.card h3 {
text-wrap: balance;
}
.card p {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
overflow: hidden;
}
text-wrap: balance makes the browser choose break points that even out line lengths, which suits short text such as headings; browsers limit it to a few lines for performance. Line clamping needs display: -webkit-box, -webkit-box-orient: vertical, -webkit-line-clamp and overflow: hidden together. The standard line-clamp property is being specified to replace them. The full text stays in the DOM, so search and screen readers still get it. In Chrome, each paragraph is exactly two lines tall.
Common questions
My answer works in Chrome but not in another browser. Is it wrong?
Check the support notes in the linked articles. Anchor positioning, @scope and a few others arrived in the last browser releases, so older versions need fallbacks, for example with @supports (article 80).
Why check reduced motion in an exercise about a loading skeleton? Continuously moving content is among the triggers for people with vestibular disorders, and loading indicators often stay on screen for a long time. Article 112 covers motion preferences in detail.
Summary
subgridaligns parts across sibling components; container query units size content to its container.:has()with validation pseudo-classes styles fields from their state; anchor positioning places popups without wrappers.- Scroll snap builds carousels in CSS; reduced-motion queries should replace motion, not just stop it.
- Layers,
@scope,light-dark()tokens and logical properties make components that hold up in any context, theme and direction.
Practice questions
In exercise 1, what happens if a card has four children but still spans three rows?
Answer
A subgrid can't create implicit tracks in its subgridded axis, so the fourth child is placed in the last of the three rows and overlaps the third child. Span as many rows as there are children.
In exercise 9, would --surface: #ffffff on :root plus --surface: #1f2328 on .promo work too? What's the advantage of light-dark()?
Answer
Yes, it works, but then every token needs two definitions, and each dark subtree must redefine all of them. With light-dark(), each token is defined once, and any element that sets color-scheme: dark, including in response to the user's system setting, gets the dark values automatically.
What's next
Finished the course? Test everything with the CSS final exam, practise for job interviews with CSS interview questions, and check the edge cases in CSS delicate details.
Previous: CSS Exercises: Intermediate (Flexbox, Grid, Positioning and Responsive Design)
Next: CSS Course Outline: From Selectors to Modern Layout and Motion

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.