Ad space (banner)
🎨HTML & CSS Lessons
Lesson 38 / 50

Adjusting How an Image Displays with object-fit

In this lesson you'll learn how to adjust how an image is displayed using object-fit, so you can display images of different aspect ratios at a consistent size. This is for people searching "CSS object-fit usage."

object-fit specifies how an image or video should be fit into a fixed-size frame. cover scales it up to fill the frame completely, cropping with no leftover space, while contain shrinks it so the whole thing stays visible.

The sample code fits a wide image into a 150x150px square frame using object-fit: cover;. Confirm how the image scales up to fill the frame while keeping its aspect ratio, with any overflowing part automatically cropped.

A common beginner stumbling block is that without specifying object-fit, an image gets stretched to fit the frame ignoring its aspect ratio, resulting in an unnaturally distorted look. Understanding the purpose — that object-fit is used to prevent this distortion — makes the usage easier to remember.

Even on a list page mixing images of different aspect ratios, this setting lets you unify every thumbnail into the same size frame for a tidy appearance. This is a setting frequently used on things like an e-commerce site's product listing.

HTML & CSS
OUTPUT

💡 The preview runs entirely on this page — nothing is sent externally.

Ad space (banner)
Ad space (in-article)