CSS · Layout and visual systems
How does the aspect-ratio property work?
Short answer
aspect-ratio sets a preferred width-to-height ratio for a box. When one dimension is auto, the other can be derived from the ratio, which replaces padding-based ratio hacks.
aspect-ratio accepts a ratio such as 16 / 9 or 1. The box uses it as a preferred ratio when at least one dimension is auto. If both width and height are explicitly set, the ratio has no room to influence the result.
.thumb {
aspect-ratio: 16 / 9;
width: 100%;
object-fit: cover;
}
.square {
aspect-ratio: 1;
width: 4rem;
}Where it helps
- Reserving space for images and videos before they load
- Making square avatars or icon tiles
- Creating fixed-ratio media containers without padding-top hacks
- Combining with object-fit to control how replaced content fills the box
- Using auto in the ratio so the box can follow content in one dimension
For images, the ratio can also come from the intrinsic dimensions of the file. Setting aspect-ratio explicitly is still useful when the layout needs a consistent ratio regardless of the source image.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What happens when both width and height are set?
- 2How does object-fit interact with aspect-ratio?
- 3Why is aspect-ratio better than the padding-top hack?
- 4How does aspect-ratio help prevent layout shift?
Say it in the interview
aspect-ratio sets a preferred width-to-height ratio. If one dimension is auto, the other is derived from it, which is perfect for media boxes and avatars. It replaces the old padding-top percentage hack and pairs well with object-fit. It is still a preference, so explicit sizes, min and max constraints, or content can override it.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live CSS mock interview and get scored feedback on depth, clarity and confidence.