Object-Fit
The object-fit
property in CSS dictates how the content of a replaced element, such as an <img>
or <video>
, should be resized to fit its container. This property is essential for maintaining the aspect ratio of media without distortion. It provides more control over the display of images and videos within specific dimensions.
Example 1: object-fit: fill
.container {
width: 200px;
height: 300px;
border: 2px solid #ccc;
}
.container img {
width: 100%;
height: 100%;
object-fit: fill; /* Stretches the image to fit the container, potentially distorting it */
}
Explanation The fill
value is the default. It stretches the image to completely fill the container's dimensions, which can lead to the image appearing squished or stretched if the container's aspect ratio differs from the image's.
Example 2: object-fit: contain
.container {
width: 200px;
height: 300px;
border: 2px solid #ccc;
background-color: #f0f0f0;
}
.container img {
width: 100%;
height: 100%;
object-fit: contain; /* Scales the image to fit within the container while maintaining its aspect ratio */
}
Explanation The contain
value scales the image up or down to fit inside the container while preserving its original aspect ratio. This may result in letterboxing (empty space) if the aspect ratios of the image and container do not match.
Example 3: object-fit: cover
.container {
width: 200px;
height: 300px;
border: 2px solid #ccc;
}
.container img {
width: 100%;
height: 100%;
object-fit: cover; /* Scales the image to cover the entire container, cropping as needed */
}
Explanation With cover
, the image is scaled to completely cover the container, also while maintaining its aspect ratio. If the aspect ratios differ, the image will be clipped to fit.
Example 4: object-fit: none
.container {
width: 200px;
height: 300px;
border: 2px solid #ccc;
overflow: hidden; /* Hides the parts of the image that overflow the container */
}
.container img {
width: 100%;
height: 100%;
object-fit: none; /* Does not resize the image */
}
Explanation The none
value does not resize the image at all. The image will be displayed at its original size and may be clipped if it is larger than the container.
Example 5: object-fit: scale-down
.container {
width: 200px;
height: 300px;
border: 2px solid #ccc;
background-color: #f0f0f0;
}
.container img {
width: 100%;
height: 100%;
object-fit: scale-down; /* Compares 'none' and 'contain' and chooses the smaller size */
}
Explanation scale-down
selects the smaller of either the none
or contain
behavior. This means the image will either be its natural size or scaled down to fit if it's larger than the container, preventing it from ever being scaled up.
Object-Position
The object-position
property works in conjunction with object-fit
to specify the alignment of the element's content within its container. This is particularly useful when using object-fit: cover
or object-fit: contain
to control which part of the image is visible.
Example 1: object-position with keywords
.container {
width: 300px;
height: 200px;
border: 2px solid #ccc;
}
.container img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: top right; /* Aligns the image to the top right corner of the container */
}
Explanation Using keywords like top
, bottom
, left
, right
, and center
, you can control the alignment of the image. Here, the image is positioned so that its top right corner is visible within the container.
Example 2: object-position with percentages
.container {
width: 300px;
height: 200px;
border: 2px solid #ccc;
}
.container img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: 25% 75%; /* Positions the image with 25% from the left and 75% from the top */
}
Explanation Percentage values for object-position
allow for precise control. A value of 25% 75%
aligns the point at 25% horizontally and 75% vertically within the image with the corresponding point in the container.