css · level 2 · unit 7 · lesson 3
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
fills a frame without squashing the picture.
Give a picture a width and height that do not match its shape and it stretches, like a face in a funfair mirror. object-fit fixes that.
fills the whole box and trims the edges that do not fit. shows the whole picture and leaves empty space instead. The default, fill, stretches.
For card photos, width: 100% plus a fixed height and object-fit: cover gives every card the same neat frame, whatever shape its photo is.
<div class="card">
<img class="thumb" src="dog.svg" alt="Rex the dog">
<h3>Rex</h3>
</div> .card {
width: 220px;
}
.thumb {
width: 100%;
height: 160px;
object-fit: cover;
border-radius: 8px;
} Press show my page to see it here.
Change cover to fill and watch Rex stretch out of shape.
guess first
contain in a long, thin frame. What do you see?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
<img src="cat.svg" alt="A cat"> img {
width: 300px;
height: 100px;
object-fit: contain;
background-color: whitesmoke;
}
your turn
Your turn. Neat round avatars.
Make the avatars fill their round frames completely, trimming the edges instead of squashing.
the page you’re styling
<img class="avatar" src="cat.svg" alt="Pickles">
<img class="avatar" src="dog.svg" alt="Rex">your css
Press show my page to see it here.
- the avatars fill their framesnot checked yet
fix it
The photo still looks squashed. Fix it!
the page you’re styling
<div class="card">
<img src="tree.svg" alt="A tall pine tree">
<h3>Pine forest walk</h3>
</div>your css
Press show my page to see it here.
- the photo fills its framenot checked yet
- Which element is the picture: the card, or the
<img>inside it? object-fitonly works on the picture itself.- Move
object-fit: cover;into the.card imgrule, then press run.
nailed it
your pictures fit their frames. :D
object-fit: coverfills the box and trims the edges.containshows the whole picture, with empty space.- Put
object-fiton the picture, not its parent.
new words in your collection
- object-fit
- cover
- contain
This lesson isn’t saved. A free account remembers every lesson you finish.