bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/CSS/Advanced Styling
CSS•Advanced Styling

CSS object-fit Property

The CSS object-fit property is used to specify how an <img> or <video> should be resized to fit its container.

This property can take one of the following values:

  • fill - This is default. Does not preserve the aspect ratio. The image is resized to fill the container (the image will be stretched or squeezed to fit).
  • cover - Preserves the aspect ratio, and the image fills the container. Cuts overflowing content if needed.
  • contain - Preserves the aspect ratio, and fits the image inside the container, without cutting - leaves empty space if needed.
  • none - The image is not resized.
  • scale-down - the image is scaled down to the smallest version of none or contain.

Using object-fit: fill;

The object-fit: fill; value does not preserve the aspect ratio, and the image is resized to fill the container (the image will be stretched or squeezed to fit):

Example

Formatted code
.image-container {
  width: 200px;
  height: 300px;
  border: 1px solid black;
  margin-bottom: 25px;
} .image-container img {
width: 100%;
height: 100%;
object-fit: fill;
}

Live preview

Using object-fit: cover;

The object-fit: cover; value preserves the aspect ratio, and the image fills the container. The image will be clipped to fit:

Example

Formatted code
.image-container {
  width: 200px;
  height: 300px;
  border: 1px solid black;
  margin-bottom: 25px;
} .image-container img {
width: 100%;
height: 100%;
object-fit: cover;
}

Live preview

Using object-fit: contain;

The object-fit: contain; value preserves the aspect ratio, and fits the image inside the container, without cutting - will leave empty space if needed:

Example

Formatted code
.image-container {
  width: 200px;
  height: 300px;
  border: 1px solid black;
  margin-bottom: 25px;
} .image-container img {
width: 100%;
height: 100%;
object-fit: contain;
}

Live preview

Using object-fit: none;

The object-fit: none; value does not resize or scale the image:

Example

Formatted code
.image-container {
  width: 200px;
  height: 300px;
  border: 1px solid black;
  margin-bottom: 25px;
} .image-container img {
width: 100%;
height: 100%;
object-fit: none;
}

Live preview

Using object-fit: scale-down;

The object-fit: scale-down; value scales the image down to the smallest version of none or contain :

Example

Formatted code
.image-container {
  width: 200px;
  height: 300px;
  border: 1px solid black;
  margin-bottom: 25px;
} .image-container img {
width: 100%;
height: 100%;
object-fit: scale-down;
}

Live preview

Another Example

Here we have two images and we want them to fill the width of 50% of the browser window and 100% of the height of the container.

In the following example we do NOT use object-fit , so when we resize the browser window, the aspect ratio of the images will be destroyed:

Example

In the next example, we use object-fit: cover; , so when we resize the browser window, the aspect ratio of the images is preserved:

CSS object-* Properties

The following table lists the CSS object-* properties:

PropertyDescription
object-fitSpecifies how an <img> or <video> should be resized to fit its container
object-positionSpecifies how an <img> or <video> should be positioned with x/y coordinates inside its "own content box"

Previous

CSS Image Shapes

Next

CSS object-position Property

This chapter

Overview
53

Lessons

154m

Read time

1. CSS Rounded Corners2. Sass Tutorial3. CSS Border Images4. CSS Multiple Backgrounds5. CSS Background Size6. CSS Background Origin7. CSS Background Clip8. CSS Colors9. CSS Color Keywords10. CSS Gradients11. CSS Radial Gradients12. CSS Conic Gradients13. CSS Shadow Effects14. CSS Box Shadow15. CSS Text Effects16. CSS Custom Fonts17. CSS 2D Transforms18. CSS 2D Transform Scale19. CSS 2D Transform Skew / Matrix20. CSS 3D Transforms21. CSS Transitions22. CSS Transition Timing23. CSS Animations24. CSS Animation Timing25. CSS Animation Properties26. CSS Tooltip27. CSS Tooltip Arrows28. CSS Styling Images29. CSS Image Effects30. CSS Image Hover Overlays31. CSS Responsive Modal Images32. CSS Centering Images33. CSS Image Filter Effects34. CSS Image Shapes35. CSS object-fit Property36. CSS object-position Property37. CSS Masking38. CSS Gradient Mask Layers39. CSS SVG Mask Layers40. CSS Styling Buttons41. CSS Button Hover Effects42. CSS Button Groups43. CSS Pagination Styles44. CSS Multiple Columns45. CSS Column Rules46. CSS User Interface47. CSS Variables - var() Function48. CSS Overriding Variables49. CSS Variables and JavaScript50. CSS Variables in Media Queries51. CSS @property Rule52. CSS Box Sizing53. CSS Media Queries

On this page

Using object-fit: fill;Using object-fit: cover;Using object-fit: contain;Using object-fit: none;Using object-fit: scale-down;Another ExampleCSS object-* Properties