How to use the Object-Fit Property in CSS

In web design, properly presenting images and videos across various different devices and screen sizes can be quite a challenge. This is where the CSS object-fit property becomes a game-changer, allowing developers to control how these elements fit into their containers. Object-fit is your go-to solution if you’re looking to maintain aspect ratios, fill specific areas without distortion, or simply manage how your content is displayed. This post will dive deep into the object-fit property, showcasing how it works through practical examples.

What is Object-Fit?

The object-fit property in CSS dictates how <img> or <video> elements should resize to fit their containers. It offers several strategies for resizing content, such as maintaining the original aspect ratio, filling the designated area, or even clipping content to ensure a perfect fit.

The Syntax

The syntax for object-fit is straightforward:

css

object-fit: value; 

Here, value can be one of the following:

  • fill
  • contain
  • cover
  • none
  • scale-down

Exploring Object-Fit Values

Let’s explore what each object-fit value signifies and when to use it:

  • Fill: Stretches the content to fit the container, possibly distorting the aspect ratio. Ideal when the content’s exact dimensions must match the container’s.
  • Contain: Scales the content to fit the container while preserving its aspect ratio. All content is visible, but this may introduce empty space in the container.
  • Cover: Ensures the content fully covers the container, maintaining its aspect ratio. Some content might be clipped to achieve this.
  • None: Displays the content at its original size, not resizing it to fit in the container.
  • Scale-Down: Acts like either none or contain, whichever results in a smaller size, effectively preventing upscaling.

Practical Examples

To illustrate how object-fit can be applied, let’s look at some examples:

Example 1: Using object-fit: cover;

Imagine you have a square container where you want to display an image without distorting its aspect ratio, even if it means clipping some parts of the image.

HTML:

html

<div class=”container”> <img src=”path/to/image.jpg” alt=”Beautiful Landscape”> </div> 

CSS:

css

.container { width: 200px; height: 200px; overflow: hidden; } .container img { width: 100%; height: 100%; object-fit: cover; } 

This setup ensures the image covers the entire container, maintains its aspect ratio, and clips any excess.

Example 2: Ensuring Full Visibility with object-fit: contain;

For situations where you want to ensure the entire image is visible without clipping:

CSS adjustment:

css

.container img { object-fit: contain; } 

Now, the image scales down to fit within the container, ensuring all content is visible, potentially adding empty space around it.

When to Use Object-Fit

Object-fit is incredibly useful in responsive design, where container sizes vary across devices. It helps in:

  • Maintaining aspect ratios of images or videos.
  • Filling background images without stretching.
  • Ensuring visuals look good on devices of all sizes.

Conclusion

The object-fit property is a powerful tool in the CSS toolkit, enabling developers and designers to create visually consistent and appealing websites. By understanding and applying the different values of object-fit, you can ensure that your images and videos always look their best, regardless of the container size or screen resolution.

Experiment with object-fit in your next project and see the difference it makes in your responsive designs. 

Recent Posts