CSS Exercises
Time to practise. Each exercise gives you a starter playground — edit the CSS until the result matches the goal, then compare with the solution. Try on your own first; peeking is allowed, but you'll learn more if you struggle a little.
| # | Topic |
|---|---|
| 1 | Selectors and colours |
| 2 | Box model |
| 3 | Text and fonts |
| 4 | Link states |
| 5 | Positioning a badge |
| 6 | Flexbox nav bar |
| 7 | Centering |
| 8 | Grid gallery |
| 9 | Transitions |
| 10 | Media query |
Exercise 1 — Selectors and colours
Make the heading dark blue (#0d47a1), give every paragraph with class note a light yellow background (#fff9c4), and make the element with id warning red text.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
/* your CSS here */
</style>
</head>
<body>
<h1>Exercise 1</h1>
<p class="note">This is a note.</p>
<p>A normal paragraph.</p>
<p class="note">Another note.</p>
<p id="warning">Careful!</p>
</body>
</html>Show solution
<style>
body { font-family: system-ui, sans-serif; }
h1 { color: #0d47a1; }
.note { background: #fff9c4; }
#warning { color: red; }
</style>Exercise 2 — Box model
Turn the .card into a box that is exactly 250px wide including padding and border, with 16px padding, a 3px solid #7b1fa2 border, rounded 10px corners and 20px of space above it.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
.ruler { width: 250px; height: 6px; background: #333; }
.card { background: #f3e5f5; }
</style>
</head>
<body>
<div class="ruler"></div>
<div class="card">I should be exactly as wide as the ruler.</div>
</body>
</html>Show solution
<style>
.card {
background: #f3e5f5;
box-sizing: border-box;
width: 250px;
padding: 16px;
border: 3px solid #7b1fa2;
border-radius: 10px;
margin-top: 20px;
}
</style>Exercise 3 — Text and fonts
Style the quote: Georgia (serif fallback), italic, 22px, line-height 1.5, centred, grey #555. Make the author line uppercase with 2px letter spacing.
<!DOCTYPE html>
<html>
<head>
<style>
/* your CSS here */
</style>
</head>
<body>
<p class="quote">Simplicity is the soul of efficiency.</p>
<p class="author">An engineer</p>
</body>
</html>Show solution
<style>
.quote {
font-family: Georgia, serif;
font-style: italic;
font-size: 22px;
line-height: 1.5;
text-align: center;
color: #555;
}
.author {
text-align: center;
text-transform: uppercase;
letter-spacing: 2px;
}
</style>Exercise 4 — Link states
Remove the underline from the links, colour them #00796b, and on hover show a #e0f2f1 background and an underline. Add a visible orange focus outline for keyboard users.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; font-size: 20px; }
/* your CSS here */
</style>
</head>
<body>
<p><a href="#">First link</a> and <a href="#">second link</a></p>
</body>
</html>Show solution
<style>
a { color: #00796b; text-decoration: none; padding: 2px 4px; }
a:hover { background: #e0f2f1; text-decoration: underline; }
a:focus-visible { outline: 3px solid orange; outline-offset: 2px; }
</style>Exercise 5 — Positioning a badge
Place the red .badge on the top-right corner of the button, half overlapping its edge.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; padding: 30px; }
.btn { padding: 12px 20px; background: #1565c0; color: white; border-radius: 8px; display: inline-block; }
.badge { background: #e53935; color: white; border-radius: 50%; width: 24px; height: 24px; display: grid; place-items: center; font-size: 13px; }
</style>
</head>
<body>
<span class="btn">Messages <span class="badge">5</span></span>
</body>
</html>Show solution
<style>
.btn { position: relative; /* plus the existing styles */ }
.badge {
position: absolute;
top: -12px;
right: -12px;
/* plus the existing styles */
}
</style>Exercise 6 — Flexbox nav bar
Turn the list into a horizontal dark nav bar: no bullets, links side by side with 14px padding, and the last item ("Log in") pushed to the far right.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; margin: 0; }
/* your CSS here */
</style>
</head>
<body>
<ul class="nav">
<li><a href="#">Home</a></li>
<li><a href="#">Shop</a></li>
<li><a href="#">Blog</a></li>
<li class="login"><a href="#">Log in</a></li>
</ul>
</body>
</html>Show solution
<style>
.nav { list-style: none; margin: 0; padding: 0; display: flex; background: #212121; }
.nav a { display: block; padding: 14px; color: white; text-decoration: none; }
.nav a:hover { background: #424242; }
.login { margin-left: auto; }
</style>Exercise 7 — Centering
Centre the .dialog horizontally and vertically inside the full-height page.
<!DOCTYPE html>
<html>
<head>
<style>
html, body { height: 100%; margin: 0; }
body { background: #b0bec5; font-family: system-ui, sans-serif; }
.dialog { background: white; padding: 20px 30px; border-radius: 10px; width: fit-content; }
</style>
</head>
<body>
<div class="dialog">Centre me!</div>
</body>
</html>Show solution
<style>
body {
display: grid;
place-items: center;
/* or: display: flex; justify-content: center; align-items: center; */
}
</style>Exercise 8 — Grid gallery
Lay out the tiles as a grid with as many 90px-minimum columns as fit, 8px gaps, and make the first tile span two columns and two rows.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
.tile { height: 70px; border-radius: 8px; background: linear-gradient(135deg, #7e57c2, #ec407a); }
/* your CSS here */
</style>
</head>
<body>
<div class="gallery">
<div class="tile"></div><div class="tile"></div><div class="tile"></div><div class="tile"></div>
<div class="tile"></div><div class="tile"></div><div class="tile"></div><div class="tile"></div>
</div>
</body>
</html>Show solution
<style>
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(90px, 1fr));
grid-auto-rows: 70px;
gap: 8px;
}
.tile { height: auto; }
.tile:first-child { grid-column: span 2; grid-row: span 2; }
</style>Exercise 9 — Transitions
When hovering the card, it should lift up by 6px and get a bigger shadow, animating smoothly over 0.25s.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; padding: 40px; background: #f5f5f5; }
.card { width: 200px; padding: 20px; background: white; border-radius: 12px; box-shadow: 0 2px 4px rgba(0,0,0,.15); }
/* your CSS here */
</style>
</head>
<body>
<div class="card">Hover over me</div>
</body>
</html>Show solution
<style>
.card { transition: transform .25s ease, box-shadow .25s ease; }
.card:hover {
transform: translateY(-6px);
box-shadow: 0 12px 24px rgba(0,0,0,.2);
}
</style>Exercise 10 — Media query
The two panels sit side by side. Make them stack vertically when the frame is narrower than 450px (drag the playground's edge to test).
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
.row { display: flex; gap: 10px; }
.row div { flex: 1; padding: 30px 10px; color: white; text-align: center; border-radius: 8px; }
.a { background: #ef6c00; }
.b { background: #0277bd; }
/* your CSS here */
</style>
</head>
<body>
<div class="row"><div class="a">Left</div><div class="b">Right</div></div>
</body>
</html>Show solution
<style>
@media (max-width: 449px) {
.row { flex-direction: column; }
}
</style>Test yourself
Which declaration makes width include padding and border?
- box-sizing: content-box
- box-sizing: border-box
- width: include
- padding: inside
Which single rule pushes the last flex item to the far end of a row?
- float: right
- margin-left: auto
- order: 99
- justify-self: end
What must the parent of an absolutely positioned badge usually have?
- display: flex
- position: relative
- overflow: hidden
- z-index: -1