TExcelTAD
Home › CSS › CSS Object-fit

CSS Object-fit

When you give an image (or video) a fixed width and height that don't match its natural shape, the browser stretches it by default, distorting it. The object-fit property decides how the content should fill its box instead — like the "fit" and "fill" options for a desktop wallpaper.

The values

Value Behaviour
fill (default) Stretch to fill the box exactly; aspect ratio may be distorted
contain Scale to fit entirely inside the box; empty bars may appear
cover Scale to cover the whole box; overflowing parts are cropped
none Natural size, no scaling; cropped by the box
scale-down Whichever is smaller of none and contain

In the examples below the image is a 300 × 150 SVG (a wide picture) shown in a 120 × 120 square.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; display: flex; flex-wrap: wrap; gap: 12px; }
  figure { margin: 0; text-align: center; font-size: 13px; }
  img { width: 120px; height: 120px; background: #eceff1; border: 2px solid #607d8b; display: block; }
  .fill { object-fit: fill; }
  .contain { object-fit: contain; }
  .cover { object-fit: cover; }
  .none { object-fit: none; }
  .sd { object-fit: scale-down; }
</style>
</head>
<body>
  <figure><img class="fill" alt="" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='150'%3E%3Crect width='300' height='150' fill='%2381d4fa'/%3E%3Crect y='100' width='300' height='50' fill='%2366bb6a'/%3E%3Ccircle cx='60' cy='45' r='25' fill='%23ffeb3b'/%3E%3Ccircle cx='150' cy='85' r='30' fill='%23e53935'/%3E%3Ctext x='150' y='92' font-size='20' text-anchor='middle' fill='white'%3E:)%3C/text%3E%3C/svg%3E"><figcaption>fill</figcaption></figure>
  <figure><img class="contain" alt="" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='150'%3E%3Crect width='300' height='150' fill='%2381d4fa'/%3E%3Crect y='100' width='300' height='50' fill='%2366bb6a'/%3E%3Ccircle cx='60' cy='45' r='25' fill='%23ffeb3b'/%3E%3Ccircle cx='150' cy='85' r='30' fill='%23e53935'/%3E%3Ctext x='150' y='92' font-size='20' text-anchor='middle' fill='white'%3E:)%3C/text%3E%3C/svg%3E"><figcaption>contain</figcaption></figure>
  <figure><img class="cover" alt="" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='150'%3E%3Crect width='300' height='150' fill='%2381d4fa'/%3E%3Crect y='100' width='300' height='50' fill='%2366bb6a'/%3E%3Ccircle cx='60' cy='45' r='25' fill='%23ffeb3b'/%3E%3Ccircle cx='150' cy='85' r='30' fill='%23e53935'/%3E%3Ctext x='150' y='92' font-size='20' text-anchor='middle' fill='white'%3E:)%3C/text%3E%3C/svg%3E"><figcaption>cover</figcaption></figure>
  <figure><img class="none" alt="" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='150'%3E%3Crect width='300' height='150' fill='%2381d4fa'/%3E%3Crect y='100' width='300' height='50' fill='%2366bb6a'/%3E%3Ccircle cx='60' cy='45' r='25' fill='%23ffeb3b'/%3E%3Ccircle cx='150' cy='85' r='30' fill='%23e53935'/%3E%3Ctext x='150' y='92' font-size='20' text-anchor='middle' fill='white'%3E:)%3C/text%3E%3C/svg%3E"><figcaption>none</figcaption></figure>
  <figure><img class="sd" alt="" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='150'%3E%3Crect width='300' height='150' fill='%2381d4fa'/%3E%3Crect y='100' width='300' height='50' fill='%2366bb6a'/%3E%3Ccircle cx='60' cy='45' r='25' fill='%23ffeb3b'/%3E%3Ccircle cx='150' cy='85' r='30' fill='%23e53935'/%3E%3Ctext x='150' y='92' font-size='20' text-anchor='middle' fill='white'%3E:)%3C/text%3E%3C/svg%3E"><figcaption>scale-down</figcaption></figure>
</body>
</html>

Look at the red circle: with fill it becomes an oval; every other value keeps it round.

Tip: object-fit: cover is the go-to choice for thumbnails, avatars and card images — the box stays a consistent shape and the image never distorts.

object-position

When cover or none crops the image, object-position chooses which part stays visible. It works like background-position: keywords (left, top right), percentages or lengths. The default is 50% 50% (centre).

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; display: flex; gap: 14px; }
  figure { margin: 0; text-align: center; font-size: 13px; }
  img { width: 110px; height: 140px; object-fit: cover; border-radius: 10px; display: block; }
  .l { object-position: left; }
  .c { object-position: center; }
  .r { object-position: right; }
</style>
</head>
<body>
  <figure><img class="l" alt="" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='100'%3E%3Crect width='100' height='100' fill='%23e53935'/%3E%3Crect x='100' width='100' height='100' fill='%23fdd835'/%3E%3Crect x='200' width='100' height='100' fill='%231e88e5'/%3E%3C/svg%3E"><figcaption>left</figcaption></figure>
  <figure><img class="c" alt="" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='100'%3E%3Crect width='100' height='100' fill='%23e53935'/%3E%3Crect x='100' width='100' height='100' fill='%23fdd835'/%3E%3Crect x='200' width='100' height='100' fill='%231e88e5'/%3E%3C/svg%3E"><figcaption>center</figcaption></figure>
  <figure><img class="r" alt="" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='100'%3E%3Crect width='100' height='100' fill='%23e53935'/%3E%3Crect x='100' width='100' height='100' fill='%23fdd835'/%3E%3Crect x='200' width='100' height='100' fill='%231e88e5'/%3E%3C/svg%3E"><figcaption>right</figcaption></figure>
</body>
</html>

The source image is a red-yellow-blue strip. Each box shows a different third of it.

A practical card grid

Real photos come in all shapes. Forcing them into equal-height tiles with object-fit: cover keeps the grid tidy. Here the "photos" are a tall and a wide SVG.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  .cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 10px; }
  .card { border-radius: 10px; overflow: hidden; box-shadow: 0 2px 6px rgba(0,0,0,.2); }
  .card img { width: 100%; height: 110px; object-fit: cover; display: block; }
  .card p { margin: 0; padding: 6px 8px; font-size: 14px; }
</style>
</head>
<body>
  <div class="cards">
    <div class="card"><img alt="" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100' height='300'%3E%3Crect width='100' height='300' fill='%237e57c2'/%3E%3Ccircle cx='50' cy='150' r='35' fill='%23fff176'/%3E%3C/svg%3E"><p>Tall source</p></div>
    <div class="card"><img alt="" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='100'%3E%3Crect width='400' height='100' fill='%2326a69a'/%3E%3Ccircle cx='200' cy='50' r='35' fill='%23ffccbc'/%3E%3C/svg%3E"><p>Wide source</p></div>
    <div class="card"><img alt="" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='200' height='200'%3E%3Crect width='200' height='200' fill='%23ef6c00'/%3E%3Ccircle cx='100' cy='100' r='50' fill='%23fff'/%3E%3C/svg%3E"><p>Square source</p></div>
  </div>
</body>
</html>

Where object-fit applies

object-fit works on replaced elements — img, video, canvas, iframe, embed. For CSS background images, use background-size: cover | contain and background-position, which do the same job.

Element type Use
<img>, <video> object-fit, object-position
background-image background-size, background-position

Test yourself

Which object-fit value fills the box completely while keeping the aspect ratio, cropping if needed?

  1. fill
  2. contain
  3. cover
  4. none

What is the default value of object-fit?

  1. contain
  2. fill
  3. cover
  4. auto

Which property chooses which part of a cropped image stays visible?

  1. object-align
  2. object-position
  3. background-origin
  4. image-focus