Photos rarely have the exact shape your layout needs. A product grid wants square thumbnails, a banner wants a wide strip, and a profile wants a circle. Squeezing an image into the wrong shape stretches it badly. CSS solves this with object-fit, which crops or fits an image inside its box, and aspect-ratio, which sets the box’s shape. On top of that, filters adjust colour and blur, and clip-path cuts images into shapes, all without editing the image file.
In this lesson you will learn
object-fit:fill,containandcover- Choosing which part stays visible with
object-position - Fixing an image’s shape with
aspect-ratio - Colour and blur effects with
filter - Round avatars and custom shapes with
border-radiusandclip-path
You should know HTML images and the box model.
Fitting Images into a Box
The examples use a 400 × 200 picture of hills, twice as wide as it is tall. In the code it is called hills.jpg; the live result uses a small built-in drawing so the demo works anywhere.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>object-fit</title>
<style>
body { font-family: sans-serif; margin: 12px; display: flex; gap: 12px; flex-wrap: wrap; font-size: 13px; }
figure { margin: 0; }
img { width: 120px; height: 120px; border: 2px solid #455a64; background: #eceff1; }
.fill { object-fit: fill; } /* default: stretched to the box */
.contain { object-fit: contain; } /* whole image visible, gaps around it */
.cover { object-fit: cover; } /* fills the box, edges cropped */
.right { object-fit: cover; object-position: right; } /* crop from the left side instead */
.wide { width: 200px; height: auto; aspect-ratio: 16 / 9; object-fit: cover; }
</style>
</head>
<body>
<!-- The photo is 400 x 200 pixels: twice as wide as it is tall -->
<figure><img class="fill" src="hills.jpg" alt="Hills under a sun"><figcaption>fill</figcaption></figure>
<figure><img class="contain" src="hills.jpg" alt="Hills under a sun"><figcaption>contain</figcaption></figure>
<figure><img class="cover" src="hills.jpg" alt="Hills under a sun"><figcaption>cover</figcaption></figure>
<figure><img class="right" src="hills.jpg" alt="Hills under a sun"><figcaption>cover + position right</figcaption></figure>
<figure><img class="wide" src="hills.jpg" alt="Hills under a sun"><figcaption>aspect-ratio 16 / 9</figcaption></figure>
</body>
</html>
Result
- Each square box is 120 × 120 pixels, but the picture is wide.
object-fitdecides how the picture fills the box, much likebackground-sizedoes for background images. fillis the default. It stretches the picture to the box, distorting it: the sun becomes an oval.containshows the whole picture at its true shape and leaves empty bands where it does not reach. Use it for logos and diagrams, where nothing may be cut off.coverfills the whole box at the true shape and crops what does not fit. It is the usual choice for thumbnails, cards and banners.object-positionchooses which part survives the crop. The default iscenter;rightkeeps the right side with the sun.aspect-ratio: 16 / 9withheight: autogives the box a fixed shape that scales with its width. The browser can also reserve the space before the image loads, so the page does not jump.
Filters and Shapes
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Filters and clip-path</title>
<style>
body { font-family: sans-serif; margin: 12px; display: flex; gap: 12px; flex-wrap: wrap; font-size: 13px; }
figure { margin: 0; text-align: center; }
img { width: 110px; height: 110px; object-fit: cover; display: block; transition: filter .3s; }
.gray { filter: grayscale(100%); }
.gray:hover { filter: none; } /* colour returns on hover */
.blur { filter: blur(3px); }
.bright { filter: brightness(1.3) contrast(1.2); }
.sepia { filter: sepia(80%); }
.round { border-radius: 50%; } /* circular avatar */
.hex { clip-path: polygon(25% 0, 75% 0, 100% 50%, 75% 100%, 25% 100%, 0 50%); }
</style>
</head>
<body>
<figure><img src="hills.jpg" alt="Hills under a sun"><figcaption>original</figcaption></figure>
<figure><img class="gray" src="hills.jpg" alt="Hills under a sun"><figcaption>grayscale (hover me)</figcaption></figure>
<figure><img class="blur" src="hills.jpg" alt="Hills under a sun"><figcaption>blur</figcaption></figure>
<figure><img class="bright" src="hills.jpg" alt="Hills under a sun"><figcaption>brightness + contrast</figcaption></figure>
<figure><img class="sepia" src="hills.jpg" alt="Hills under a sun"><figcaption>sepia</figcaption></figure>
<figure><img class="round" src="hills.jpg" alt="Hills under a sun"><figcaption>border-radius 50%</figcaption></figure>
<figure><img class="hex" src="hills.jpg" alt="Hills under a sun"><figcaption>clip-path hexagon</figcaption></figure>
</body>
</html>
Result
filterapplies effects in the browser:grayscale(),blur(),brightness(),contrast(),sepia(),saturate()and more. Several can be combined in one declaration, separated by spaces.- Filters can be transitioned. The grayscale picture turns back to colour on hover with
transition: filter .3s, a common effect for logo walls and galleries. border-radius: 50%on a square image makes a circle. Combine it withobject-fit: coverso that faces in rectangular photos are not squashed.clip-pathcuts the element to a shape.polygon()takes a list of corner points as percentages, here forming a hexagon.circle(),ellipse()andinset()are also available.
Image Styling Reference
| Property | Example | Result |
|---|---|---|
object-fit | cover | Fill the box, crop the rest |
object-position | top | Keep the top of the image when cropping |
aspect-ratio | 1 / 1 | Square box at any width |
filter | grayscale(100%) | Black and white |
filter | drop-shadow(0 4px 6px rgba(0,0,0,.3)) | Shadow that follows transparent shapes |
border-radius | 50% | Circle (on a square) |
clip-path | circle(40%) | Circular cut-out |
Common Mistakes
- Setting both width and height on an image without
object-fit, which stretches it. - Using
coveron logos or charts, which crops important parts. Usecontain. - Relying on filters for the main image on slow devices. Heavy blur on large images can make scrolling less smooth.
- Forgetting
alttext. Styling changes nothing about what the image means.
Practice Exercises
Exercise 1. Make every image in .gallery a square thumbnail that fills its grid cell.
Show answer
.gallery img {
width: 100%;
aspect-ratio: 1 / 1;
object-fit: cover;
display: block;
}
Exercise 2. Darken a banner image to 60% brightness so white text on top stays readable.
Show answer
.banner img { filter: brightness(60%); }
A dark gradient overlay, shown in CSS backgrounds and gradients, is another way.
Frequently Asked Questions
Does object-fit work on videos?
Yes. object-fit and object-position work on any replaced element, including <video>. object-fit: cover is often used for full-width background videos.
Does object-fit: cover make the file smaller?
No. The full image is still downloaded and only cropped on screen. To save data, serve smaller files with srcset, covered in HTML audio, video and responsive images.
Summary
object-fit: covercrops to fill;containfits without cropping.object-positionpicks which part stays visible.aspect-ratiogives boxes a fixed shape and prevents layout jumps.filter,border-radiusandclip-pathstyle images without editing them.
