CSS Exam 5: Mastery and Modern Features (10 Composite Exercises)
CSS Exam 5: Mastery and Modern Features
Test your knowledge of the latest CSS techniques that are reshaping how we build the web, like container queries, layers, and logical properties.
Exercise 1
Container Queries
Make an element respond to its container size rather than the viewport:
- Define (.card-container) as a query container (container-type: inline-size).
- Use `@container` to change the child (.card) background to red if the container is under 300px wide.
Solution
.card-container {
container-type: inline-size;
}
@container (max-width: 300px) {
.card {
background-color: red;
}
}
Exercise 2
Cascade Layers
Organize CSS priority using layers:
- Define layers in this order: base, components, utilities.
- Add a rule in `base` that makes text blue.
- Add a rule in `utilities` that makes text red (it should win because it's later).
Solution
@layer base, components, utilities;
@layer base {
p { color: blue; }
}
@layer utilities {
p { color: red; } /* this wins */
}
Exercise 3
Logical Properties
Use logical properties to support LTR/RTL:
- Instead of `margin-left`, use the logical inline start property.
- Instead of `padding-top`, use the logical block start property.
Solution
.box {
margin-inline-start: 20px; /* left in LTR, right in RTL */
padding-block-start: 10px; /* top */
}
Exercise 4
CSS Nesting
Use modern CSS nesting (no preprocessor):
- Inside `.card`, style the child `.title`.
- In the same rule, use `&` to style the card hover state.
Solution
.card {
background: white;
.title {
font-weight: bold;
}
&:hover {
background: gray;
}
}
Exercise 5
Clamp Function
Make font size responsive with clamp():
- Use `clamp()` for the font size.
- Minimum: 1rem.
- Preferred: 2.5vw (responsive to viewport width).
- Maximum: 2rem.
Solution
h1 {
font-size: clamp(1rem, 2.5vw, 2rem);
}
Exercise 6
Subgrid
Make a child inherit the parent's grid:
- You have a parent `.grid` defined as a grid.
- A child `.item` spans 3 columns.
- Make the child a grid whose columns follow the parent's columns using `subgrid`.
Solution
.item {
display: grid;
grid-template-columns: subgrid;
}
Exercise 7
Aspect Ratio
Control box dimensions:
- Make the element always maintain a 16:9 ratio.
- Regardless of its changing width.
Solution
.video-container {
width: 100%;
aspect-ratio: 16 / 9;
}
Exercise 8
Scrollbar Styling
Customize the scrollbar (modern browsers):
- Make the scrollbar thin.
- Set the thumb color to blue and the track to transparent.
Solution
/* Standard property */
html {
scrollbar-width: thin;
scrollbar-color: blue transparent;
}
Exercise 9
Calc Function
Perform calculations for values:
- Set the element width to 100% minus 50px.
- This is useful when combining different units.
Solution
.sidebar {
width: calc(100% - 50px);
}
Exercise 10
:has() (The Parent Selector)
Select the parent based on its child state:
- Select (.card) only if it contains an image (img).
- Give the card a red border.
Solution
.card:has(img) {
border: 2px solid red;
}