css · level 2 · unit 1 · lesson 4
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
sets the starting size; , the squeeze.
flex-basis is the size a flex child starts at, like flex-basis: 200px. Then free space is shared out with flex-grow, or taken away when the row is too small.
Taking away is flex-shrink. Its default is 1, so every child squeezes. Set flex-shrink: 0 on anything that must keep its size, like a photo.
The flex shorthand sets all three in this order: grow, shrink, basis. flex: 0 0 64px means never grow, never shrink, start at 64px.
<div class="chat">
<img class="face" src="cat.svg" alt="Pickles">
<p>Has anyone seen my tuna? I left it right here by the sofa.</p>
</div> .chat {
display: flex;
gap: 12px;
align-items: center;
}
.face {
flex: 0 0 64px;
} Press show my page to see it here.
On a narrow phone the words wrap onto more lines, but the face stays 64px wide.
guess first
flex: 1 on every card. How do they share the row?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
<div class="row">
<div class="card">Hi</div>
<div class="card">Hello there</div>
<div class="card">Good morning, everyone</div>
</div> .row {
display: flex;
gap: 8px;
}
.card {
flex: 1;
padding: 12px;
background-color: lightpink;
}
your turn
Your turn. Stop the logo squashing.
Fill in the logo's flex shorthand so it never grows, never shrinks and starts at 48px.
the page you’re styling
<div class="header">
<img class="logo" src="logo.svg" alt="Gwecode">
<h2>Welcome back to your coding garden</h2>
</div>your css
Press show my page to see it here.
- the logo never growsnot checked yet
- the logo never shrinksnot checked yet
- the logo starts at 48pxnot checked yet
fix it
The price should start at 80px. Fix it!
the page you’re styling
<div class="item">
<p>Large pepperoni pizza with extra cheese and olives</p>
<p class="price">£12.50</p>
</div>your css
Press show my page to see it here.
- the price never squashesnot checked yet
- the price starts at 80pxnot checked yet
- The browser skipped one declaration. Which number is missing something?
flex-growandflex-shrinkare plain numbers, butflex-basisis a size.- Write
80pxin place of80, then press run.
nailed it
you control how flex children size up. :D
flex-basisis where a flex child starts.flex-shrink: 0stops a child squashing.flex: grow shrink basissets all three at once.
new words in your collection
- flex-basis
- flex-shrink
This lesson isn’t saved. A free account remembers every lesson you finish.