Performance: Object fit
Controls how replaced elements like images or videos fit within their container, impacting layout and paint performance.
Jump into concepts and practice - no test required
img { width: 100%; height: 100%; object-fit: cover; }img { width: 100%; height: auto; }| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Using width:auto and height:auto without object-fit | Low | Multiple reflows on image load | High due to layout shifts | [X] Bad |
| Using fixed container with object-fit: cover | Low | Single reflow | Low paint cost | [OK] Good |
object-fit: cover; do to an image inside a container?object-fit: cover; behaviorcontain which fits without cropping, cover crops to fill fully.object-fit: contain; fits the entire image inside the container without cropping or distortion.fill stretches, stretch is invalid, and crop is not a valid value.<div style="width: 200px; height: 100px;"> <img src="image.jpg" style="width: 100%; height: 100%; object-fit: contain;"> </div>
object-fit: contain; effectimg {
width: 100%;
height: 100%;
object-fit: fill;
}object-fit: fill;object-fit: cover; to fix distortioncover fills the container while preserving aspect ratio, cropping if needed, preventing distortion.object-fit to cover. -> Option Avideo {
width: 100%;
height: 100%;
object-fit: ?
object-position: ?
}cover fills the container fully, preserving aspect ratio, cropping if needed.object-position: center; ensures cropping keeps the center area visible.