object-fit: cover?Consider an image inside a fixed 200x200 pixel container. The image is larger and rectangular. The CSS class object-cover is applied to the image.
What will the image look like inside the container?
<div class="w-50 h-50 border border-gray-400"> <img src="https://via.placeholder.com/400x300" alt="Sample" class="object-cover w-full h-full" /> </div>
Think about how cover behaves: it fills the container while keeping the image's shape, even if some parts get cut off.
The object-cover class applies object-fit: cover;. This makes the image fill the container completely, preserving its aspect ratio. If the image's shape doesn't match the container, parts of the image will be cropped.
object-fit: contain?You want an image to scale down to fit inside its container without cropping, showing the entire image with possible empty space.
Which Tailwind CSS class achieves this?
Remember, contain keeps the whole image visible inside the container.
The object-contain class applies object-fit: contain; which scales the image to fit inside the container while preserving aspect ratio. It may leave empty space if the aspect ratios differ.
object-fit: scale-down on an image?Given an image inside a container, what is the behavior of object-fit: scale-down?
Think about when the image is smaller or larger than the container.
object-fit: scale-down; compares none and contain behaviors and applies whichever results in a smaller image. So it scales down if needed but never scales up.
object-fit remain accessible?You use object-fit to style images on your webpage. What is the best practice to keep images accessible?
Think about screen readers and users who rely on them.
Images styled with object-fit should always have meaningful alt text describing their content. If images are interactive, they must be keyboard accessible. Hiding images or removing alt harms accessibility.
You want a square container that adjusts size with the screen width. Inside it, an image should fill the container completely, cropping if needed, and keep its aspect ratio.
Which combination of Tailwind CSS classes achieves this?
Remember aspect-square keeps a square ratio and object-cover fills the container cropping if needed.
The aspect-square class ensures the container is always square and responsive. The image with object-cover w-full h-full fills the container fully, cropping excess parts while preserving aspect ratio.