CSS Exercises: Intermediate (Flexbox, Grid, Positioning and Responsive Design)
Fourteen intermediate CSS exercises with target screenshots, often at two widths: flexbox bars, cards and media objects, grid layouts with auto-fit, areas and spanning, positioned badges and tooltips, fluid type and container queries.
kvi5hnuCode Sprout team member15 min readOn this page
The second exercise set moves from styling single elements to laying out groups of them. It works the same way as the beginner set: read the task, compare with the target screenshot, edit the CSS in the playground, and open the hint or answer when you need it.
Several exercises are responsive, so their target shows two widths: 350px, like the playground preview, and 700px. The playground can only show the narrow one, so the wide target tells you what your CSS must also do on a larger screen. To try that width yourself, copy the code into a local file and resize the browser window. Every answer was checked in Chrome at both widths.
The exercises use these articles most: flexbox (61–65), grid (66–72), positioning (56–58) and responsive design (75–81).
Flexbox
1. A header bar with the links pushed right
Lay out the header in one row: the logo on the left, the links on the right, and the button after the links, all vertically centered. Keep 0.8rem between the links. Don't add wrapper elements.

body { font: 14px/1.45 system-ui, sans-serif; margin: 12px; color: #2a2118; }
.bar { background: #1d3557; padding: 0.5rem 0.8rem; border-radius: 8px; }
.bar a { color: white; text-decoration: none; }
.logo { font-size: 1.25rem; font-weight: 800; }
.cta { background: #e76f51; padding: 0.35rem 0.7rem; border-radius: 6px; }
/* your CSS here */
Hint
In a flex container, an auto margin takes all the free space on its side. Which item should have the free space after it?
Answer
.bar {
display: flex;
align-items: center;
gap: 0.8rem;
}
.logo {
margin-inline-end: auto;
}
display: flex puts the four links in a row, align-items: center centers them vertically despite the logo's larger font, and gap spaces them. margin-inline-end: auto on the logo absorbs the free space, pushing everything after it to the right. justify-content: space-between wouldn't work here: it would spread Cities and Deals across the bar too.
2. Equal-height cards with buttons at the bottom
The two cards should be equally tall, side by side with a 0.6rem gap, and each card's button should sit at the bottom of the card, aligned with the other card's button, even though their texts have different lengths.

body { font: 14px/1.45 system-ui, sans-serif; margin: 12px; color: #2a2118; }
.card { border: 1px solid #d8cfc0; border-radius: 8px; padding: 0.6rem; }
.card h3 { margin: 0 0 0.3rem; }
.card p { margin: 0 0 0.6rem; }
.btn { background: #8a3b12; color: white; text-decoration: none; padding: 0.3rem 0.7rem; border-radius: 6px; text-align: center; }
/* your CSS here */
Hint
Flex items stretch to the tallest item by default. Inside each card, a second flex container in a column lets an auto margin push the button down.
Answer
.cards {
display: flex;
gap: 0.6rem;
}
.card {
flex: 1;
display: flex;
flex-direction: column;
}
.btn {
margin-block-start: auto;
}
The outer flex container makes the cards equal height, because align-items defaults to stretch; flex: 1 makes them equal width. Each card is also a column flex container, where margin-block-start: auto on the button takes the leftover height and pushes it to the bottom. As a flex item, the link is blockified, so it fills the card's width; add align-self: start if you want a narrower button.
3. A media object that handles long text
Put the avatar on the left at a fixed 48px and the text next to it, taking the rest of the width. The long unbroken email address must wrap inside the text column instead of overflowing the card. The avatar must stay round, never squashed.

body { font: 14px/1.45 system-ui, sans-serif; margin: 12px; color: #2a2118; }
.comment { width: 240px; border: 1px solid #d8cfc0; border-radius: 8px; padding: 0.6rem; }
.avatar { width: 48px; height: 48px; border-radius: 50%; background: #e76f51; color: white; display: grid; place-items: center; font-weight: 700; }
.mail { color: #6b6259; overflow-wrap: anywhere; }
.body p { margin: 0.2rem 0 0; }
/* your CSS here */
Hint
Two flex defaults cause trouble: items may shrink (flex-shrink: 1), and they won't shrink below their content's width (min-width: auto).
Answer
.comment {
display: flex;
gap: 0.6rem;
align-items: start;
}
.avatar {
flex: none;
}
.body {
flex: 1;
min-width: 0;
}
flex: none stops the avatar from shrinking, so it stays 48 × 48 and round. flex: 1 gives the text column the remaining space, and min-width: 0 lets it become narrower than its longest unbreakable content, so overflow-wrap can break the address. With overflow-wrap: anywhere, the address can already break here even without min-width: 0, because anywhere also reduces the content's minimum width; break-word wouldn't, so min-width: 0 is the robust habit. align-items: start keeps the avatar from stretching to the card's height.
4. Wrapping tags with even spacing
Show the tags as pills that wrap onto new lines when the row is full, with 0.4rem between pills in both directions. The list's bullets and indent must go.

body { font: 14px/1.45 system-ui, sans-serif; margin: 12px; color: #2a2118; }
.tags { width: 230px; border: 1px dashed #b9ad9c; padding: 0.5rem; }
.tags li { background: #f1e3d3; border-radius: 99px; padding: 0.1rem 0.6rem; }
/* your CSS here */
Hint
gap works in flex containers too, and on wrapped lines it spaces rows as well as items.
Answer
.tags {
list-style: none;
margin: 0;
display: flex;
flex-wrap: wrap;
gap: 0.4rem;
}
flex-wrap: wrap moves items to a new line when they don't fit, and gap adds space between items and between lines without the edge problems of margins. list-style: none removes the bullets, and margin: 0 removes the list's default top and bottom margins; the default left padding is replaced by the 0.5rem padding set in the starting CSS.
Grid
5. A card grid that adapts without media queries
Lay out the cards in a grid with 0.6rem gaps, where each column is at least 140px wide and columns share the space equally. The number of columns should change by itself with the available width: two at 350px, four at 700px.

body { font: 14px/1.45 system-ui, sans-serif; margin: 12px; color: #2a2118; }
.card { background: #f1e3d3; border-radius: 8px; padding: 1rem 0.6rem; font-weight: 600; }
/* your CSS here */
Hint
repeat() can create as many tracks as fit, and minmax() gives each track a minimum and a flexible maximum.
Answer
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
gap: 0.6rem;
}
repeat(auto-fill, minmax(140px, 1fr)) creates as many columns as fit at 140px or more, then stretches them equally with 1fr. In the 326px content box at 350px, two fit; at 676px, four fit (4 × 140 + 3 × 9.6 = 588.8px). auto-fit gives the same result here, because the six cards fill every column; the difference shows only when there are fewer items than columns.
6. A page layout with named areas
Use grid-template-areas to lay out the page. On narrow screens, everything is stacked: header, main, aside, footer. From 600px wide, the aside becomes a 10rem column to the right of main, with the header and footer across the full width. Use 0.5rem gaps.

body { font: 14px/1.45 system-ui, sans-serif; margin: 12px; color: #2a2118; }
.page > * { padding: 0.8rem; border-radius: 6px; }
header { background: #1d3557; color: white; }
main { background: #f1ece4; min-height: 5rem; }
aside { background: #e9d5c3; }
footer { background: #d8cfc0; }
/* your CSS here */
Hint
Name each element's area with grid-area, then draw the layout as strings. The media query only needs to redraw the strings and add columns.
Answer
.page {
display: grid;
gap: 0.5rem;
grid-template-areas: "header" "main" "aside" "footer";
}
header { grid-area: header; }
main { grid-area: main; }
aside { grid-area: aside; }
footer { grid-area: footer; }
@media (width >= 600px) {
.page {
grid-template-columns: 1fr 10rem;
grid-template-areas:
"header header"
"main aside"
"footer footer";
}
}
Each string in grid-template-areas is a row, and each name a cell; repeating a name across cells makes the area span them. The elements keep their names in both layouts, so the media query only changes the container: at 700px, Chrome places the aside 160px wide to the right of main, in the same row. Without the media query, the single-column version works on any width, which is the mobile-first approach (article 75).
7. A featured item that spans cells
Make a three-column grid of equal columns with 0.4rem gaps and rows 4rem tall. The featured photo should cover two columns and two rows in the top-left corner, and the other items should fill the remaining cells in order.

body { font: 14px/1.45 system-ui, sans-serif; margin: 12px; color: #2a2118; }
.item { background: #f1e3d3; border-radius: 6px; display: grid; place-items: center; font-weight: 700; }
.featured { background: #1d3557; color: white; }
/* your CSS here */
Hint
span 2 makes an item cover two tracks starting wherever the placement algorithm puts it, here the first cell.
Answer
.gallery {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 4rem;
gap: 0.4rem;
}
.featured {
grid-column: span 2;
grid-row: span 2;
}
grid-auto-rows sizes every row the grid creates, so you don't need to count rows. The featured item spans two columns and two rows from the first cell; items 2 and 3 fill the third column of those two rows, and 4 to 7 continue below. Chrome reports the featured tile as 64 × 2 + 6.4 = 134.4px tall.
8. Text over an image without positioning
Place the caption on top of the photo, at its bottom edge, using grid instead of position. The caption gets a dark translucent background, rgb(0 0 0 / 0.55), and white text.

body { font: 14px/1.45 system-ui, sans-serif; margin: 12px; color: #2a2118; }
.photo { margin: 0; border-radius: 10px; overflow: hidden; }
.img { aspect-ratio: 16 / 9; background: linear-gradient(#f4a261 10%, #e76f51 45%, #264653); }
figcaption { padding: 0.4rem 0.6rem; }
/* your CSS here */
Hint
Two grid items can be placed in the same cell. Later items paint on top of earlier ones.
Answer
.photo {
display: grid;
}
.photo > * {
grid-area: 1 / 1;
}
figcaption {
align-self: end;
background: rgb(0 0 0 / 0.55);
color: white;
}
grid-area: 1 / 1 puts both children in the first row and column, so they overlap; the caption comes later in the HTML, so it paints on top. align-self: end moves it to the bottom of the cell, whose height comes from the image's aspect ratio. Unlike absolute positioning, the caption still takes part in sizing: a long caption makes the cell taller instead of overflowing.
9. A form with aligned labels
Lay out the form as a two-column grid: labels in the first column, as wide as the longest label, and inputs filling the rest. Align each label with its input's text vertically. The button goes in the second column, sized to its text.

body { font: 14px/1.45 system-ui, sans-serif; margin: 12px; color: #2a2118; }
input, button { font: inherit; padding: 0.3rem 0.4rem; border: 1px solid #b9ad9c; border-radius: 5px; }
button { background: #8a3b12; color: white; border: 0; padding-inline: 0.9rem; }
/* your CSS here */
Hint
max-content sizes a track to its widest content. Items fill the grid in source order, so labels and inputs alternate between the two columns automatically.
Answer
.f {
display: grid;
grid-template-columns: max-content 1fr;
gap: 0.5rem 0.6rem;
align-items: center;
}
.f button {
grid-column: 2;
justify-self: start;
}
With two columns, the six labels and inputs fall into place row by row. max-content makes the first column exactly as wide as "Arrival date", so every input starts at the same edge. The button would land in column 1, so grid-column: 2 moves it under the inputs, and justify-self: start stops it from stretching. Inputs stretch to fill their column because grid items stretch by default.
Positioning
10. A badge in the corner of a card
Place the "-20%" badge in the top-right corner of the card, overlapping the card's edge by 0.5rem up and to the right. The card's content must not move. The badge must follow the card if the card moves.

body { font: 14px/1.45 system-ui, sans-serif; margin: 12px; color: #2a2118; }
.wrap { padding: 1rem; }
.card { border: 1px solid #d8cfc0; border-radius: 8px; padding: 0.8rem; }
.card h3 { margin: 0 0 0.2rem; }
.card p { margin: 0; }
.badge { background: #c62828; color: white; font-weight: 700; font-size: 0.75rem; width: 2.6rem; height: 2.6rem; border-radius: 50%; display: grid; place-items: center; }
/* your CSS here */
Hint
An absolutely positioned element is placed relative to its containing block: the nearest positioned ancestor.
Answer
.card {
position: relative;
}
.badge {
position: absolute;
top: -0.5rem;
right: -0.5rem;
}
position: absolute takes the badge out of the flow, so the heading moves up to where it would be without it. position: relative on the card, with no offsets, doesn't move the card but makes it the badge's containing block. Negative insets then push the badge 8px past the card's padding edge, the inside of its border. Without position: relative on the card, the badge would be placed relative to the page.
11. A centered dialog over a dimmed page
The .overlay should cover the whole viewport with rgb(0 0 0 / 0.5), even if the page scrolls, and the .box must be centered in it horizontally and vertically. Use as little CSS as you can.

body { font: 14px/1.45 system-ui, sans-serif; margin: 12px; color: #2a2118; }
body { min-height: 260px; }
.box { background: white; border-radius: 10px; padding: 1rem; width: 14rem; }
.box p { margin: 0.3rem 0 0.7rem; }
/* your CSS here */
Hint
inset: 0 sets all four offsets at once. Then the overlay is a box the size of the viewport, and any centering technique works inside it.
Answer
.overlay {
position: fixed;
inset: 0;
background: rgb(0 0 0 / 0.5);
display: grid;
place-items: center;
}
position: fixed with inset: 0 stretches the overlay to the viewport, and it stays there while the page scrolls. display: grid with place-items: center centers the box in both directions with one declaration. For a real dialog, use the <dialog> element with showModal() instead (article 60): it adds a backdrop, puts the dialog in the top layer, and makes the rest of the page inert.
12. A tooltip centered under its button
Show the tooltip below the button, 6px under it, horizontally centered on the button, whatever the tooltip's width. Draw a small arrow pointing up at the button with a pseudo-element. (In a real interface it would appear on hover or focus; here it stays visible.)

body { font: 14px/1.45 system-ui, sans-serif; margin: 12px; color: #2a2118; }
body { padding: 0.5rem 5rem 3rem; }
button { font: inherit; padding: 0.35rem 0.8rem; }
.tip { background: #1f2328; color: white; font-size: 0.8rem; padding: 0.3rem 0.5rem; border-radius: 5px; white-space: nowrap; }
/* your CSS here */
Hint
left: 50% is a percentage of the containing block; translate: -50% is a percentage of the tooltip itself. Together they center it.
Answer
.tip-wrap {
position: relative;
display: inline-block;
}
.tip {
position: absolute;
top: calc(100% + 6px);
left: 50%;
translate: -50% 0;
}
.tip::before {
content: "";
position: absolute;
bottom: 100%;
left: 50%;
translate: -50% 0;
border: 5px solid transparent;
border-bottom-color: #1f2328;
}
top: calc(100% + 6px) starts the tooltip 6px below the wrapper, which is exactly as large as the button because it's an inline-block. left: 50% moves the tooltip's left edge to the button's center, and translate: -50% 0 shifts it back by half its own width. The arrow is an empty box with only a bottom border colored, which draws a triangle. Anchor positioning (article 59) does this without a wrapper and can flip the tooltip when there's no room below.
Responsive design
13. Fluid headline size
Make the heading's font size grow smoothly with the viewport width: never smaller than 1.5rem and never larger than 2.5rem, with a preferred size of 1rem + 3vw. Write a single declaration and no media queries.

body { font: 14px/1.45 system-ui, sans-serif; margin: 12px; color: #2a2118; }
h1 { margin: 0 0 0.3rem; line-height: 1.15; font-size: 2rem; }
p { margin: 0; }
/* your CSS here */
Hint
clamp(min, preferred, max) returns the preferred value, kept between the minimum and the maximum.
Answer
h1 {
font-size: clamp(1.5rem, 1rem + 3vw, 2.5rem);
}
At 350px, 1rem + 3vw is 16 + 10.5 = 26.5px, between the 24px minimum and the 40px maximum, so Chrome uses 26.5px. At 700px, it would be 37px, still under the 40px cap. Adding a rem term to the vw keeps the size responsive to the user's font setting and to zoom, which a pure vw value isn't (article 77).
14. A card that changes layout with its container
Make .slot a size container. When the card's container is at least 400px wide, put the image on the left at 40% of the card's width and the text on the right; otherwise, stack them. Use a container query, not a media query, so the card works in narrow sidebars too.

body { font: 14px/1.45 system-ui, sans-serif; margin: 12px; color: #2a2118; }
.card { border: 1px solid #d8cfc0; border-radius: 10px; overflow: hidden; }
.img { aspect-ratio: 3 / 2; background: linear-gradient(135deg, #2a9d8f, #264653); }
.text { padding: 0.7rem; }
.text h3 { margin: 0 0 0.2rem; }
.text p { margin: 0 0 0.4rem; }
/* your CSS here */
Hint
A container query can't style the container itself, only its descendants, so the container must be an element around the card.
Answer
.slot {
container-type: inline-size;
}
@container (width >= 400px) {
.card {
display: grid;
grid-template-columns: 40% 1fr;
}
.img {
aspect-ratio: auto;
}
}
container-type: inline-size makes .slot a query container for its width. The @container rule then checks that width, not the viewport's, so the same card stacks in a 300px sidebar even on a wide screen. In the wide layout, aspect-ratio: auto lets the image stretch to the text's height instead of forcing 3. At 700px, Chrome makes the image 270px wide: 40% of the 674px content box inside the card's border.
Common questions
The wide target shows a layout I can't see in the 350px preview. How do I test it? Copy the HTML and CSS into a local file and open it in your browser, then resize the window or use the device toolbar in DevTools. The checks in each explanation were run at 350px and 700px.
Why do several answers use grid where flexbox would also work? Flexbox is best when items share one line and size from their content; grid when you place items in rows and columns at once. Many layouts work with either. Article 65 compares them.
Summary
- Flexbox:
gap, auto margins,flex: noneandmin-width: 0solve most row layouts; nested flex columns push footers down. - Grid:
repeat(auto-fill, minmax())adapts without breakpoints; named areas make responsive page layouts readable;spanand shared cells handle overlap and features. - Positioning: make the intended ancestor
position: relative, then use insets;left: 50%plustranslate: -50%centers on an anchor. - Responsive:
clamp()with aremterm for fluid sizes, and container queries for components that live in different places.
Practice questions
In exercise 5, what changes if only three cards remain, with auto-fill vs auto-fit, at 700px?
Answer
Four columns fit. With auto-fill, the fourth column stays empty, so the three cards keep their 160px-or-so width. With auto-fit, the empty track collapses to zero, and the three cards stretch to fill the row.
In exercise 10, the card is inside a transformed wrapper. Does the badge still work?
Answer
Yes. A transform would make the wrapper a containing block for absolutely positioned descendants, but the card is closer and already positioned, so it stays the badge's containing block.
Previous: CSS Exercises: Beginner (Selectors, Cascade, Box Model and Color)
Next: CSS Exercises: Advanced (Modern Layout, Motion, Container Queries and Architecture)

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.