css · level 2 · unit 4 · lesson 3
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
, and give sizes limits.
min() picks the smallest of its values. width: min(100%, 500px) is the full width on a phone, but never more than 500px. max() picks the largest, so it sets a floor instead.
clamp() does both: clamp(smallest, ideal, largest). font-size: clamp(24px, 6vw, 48px) grows with the screen, but never drops below 24px or climbs above 48px.
Sizes like these change smoothly on every screen, instead of jumping at a breakpoint. They often save you a media query.
<h1>Big news</h1>
<div class="box">The library opens on Sundays from next week.</div> h1 {
font-size: clamp(24px, 6vw, 48px);
}
.box {
width: min(100%, 500px);
padding: 16px;
background-color: aliceblue;
} Press show my page to see it here.
The box fills a phone's width, but stops at 500px on a wide screen.
guess first
What is the smallest this heading can be?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
<h1 class="title">Sports day</h1> .title {
font-size: clamp(20px, 4vw, 40px);
}
your turn
Your turn. A heading that grows with the screen.
Give the heading a clamp() font size: never below 28px, never above 56px, with an ideal in vw between them.
the page you’re styling
<h1>Summer fair</h1>
<p>Saturday on the village green.</p>your css
Press show my page to see it here.
- a
clamp()from 28px to 56pxnot checked yet - the heading stays in rangenot checked yet
fix it
The card should never grow past 400px. Fix it!
the page you’re styling
<div class="card">
<h2>Book club</h2>
<p>This month we are reading a story about a brave mouse.</p>
</div>your css
Press show my page to see it here.
min()stops the card at 400pxnot checked yet
- On a wide screen the card is far too wide. Which function picks the larger value?
max()picks the largest value, so the card is never narrower than 400px.- Change
maxtomin, then press run.
nailed it
your sizes stretch, but only so far. :D
min()picks the smallest value, so it sets a ceiling.max()picks the largest value, so it sets a floor.clamp(smallest, ideal, largest)keeps a value in range.
new words in your collection
- min()
- max()
- clamp()
This lesson isn’t saved. A free account remembers every lesson you finish.