Performance: Object-fit for images and media
This affects how images and media scale and fit within their containers, impacting layout stability and paint performance.
Jump into concepts and practice - no test required
<img src="photo.jpg" class="w-64 h-64 object-cover" alt="Photo">
<img src="photo.jpg" class="w-64 h-64" style="width:16rem; height:16rem;" alt="Photo">
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Image without object-fit in fixed container | Minimal | Multiple on load if image size differs | High due to repaints from layout shifts | [X] Bad |
| Image with object-fit (e.g., object-cover) | Minimal | Single initial reflow | Lower paint cost due to stable layout | [OK] Good |
object-cover do to an image inside a container?object-cover behaviorobject-cover class makes the image fill the container fully, keeping aspect ratio but cropping parts if needed.object-cover does not do.object-contain class makes the image fit inside the container without cropping, preserving aspect ratio.object-stretch and object-scale are not valid Tailwind classes; object-fill stretches the image, which is not fitting without cropping.<div class="w-40 h-40"> <img src="photo.jpg" class="object-contain w-full h-full" alt="Sample" /> </div>
object-contain with full width and height.object-contain effectobject-contain scales the image to fit inside the container fully without cropping, preserving aspect ratio, so some empty space may appear.<img src="pic.jpg" class="object-contain w-full h-full" alt="Pic" />
object-contain behaviorobject-contain scales the image to fit inside the container fully without cropping, preserving aspect ratio, so empty space can appear.w-full and h-full are present, image size does not affect cropping behavior, and object-contain does not crop by design.object-contain fits the whole image inside, no cropping. -> Option C<img src="landscape.jpg" alt="Landscape" class="?" />
object-cover fills container fully with cropping and preserves aspect ratio (no distortion). w-full h-full ensures it fills container size responsively.object-contain fits inside without cropping, object-fill stretches (distorts), object-none shows original size without fitting.