Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Properties

CSS object-fit Property

Posted in CSS Tutorial
Updated on Jul 26, 2024
By Mari Selvan
👁️ 27 - Views
⏳ 4 mins
💬 1 Comment
CSS object-fit Property

Photo Credit to CodeToFun

🙋 Introduction

The object-fit property in CSS is used to control how the content of a replaced element, such as an <img> or <video>, is resized to fit its container.

This property is particularly useful for maintaining aspect ratios and preventing distortion when images or videos are scaled to fit a specific size.

💡 Syntax

The syntax for the object-fit property is straightforward. It can be applied to any replaced element, such as an image, video, or iframe.

Syntax
Copied
Copy To Clipboard
element {
  object-fit: value;
}

Here, value determines how the content is resized to fit the container.

🎛️ Default Value

The default value of the object-fit property is fill. This means the content will stretch to fill the entire container, which can sometimes result in distortion if the aspect ratio of the content doesn't match the container.

🏠 Property Values

ValueDescription
fillThe content is resized to fill the container. If necessary, the content will be stretched to fit, which may alter the aspect ratio.
containThe content is scaled to maintain its aspect ratio while fitting within the container. The entire content will be visible, and the aspect ratio will be preserved.
coverThe content is scaled to maintain its aspect ratio while filling the container. The content may be clipped to fit, but the aspect ratio will be preserved.
noneThe content is not resized. The image is displayed at its original size.
scale-downThe content is sized as if none or contain were specified, whichever results in a smaller size.

📄 Example

In this example, we'll use the object-fit property to display an image in different ways within a fixed-size container.

index.html
Copied
Copy To Clipboard
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS object-fit Example</title>
  <style>
    .container {
      width: 300px;
      height: 200px;
      border: 1px solid #ddd;
      margin-bottom: 20px;
    }

    .container img {
      width: 100%;
      height: 100%;
      object-fit: cover; /* Change to fill, contain, none, or scale-down for different results */
    }
  </style>
</head>
<body>
  <h1>Image with Object-Fit Property</h1>
  <div class="container">
    <img src="example.jpg" alt="Example Image">
  </div>
</body>
</html>

In the above example, the image will be resized to cover the entire container, maintaining its aspect ratio. You can change the value of object-fit to see different behaviors.

🖥️ Browser Compatibility

The object-fit property is supported in most modern browsers, including the latest versions of Chrome, Firefox, Safari, Edge, and Opera. However, it is always a good practice to test your website across different browsers to ensure compatibility.

🎉 Conclusion

The object-fit property provides a simple yet powerful way to control how content like images and videos are displayed within their containers.

By understanding and utilizing the various values of this property, you can ensure that your media elements look great across different screen sizes and layouts, enhancing the visual appeal and user experience of your website.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy