CSS Exercises with Answers
Practise CSS with 228 interactive exercises: multiple choice, fill-in-the-blank, output prediction and debugging. Every exercise gives instant feedback, a hint and the full solution.
1CSS Introduction
Read the lessonMake every paragraph's text green.
{
: green;
}
p selects paragraphs and color sets the text colour.In h1 { color: red; }, what is h1?
The heading should be orange and 32px, but the CSS has two syntax errors. Fix them.
<h1>Pumpkin Spice Season</h1>h1 {
color = orange;
font-size: 32px
font-family: sans-serif;
}- The heading is orange
- The heading is 32px
- The font is sans-serif
: between property and value, and end every declaration with ;. A missing semicolon breaks the next line too.2CSS Syntax
Read the lessonComplete the rule.
.price
color crimson
}
{ }, declarations use property: value;.What does h1, p { color: gray; } do?
Three things are wrong: a // comment, a missing semicolon and a missing closing brace. Fix them so the badge is purple with white text.
<span class="badge">PRO</span>// badge styles
.badge {
background: purple
color: white;
padding: 4px 8px;- The badge is purple
- The text is white
- The padding works
3Adding CSS to HTML
Read the lessonLink the external stylesheet.
<link ="stylesheet" ="main.css">
rel="stylesheet" says what the file is; href says where it is.Which way of adding CSS is best for a website with many pages?
A paragraph has an inline style color: red and the stylesheet says p { color: blue; }. What colour is it?
The internal stylesheet isn't working because it's not valid. Fix it so the button is green with white text.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Shop</title>
<style src="">
button { background: green; color: white }
</stlye>
</head>
<body>
<button>Buy</button>
</body>
</html>- The button is green
- The button text is white
4Selectors
Read the lessonSelect the element with id="hero" and all elements with class="btn".
hero { height: 300px; }
btn { padding: 10px; }
# for ids, . for classes.What does article > p select?
> is the child combinator, so it matches direct children only.Which selector targets an element with both classes btn and large?
Which items turn red?
<h2>Title</h2>
<p>A</p>
<p>B</p>
<style> h2 + p { color: red; } </style>
+ selects only the element immediately after the h2.The CSS should make only the links inside the footer white, and the .price paragraph bold. Fix the selectors.
<a href="#">Header link</a>
<footer><a href="#">Footer link</a></footer>
<p class="price">£20</p>
<style>footer { background: #15161b; padding: 8px; }</style>a footer {
color: white;
}
p .price {
font-weight: bold;
}- Footer link is white
- Header link is not white
- Price is bold
5Combinators & Attributes
Read the lessonStyle only the list items that are direct children of .steps.
.steps li {
font-weight: bold;
}
> is the child combinator.Which selector matches every link whose address ends in .pdf?
$= means "ends with". ^= means "starts with".What does h3 ~ p select?
~ is the subsequent-sibling combinator.Which selector matches <input type="EMAIL"> and <input type="email">?
i flag makes the value match ignore case.Only the paragraph right after each heading should be bold, and only email inputs should have a blue border. Fix the selectors.
<h2>News</h2>
<p class="lead">First paragraph.</p>
<p class="body">Second paragraph.</p>
<input class="mail" type="email">
<input class="name" type="text">h2 ~ p { font-weight: bold; }
input [type=email] { border: 2px solid rgb(37, 99, 235); }- Lead paragraph is bold
- Second paragraph is not bold
- Email input has the blue border
- Text input is unchanged
6Colors
Read the lessonSet the text to white and the background to a HEX black.
.banner {
: white;
background-color: ;
}
color sets text; #000 (or #000000) is black.In hsl(220 80% 50%), what does 220 control?
You want a see-through background but solid text. What should you use?
opacity affects the whole element; an alpha colour affects only that property.The alert should have a light red background (#ffe3e3) and dark red text (rgb(153 27 27)). Two values are broken.
<p class="alert">Payment failed. Please try again.</p>.alert {
background-color: ffe3e3;
color: rgb(153, 27);
padding: 10px;
}- Light red background
- Dark red text
7Backgrounds
Read the lessonMake the photo fill the whole box and stay centred.
.cover-photo {
background-image: ("lake.jpg");
background-size: ;
background-position: center;
}
url() loads the image; cover fills the box.Which value shows the whole image without cropping?
contain fits the entire image inside the box.What does background-repeat: no-repeat do?
no-repeat stops that.The banner is invisible and its image tiles. Fix it: add a min-height of at least 150px, stop the repeating, and correct the image syntax.
<div class="banner"></div>.banner {
background-color: #1e1b4b;
background-image: "https://picsum.photos/id/1043/600/300";
}- Banner is at least 150px tall
- Uses url() for the image
- The image does not repeat
8Borders & Radius
Read the lessonGive the box a 2px solid black border and 10px rounded corners.
.box {
border: 2px black;
: 10px;
}
border-radius rounds corners.How do you make a square image into a circle?
What's the main difference between outline and border?
The quote should have a 5px solid green left border and the tag should be a pill shape. Fix the CSS.
<blockquote class="quote">Simplicity is the soul of efficiency.</blockquote>
<span class="tag">new</span>.quote {
border-left: 5px green;
padding-left: 12px;
}
.tag {
background: #ffd23f;
padding: 2px 10px;
border-radius: 2;
}- Quote has a 5px solid left border
- Border is green
- Tag is pill-shaped
9Text
Read the lessonCentre the heading and make it uppercase.
h1 {
: center;
text-transform: ;
}
text-align positions text; text-transform changes its case.Which line-height value is best for body text?
The product name should stay on one line and end with "…" when too long, but it's wrapping. Fix the CSS.
<p class="name">Ultra Comfortable Ergonomic Office Chair with Lumbar Support</p>.name {
width: 200px;
text-overflow: ellipsis;
}- Text doesn't wrap
- Overflow is hidden
- Ellipsis is set
10Fonts
Read the lessonGive the heading a serif font stack and make it bold.
h2 {
: Georgia, ;
font-weight: ;
}
Which generic family should code use?
Why do font names like "Open Sans" need quotes?
The heading should use a bold Courier monospace stack and be 28px. The font shorthand is in the wrong order. Fix it.
<h1 class="code-title">console.log("hi")</h1>.code-title {
font: "Courier New", monospace 28px bold;
}- It's bold
- Size is 28px
- Uses a monospace stack
11Units (px, rem, %)
Read the lessonMake the text 1.25 times the root font size and the box half its parent's width.
.card {
font-size: 1.25;
width: 50;
}
rem is relative to the root font size; % is relative to the parent.A parent has font-size: 20px. Its child has font-size: 1.5em. How big is the child's text?
.parent { font-size: 20px; }
.child { font-size: 1.5em; }
The card should be 320px wide on big screens but never wider than 90% of its container, and the title should use rem. Fix the invalid values.
<div class="wrap"><div class="card"><h2 class="title">Weekend deals</h2></div></div>.wrap { width: 300px; }
.card {
width: min(320, 90);
background: #fff1c2;
}
.title {
font-size: 1.5;
}- Card is 90% of its 300px container (270px)
- Title is 1.5rem (24px)
12Width & Height
Read the lessonMake the card shrink on small screens but never grow beyond 500px.
.card {
width: 100%;
: 500px;
}
max-width sets the upper limit.With box-sizing: border-box, what does width include?
Why might height: 50% have no effect?
Which property keeps a video box at 16:9 as it resizes?
The panel should be exactly 300px wide including its padding, and at least 120px tall even when empty. Fix the CSS.
<div class="panel"></div>.panel {
width: 300px;
padding: 20px;
height: 120;
background: #e0f2fe;
}- Panel is 300px wide in total
- Panel is at least 120px tall
- Uses min-height
13Overflow & Visibility
Read the lessonShow scrollbars only when the content is too big.
.panel {
max-height: 200px;
overflow: ;
}
auto adds scrollbars only when needed.Which rule hides an element but keeps its space in the layout?
Which three properties make a one-line ellipsis?
A table is wider than the phone screen. What's the best fix?
The caption should be cut off with an ellipsis on one line, and the hidden note should not take up any space. Fix it.
<p class="caption">A very long photo caption that keeps going and going past the edge</p>
<p class="note">Hidden note</p>.caption {
width: 200px;
overflow: visible;
text-overflow: ellipsis;
}
.note { visibility: hidden; }- Caption stays on one line
- Caption overflow is hidden
- Ellipsis is set
- Note takes no space
14Box Model
Read the lessonMake width include padding and border.
* {
: ;
}
border-box makes width/height describe the whole box.Which layer of the box model is always transparent?
With the default content-box, how wide does this box appear?
.box { width: 100px; padding: 10px; border: 2px solid; }
The search input overflows its 300px container. Make it fit exactly without removing the padding or border.
<div class="bar"><input class="search" placeholder="Search…"></div>
<style>.bar { width: 300px; background: #eee; }</style>.search {
width: 100%;
padding: 12px;
border: 2px solid #15161b;
}- The input is exactly 300px wide
- Padding is still 12px
15Margin & Padding
Read the lessonGive the button 12px top/bottom and 24px left/right padding.
.btn {
padding: ;
}
Which property adds space inside the border?
What does margin: 5px 10px 15px 20px set the left margin to?
The article should be centred with a max width of 300px, and its text should have 20px of space from the edges. Fix the CSS.
<article class="post"><p>Centred article text.</p></article>.post {
max-width: 300px;
margin: auto 0;
margin-padding: 20px;
background: #d6f4e4;
}- Left and right margins are equal (centred)
- Padding is 20px
16Display
Read the lessonLet the link accept width and padding while staying in line.
.tag {
display: ;
padding: 4px 10px;
}
inline-block sits inline but respects box sizing.Which value removes an element from the page completely?
display: none removes it from layout and from screen readers.What's the difference between display: none and visibility: hidden?
The three .chip links should sit in a row, each 100px wide with 8px padding, and the .secret paragraph should be removed from the layout. Fix the CSS.
<a class="chip" href="#">Red</a>
<a class="chip" href="#">Green</a>
<a class="chip" href="#">Blue</a>
<p class="secret">You shouldn't see me</p>
<p>Visible text</p>.chip {
display: block;
width: 100px;
padding: 8px;
background: #eee;
}
.secret {
visibility: hidden;
}- Chips sit side by side
- Chips are 100px wide (plus padding)
- The secret is removed from the layout
17Position & z-index
Read the lessonPin the button to the bottom-right of the screen.
.help {
position: ;
: 20px;
right: 20px;
}
fixed positions relative to the window.An absolute element is positioned relative to…
position: relative to the parent.Which element has a z-index that actually works?
The "NEW" ribbon should sit in the top-left corner of the product box, not the page. Fix it.
<div class="product">
<span class="ribbon">NEW</span>
<p>Trail Sneakers</p>
</div>.product {
width: 200px;
margin: 40px;
padding: 30px 16px 16px;
border: 2px solid #15161b;
}
.ribbon {
position: absolute;
top: 0;
left: 0;
background: #ffd23f;
padding: 2px 8px;
}- The ribbon sits inside the product box
- Still absolutely positioned
18Float & Clear
Read the lessonFloat the photo to the right.
.photo {
: right;
}
float takes left, right or none.What happens to text after a floated image?
Which property makes a heading start below earlier floats?
A container holds only floated boxes and its background has vanished. What's the modern fix?
The gallery's grey background has collapsed because its images are floated, and the caption sits beside the images instead of below. Fix both.
<div class="gallery">
<div class="thumb"></div><div class="thumb"></div>
</div>
<p class="caption">Two photos from the trip.</p>.gallery { background: #e5e7eb; padding: 6px; }
.thumb { float: left; width: 80px; height: 80px; margin: 4px; background: #60a5fa; }
.caption { clear: none; }- Gallery wraps its floats
- Caption clears floats
- Thumbs are still floated
19Flexbox
Read the lessonLay out the children in a row with space between them, vertically centred.
.toolbar {
display: ;
justify-content: ;
align-items: ;
}
display: flex, then justify on the main axis and align on the cross axis.In a default flex row, which property centres items vertically?
align-items.What does flex: 1 on every item do?
How are the three boxes arranged?
.row { display: flex; flex-direction: column; justify-content: flex-end; height: 300px; }
flex-end means the bottom.The three buttons should sit in a row, 10px apart, and be centred horizontally inside the toolbar. Fix the CSS.
<div class="toolbar">
<button>Bold</button>
<button>Italic</button>
<button>Link</button>
</div>.toolbar button {
display: flex;
justify-content: center;
margin: 10px;
}
.toolbar {
padding: 10px;
background: #e5e7eb;
}- Toolbar is a flex container
- Items are centred
- Buttons are 10px apart
- Buttons share one row
20Grid
Read the lessonCreate three equal columns with a 20px gap.
.grid {
display: ;
grid-template-columns: repeat(3, );
: 20px;
}
repeat(3, 1fr) makes three equal columns; gap spaces them.Which makes an item stretch across every column?
How many columns appear in a 700px-wide container?
.g { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 0; }
The dashboard should have a sidebar 120px wide and a main area taking the rest, with 10px gap. Fix the CSS.
<div class="dash">
<nav>Menu</nav>
<section>Stats</section>
</div>.dash {
grid-template-columns: 120 fr;
gap: 10px;
}
.dash > * { background: #e1e6ff; padding: 10px; }- Dash is a grid
- Sidebar is 120px wide
- Main area fills the rest
- 10px gap
21Centering
Read the lessonCenter a child in both directions with grid.
.parent {
display: grid;
: center;
}
place-items sets both align-items and justify-items.Which rule centers the text inside a paragraph?
What does a block need for margin: 0 auto to center it?
In the transform centering trick, why is translate(-50%, -50%) needed?
The message should sit in the exact center of the stage, both horizontally and vertically. Fix the CSS.
<div class="stage"><p class="msg">Hello</p></div>.stage {
height: 200px;
text-align: center;
vertical-align: middle;
}
.msg { margin: 0; }- Message is vertically centered
- Message is horizontally centered
22Pseudo-classes
Read the lessonChange link colour on hover and stripe even rows.
a { color: tomato; }
tr:nth-child() { background: #f5f5f5; }
:hover for the mouse state, :nth-child(even) for every second row.Which selector targets every list item except the last?
:not() excludes whatever is inside it.Why prefer :focus-visible over :focus for outlines?
Which items are bold?
<ul><li>A</li><li>B</li><li>C</li><li>D</li><li>E</li><li>F</li></ul>
<style> li:nth-child(3n) { font-weight: bold; } </style>
The button should turn dark on hover and the first table row should be bold. The selectors are wrong. Fix them.
<button class="go">Go</button>
<table><tr><td>Header-ish</td></tr><tr><td>Row</td></tr></table>.go { background: #ffd23f; border: 0; padding: 10px 16px; }
.go::hover {
background: #15161b;
color: white;
}
tr:first {
font-weight: bold;
}- The hover rule is valid
- The first row is bold
- The second row is not bold
23Pseudo-elements
Read the lessonAdd a star after every .fav element.
.fav {
: " ★";
}
::after plus a content value.How many colons do pseudo-elements use in modern CSS?
::before. The two colons distinguish pseudo-elements from pseudo-classes.Which property is required for ::before to appear?
content, no pseudo-element.Required labels should show a red asterisk after the text. Fix the CSS.
<label class="required">Email</label>.required:after {
color: red;
}- The asterisk appears
- It's red
24Links & Buttons
Read the lessonRemove the underline from navigation links.
nav a {
: none;
}
text-decoration: none removes underlines.Which pseudo-class styles a link while the mouse is over it?
Which selector shows a focus ring for keyboard users but not mouse clicks?
Which property moves the underline further from the text?
The button should have the hand cursor, inherit the page font, and show a 3px solid outline on keyboard focus instead of none. Fix the CSS.
<div class="page"><button class="btn">Order</button></div>.page { font-family: Georgia, serif; }
.btn { cursor: default; padding: 10px 16px; }
.btn:focus-visible { outline: none; }- Pointer cursor
- Inherits the page font
- Focus ring is defined
26Dropdowns & Tooltips
Read the lessonOpen the menu when anything inside the dropdown has keyboard focus.
.dropdown: .menu {
display: block;
}
:focus-within matches a parent when a descendant has focus.What makes the dropdown wrapper the reference point for the menu?
What does content: attr(data-tip) do?
Which HTML attribute makes a menu that opens on click and closes on Esc with no JavaScript?
The menu should sit directly under the trigger, be hidden by default, and appear on hover. Fix the CSS.
<div class="dd"><a href="#">Menu</a><div class="list">Item</div></div>.dd { position: static; }
.list { position: absolute; top: 100%; left: 0; display: block; }
.dd:hover .list { display: none; }- Wrapper is positioned
- Menu hidden by default
- Hover reveals the menu
27Forms
Read the lessonMake the checkbox green with one property.
input[type="checkbox"] {
: #16a34a;
}
accent-color recolours checkboxes, radios and sliders.Which selector shows errors only after the user has interacted?
Why use at least 16px text in inputs?
The input should fill its 300px form without overflowing, use the form's font, and only allow the textarea to resize vertically. Fix it.
<form class="f"><input class="in"><textarea class="ta"></textarea></form>.f { width: 300px; font-family: Georgia, serif; }
.in { width: 100%; padding: 12px; border: 2px solid #ccc; }
.ta { resize: both; }- Input fits the form
- Input uses the form font
- Textarea resizes vertically only
28Tables
Read the lessonMerge the doubled cell borders into single lines.
table {
border-collapse: ;
}
collapse merges adjacent borders.Which selector styles every other row for zebra stripes?
How do you keep a header row visible while scrolling a long table?
Why use font-variant-numeric: tabular-nums in a price column?
The table should have single collapsed borders, 10px cell padding, and right-aligned numbers. Fix it.
<table class="t"><tr><td class="name">Tea</td><td class="num">2.50</td></tr></table>.t { border-collapse: separate; }
.t td { border: 1px solid #ccc; padding: 0; }
.num { text-align: left; }- Borders collapsed
- Cells have 10px padding
- Numbers align right
29Images & Filters
Read the lessonFill the box and crop the image without stretching it.
.thumb {
object-fit: ;
}
cover fills the box and crops the extra.Which value shows the whole image without cropping?
How do you make a black and white image?
Which property keeps faces visible when a tall photo is cropped?
The avatar should be an 80px circle, cropped rather than stretched, and in black and white. Fix it.
<img class="av" src="https://picsum.photos/seed/av/200/300" alt="Avatar" width="200" height="300">.av {
width: 80px;
height: 60px;
border-radius: 5%;
object-fit: fill;
filter: sepia(100%);
}- Square 80px box
- Round
- Cropped, not stretched
- Black and white
30Specificity & Cascade
Read the lessonWrite the specificity scores (IDs-classes-elements).
/* */ ul.menu { }
/* */ #logo { }
/* */ .card:hover { }
ul.menu = 1 class + 1 element; #logo = 1 id; .card:hover = 2 class-level parts.Two rules have exactly the same specificity. Which one wins?
Which of these is inherited by default?
What colour is the text?
<div id="box" class="box"><p class="text">Hello</p></div>
<style>
#box { color: red; }
.text { color: blue; }
</style>
#box sets the div's colour; the paragraph only inherits it. .text targets the paragraph directly, so it wins.The "Buy" button should be green, but a more specific rule keeps it gray. Fix it without !important and without an id, by changing the second selector so it wins.
<div class="shop"><button class="btn buy">Buy</button></div>.shop .btn {
background: gray;
color: white;
}
.buy {
background: green;
}- The button is green
- No !important used
- No id selectors used
31Inheritance & !important
Read the lessonMake the button use its parent's font.
button {
font: ;
}
inherit takes the parent's value.Which property is inherited by default?
What does @layer base, utilities; do?
Which keyword returns a property to the browser's default for that element?
The note should use the card's colour and font, and the input should match the page font. Fix the CSS.
<div class="card">
<p class="note">Card note</p>
<input class="field" value="Name">
</div>.card { color: rgb(22, 101, 52); font-family: Georgia, serif; }
.note { color: initial; }
.field { font-family: initial; }- Note uses the card colour
- Input uses the card font
32calc() & clamp()
Read the lessonMake the box the full width minus 40px.
.box {
width: (100% - 40px);
}
calc() mixes percentages and pixels.What does clamp(1rem, 4vw, 2rem) return on a very wide screen?
Which line caps a width at 500px but lets it shrink on phones?
Why is calc(100%-10px) ignored?
The panel should be its container's width minus 50px, and the title should be clamped between 20px and 32px. Fix the CSS.
<div class="wrap"><div class="panel"><h2 class="title">Title</h2></div></div>.wrap { width: 400px; }
.panel { width: calc(100%-50px); background: #e0f2fe; }
.title { font-size: clamp(32px, 3vw); }- Panel is 350px wide
- Title size is between 20px and 32px
- Uses clamp with three values
33CSS Variables
Read the lessonDefine a variable and use it.
:root {
: #f0592b;
}
.link {
color: ;
}
--name, read with var(--name).Where should you put global variables?
:root is the top of the document, so everything inherits from it.What does var(--size, 16px) do if --size doesn't exist?
The card variables aren't working. Fix the definition and usage so the card has a #e1e6ff background and 20px padding.
<div class="card">Variables are neat</div>:root {
card-bg: #e1e6ff;
--card-pad = 20px;
}
.card {
background: var(card-bg);
padding: --card-pad;
}- Background comes through
- Padding is 20px
- Uses variables
34Media Queries
Read the lessonApply the rule only on screens 768px wide or more.
@ (: 768px) {
.menu { display: flex; }
}
@media (min-width: …) targets screens at least that wide.In a mobile-first approach, which queries do you mostly write?
min-width adds enhancements for larger screens.Which media feature detects the user's dark mode setting?
prefers-color-scheme reflects the operating system preference.On screens 700px and wider, the two columns should sit side by side. The media query is broken. Fix it.
<div class="cols"><div>Left</div><div>Right</div></div>.cols { display: grid; gap: 10px; }
.cols > div { background: #d6f4e4; padding: 10px; }
@media min-width = 700px {
.cols { grid-template-columns: 1fr 1fr }- A valid (min-width: 700px) media rule exists
- The media rule sets two columns
35Responsive Design
Read the lessonComplete the mobile-first media query for screens 768px and wider.
@media (: 768px) {
.layout { grid-template-columns: 1fr 1fr; }
}
What does mobile first mean?
Which line makes a responsive grid without media queries?
This page overflows on phones. Use border-box sizing, a fluid container capped at 800px, and a fluid image.
<div class="page"><img class="pic" src="https://picsum.photos/seed/rd/1200/500" alt="Banner" width="1200" height="500"></div>.page { width: 800px; padding: 20px; }
.pic { width: 1200px; }- Page uses border-box
- Page is capped at 800px
- Image is fluid
36Responsive Images
Read the lessonComplete the fluid image rule.
img {
: 100%;
height: auto;
}
Why add height: auto to fluid images?
What's the modern way to make an iframe embed responsive?
Which function provides sharper background images on high-density screens?
The video embed should fill its container at 16:9 with no border, and the photo should shrink to fit without distorting. Fix the CSS.
<div class="col" style="width: 320px">
<iframe class="vid" src="about:blank" title="Video"></iframe>
<img class="pic" src="https://picsum.photos/seed/dbg/800/400" alt="Photo" width="800" height="400">
</div>.vid { width: 560px; height: 315px; }
.pic { width: 800px; }- Video fills the column
- Video is 16:9
- Photo fits and keeps its shape
37Responsive Typography
Read the lessonKeep the paragraph to about 65 characters per line.
.prose {
max-width: 65;
}
ch is based on the width of the "0" character.Why use rem for font sizes?
Which line height suits body text best?
What does text-wrap: balance do?
The heading should use clamp() between 2rem and 3rem, the paragraph should use a unitless 1.6 line height, and lines should be at most 60ch. Fix it.
<h1 class="t">Fresh pasta at home</h1>
<p class="b">Mix flour and eggs, knead for ten minutes, rest, then roll thin.</p>.t { font-size: 6vw; }
.b { line-height: 26px; max-width: 900px; }- Heading uses clamp between 2rem and 3rem
- Heading size is within limits
- Unitless line height
- Lines limited to 60ch
38Responsive Layouts
Read the lessonFit as many 200px columns as possible in each row.
.grid {
display: grid;
grid-template-columns: repeat(, minmax(200px, 1fr));
}
auto-fit creates as many columns as fit.Which property places an element in a named grid area?
What gives a sticky footer on short pages?
Why does .content { flex: 999 1 400px; } work in a sidebar layout?
The layout should stack on phones and show main and aside side by side from 600px using named areas. Fix the area names and the media query.
<div class="pg"><main class="m">Main</main><aside class="a">Aside</aside></div>.pg { display: grid; grid-template-areas: "main" "aside"; }
.m { grid-area: content; }
.a { grid-area: aside; }
@media (max-width: 600px) {
.pg { grid-template-columns: 2fr 1fr; grid-template-areas: "main aside"; }
}- Main uses the main area
- Mobile first media query
- Side by side on wide screens
40Container Queries
Read the lessonMake the wrapper a container that tracks its width.
.wrap {
container-type: ;
}
inline-size tracks the width only.What does a container query measure?
What does 10cqi equal?
When are media queries still the better choice?
The card should go side by side when its wrapper is at least 300px wide. Fix the container and the query.
<div class="w" style="width: 400px"><div class="c"><div>Img</div><div>Text</div></div></div>.c { container-type: size; display: grid; }
@media (min-width: 300px) {
.c { grid-template-columns: 1fr 1fr; }
}- Wrapper is an inline-size container
- Uses @container
- Card is side by side
41:has(), :is() & :where()
Read the lessonStyle a label that contains a checked input.
label:(input:checked) {
font-weight: bold;
}
:has() looks inside the element.What is :has() often called?
What specificity does :where(#main .nav) a have?
Which selector matches list items except the last one?
Cards containing an image should have zero padding, and cards without an image should have a yellow background rgb(254, 252, 232). Fix the selectors.
<div class="card with"><img src="https://picsum.photos/seed/d/100/50" alt="Photo" width="100" height="50"></div>
<div class="card without"><p>Text</p></div>.card { padding: 12px; }
.card img:has() { padding: 0; }
.card:not(img) { background: rgb(254, 252, 232); }- Card with image has no padding
- Card without image keeps padding
- Only the text card is yellow
42Nesting & Logical Props
Read the lessonStyle the card's hover state inside the nested rule.
.card {
:hover { box-shadow: 0 4px 12px rgb(0 0 0 / 0.1); }
}
& refers to .card.What does & h3 mean inside .card { }?
Which logical property sets top and bottom padding?
What does .dark & match when nested inside .card?
The button should turn green on hover (not its children), and use logical padding of 10px top and bottom and 20px start and end. Fix it.
<button class="b">Go</button>.b {
padding-left: 20px;
:hover { background: rgb(22, 163, 74); }
}- Block padding is 10px
- Inline padding is 20px
- Hover uses &
43Dark Mode & Colours
Read the lessonApply these styles only when the device is in dark mode.
@media (: dark) {
:root { --bg: #0f172a; }
}
prefers-color-scheme.What does color-scheme: light dark tell the browser?
What does light-dark(white, black) return in dark mode?
Why name variables --text and --bg rather than --black and --white?
The panel should be dark when data-theme="dark" is set on it, using variables. Fix the theme rule and the variable usage.
<div class="panel" data-theme="dark">Settings</div>.panel { --bg: rgb(255, 255, 255); --text: rgb(17, 24, 39); background: white; color: var(--text); }
.panel.dark { --bg: rgb(15, 23, 42); --text: rgb(226, 232, 240); }- Dark background via the variable
- Light text
- Uses the data-theme attribute
44Transitions
Read the lessonAnimate the opacity over 0.3 seconds.
.photo {
: opacity ease;
}
.photo:hover { opacity: 0.7; }
transition: property duration timing-function.Where should you put the transition property?
Which properties are cheapest to animate smoothly?
The link's colour should fade over 0.25s both on hover and when the mouse leaves. Fix the CSS.
<a class="link" href="#">Read more</a>.link {
color: #4467ff;
}
.link:hover {
color: #f0592b;
transition: colour 0.25s;
}- The transition is on the normal state
45Transforms
Read the lessonRotate the arrow 180 degrees and grow it by 50%.
.arrow {
transform: (180deg) (1.5);
}
transform value.Does transform: scale(2) push neighbouring elements away?
How do you make an element rotate around its top-left corner?
transform-origin moves the pivot point.The badge should be rotated -8deg and scaled to 1.1. Only one transform is working. Fix it.
<span class="badge">HOT</span>.badge {
display: inline-block;
padding: 6px 12px;
background: #e5484d;
color: white;
transform: rotate(-8deg);
transform: scale(1.1);
}- Both rotate and scale are applied
46Animations
Read the lessonApply the "spin" animation for 2 seconds, forever.
.logo {
animation: 2s linear ;
}
Which value keeps the final keyframe's styles after the animation ends?
fill-mode: forwards holds the end state.How many times does the bell ring?
.bell { animation: ring 0.5s ease 3 alternate; }
alternate only changes direction on each cycle.The heart should pulse forever, but nothing moves. Fix the CSS (keep the name "beat").
<div class="heart">❤️</div>.heart {
display: inline-block;
font-size: 40px;
animation: beat;
}
@keyframe beat {
50% { transform: scale(1.3); }
}- Animation name is beat
- Has a duration
- Loops forever
- @keyframes beat is defined
47Shadows & Gradients
Read the lessonAdd a soft shadow that's 4px down with a 10px blur.
.card {
: 0 10px rgb(0 0 0 / 15%);
}
Which property adds a shadow to text?
text-shadow follows the shape of the letters.The button should have a left-to-right gradient from #17a865 to #4467ff and a hard 4px shadow with no blur in #15161b. Fix the CSS.
<button class="cta">Start free</button>.cta {
padding: 12px 22px;
border: 2px solid #15161b;
color: white;
background-color: linear-gradient(90deg, #17a865, #4467ff);
box-shadow: 4px #15161b;
}- Has a linear gradient
- Gradient uses the two colours
- Hard 4px shadow with no blur
48clip-path & Blend Modes
Read the lessonClip the avatar into a circle.
.avatar {
clip-path: (50%);
}
circle(50%) uses half the box as the radius.What happens to the parts of an element outside its clip-path?
Which feature can make soft, faded edges?
What does background-blend-mode blend?
The box should be a triangle pointing up, and the excerpt should fade from black to transparent towards the bottom. Fix the CSS.
<div class="tri" style="width: 100px; height: 100px; background: teal"></div>
<p class="ex">Fading text</p>.tri { clip-path: polygon(50% 0 100% 100% 0 100%); }
.ex { mask-image: linear-gradient(transparent, black); }- Triangle clip is valid
- Mask fades out towards the bottom
49Scroll Snap & Columns
Read the lessonMake the container snap horizontally on every scroll.
.track {
overflow-x: auto;
scroll-snap-type: x ;
}
mandatory always snaps to an item.Which property goes on each slide?
What does scroll-margin-top: 80px do on a heading?
What does columns: 3 200px mean?
The track should snap on the x axis, each slide should snap at its start, and links inside the page should scroll smoothly. Fix it.
<div class="tr"><div class="sl">1</div><div class="sl">2</div></div>.tr { display: flex; overflow-x: auto; }
.sl { flex: 0 0 80%; scroll-snap-type: x mandatory; }
html { scroll-behavior: auto; }- Container snaps on x
- Slides snap at the start
- Smooth scrolling
No exercises match those filters.