Holding a shape in CSS
One property replaces the padding-top percentage trick everyone used to memorise.
.thumb {
inline-size: 100%;
aspect-ratio: 16 / 9;
object-fit: cover;
object-position: 50% 30%;
border-radius: 0.5rem;
}
.avatar {
inline-size: 3rem;
aspect-ratio: 1;
object-fit: cover;
border-radius: 50%;
}
.embed {
aspect-ratio: 4 / 3;
inline-size: 100%;
border: 0;
}
How it works
aspect-ratiosets height from width, or the other way round.object-fitdecides how the image fills the box it was given.object-positionmoves the visible part when it has to crop.
Keywords and builtins used here
avatarborderembedremthumb
The run, in numbers
- Lines
- 20
- Characters to type
- 270
- Tokens
- 72
- Three-star pace
- 85 tpm
At the three-star pace of 85 tokens a minute, this run takes about 51 seconds.
Step 2 of 3 in Sizing, step 15 of 24 in Flexbox & grid.