Reading history

CSS object-fit, aspect-ratio, Filters and clip-path

6 min read 0 views

Style images with CSS: object-fit cover and contain, object-position, aspect-ratio, grayscale and blur filters, round avatars and clip-path shapes.

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, contain and cover
  • 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-radius and clip-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-fit decides how the picture fills the box, much like background-size does for background images.
  • fill is the default. It stretches the picture to the box, distorting it: the sun becomes an oval.
  • contain shows 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.
  • cover fills the whole box at the true shape and crops what does not fit. It is the usual choice for thumbnails, cards and banners.
  • object-position chooses which part survives the crop. The default is center; right keeps the right side with the sun.
  • aspect-ratio: 16 / 9 with height: auto gives 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

  • filter applies 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 with object-fit: cover so that faces in rectangular photos are not squashed.
  • clip-path cuts the element to a shape. polygon() takes a list of corner points as percentages, here forming a hexagon. circle(), ellipse() and inset() are also available.

Image Styling Reference

PropertyExampleResult
object-fitcoverFill the box, crop the rest
object-positiontopKeep the top of the image when cropping
aspect-ratio1 / 1Square box at any width
filtergrayscale(100%)Black and white
filterdrop-shadow(0 4px 6px rgba(0,0,0,.3))Shadow that follows transparent shapes
border-radius50%Circle (on a square)
clip-pathcircle(40%)Circular cut-out

Common Mistakes

  1. Setting both width and height on an image without object-fit, which stretches it.
  2. Using cover on logos or charts, which crops important parts. Use contain.
  3. Relying on filters for the main image on slow devices. Heavy blur on large images can make scrolling less smooth.
  4. Forgetting alt text. 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: cover crops to fill; contain fits without cropping.
  • object-position picks which part stays visible.
  • aspect-ratio gives boxes a fixed shape and prevents layout jumps.
  • filter, border-radius and clip-path style images without editing them.

Next: SVG in HTML: Shapes, Icons and Charts.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *