</> Code Corner

CSS · CSS Advanced · Lesson 52

CSS Object-fit

Fit images and videos into their box.

The object-fit property specifies how an <img> or <video> is resized to fit its container.

Example

<style>
img { width: 200px; height: 140px; border: 2px solid #333; margin: 4px; }
.fill { object-fit: fill; }
.contain { object-fit: contain; }
.cover { object-fit: cover; }
</style>
<img class="fill" src="https://picsum.photos/id/1003/400/200" alt="fill">
<img class="contain" src="https://picsum.photos/id/1003/400/200" alt="contain">
<img class="cover" src="https://picsum.photos/id/1003/400/200" alt="cover">
Value Effect
fill Default — stretched to fill, may distort
contain Keeps aspect ratio, fits inside
cover Keeps aspect ratio, fills the box and clips
none Not resized
scale-down Smaller of none or contain

Keep reading “CSS Object-fit”

Sign up to read the full lesson, try every example in the live editor, and take the 5-question quiz.

Chat with us 👋