MDN self-hosted library · e01.org

How to fill a box with an image without distorting it

In this guide you can learn a technique for causing an HTML image to completely fill a box.

no runnable cells learn-faq upstream source

In this guide you can learn a technique for causing an HTML image to completely fill a box.

Using object-fit

When you add an image to a page using the HTML img element, the image will maintain the size and aspect ratio of the image file, or that of any HTML width or height attributes. Sometimes you would like the image to completely fill the box that you have placed it in. In that case you first need to decide what happens if the image is the wrong aspect ratio for the container.

  1. The image should completely fill the box, retaining aspect ratio, and cropping any excess on the side that is too big to fit.
  2. The image should fit inside the box, with the background showing through as bars on the too-small side.
  3. The image should fill the box and stretch, which may mean it displays at the wrong aspect ratio.

The object-fit property makes each of these approaches possible. In the example below you can see how different values of object-fit work when using the same image. Select the approach that works best for your design.

```html live-sample___object-fit

a colorful hot air balloon against a clear sky
a colorful hot air balloon against a clear sky
a colorful hot air balloon against a clear sky

```css live-sample___object-fit
.wrapper {
  height: 200px;
  display: flex;
  gap: 20px;
}

.box {
  border: 5px solid black;
}

.box img {
  width: 100%;
  height: 100%;
}

.box1 img {
  object-fit: cover;
}

.box2 img {
  object-fit: contain;
}

.box3 img {
  object-fit: fill;
}