CSS Object Fit for Media
Control how images and video fill containers using CSS object-fit and object-position for responsive media display.
Code
.cover {
width: 100%;
height: 300px;
object-fit: cover;
object-position: center top;
}
.contain {
object-fit: contain;
max-width: 100%;
}CSS Object Fit for Media
Control how images and video fill containers using CSS object-fit and object-position for responsive media display.
Code
.cover {
width: 100%;
height: 300px;
object-fit: cover;
object-position: center top;
}
.contain {
object-fit: contain;
max-width: 100%;
}
Explanation
object-fit controls how an image or video fills its box (cover crops, contain letterboxes) and object-position shifts the focal point. Expected behavior: the element’s allocated box stays fixed while the media is scaled inside it. Limitation: object-fit does nothing unless the element has an explicit width and height, so intrinsic-only sizing will not show the effect.
Interactive Tools for This Topic
Aspect Ratio CalculatorSimplify a width-to-height ratio, show its decimal value, and derive a matching height for a new width.Border Radius PreviewPreview four-corner border-radius values on a live box and copy the generated shorthand border-radius CSS declaration.Box Shadow GeneratorBuild CSS box-shadow values with offset, blur, spread, color, opacity, and inset controls, with a live preview of the result.CSS Color Name LookupTranslate between CSS keyword color names and hex values with RGB and HSL conversions, exact name matching, and nearest-color suggestions.
Report a problem with this exampleOpens a pre-filled issue on GitHub.