CSS Properties
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.
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
Value | Description |
---|---|
fill | The content is resized to fill the container. If necessary, the content will be stretched to fit, which may alter the aspect ratio. |
contain | The 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. |
cover | The 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. |
none | The content is not resized. The image is displayed at its original size. |
scale-down | The 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.
<!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:
Author
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
If you have any doubts regarding this article (CSS object-fit Property), please comment here. I will help you immediately.