Object-fit helps images or videos fill their space nicely without stretching or cutting important parts.
Object-fit for images and media in Tailwind
Start learning this pattern below
Jump into concepts and practice - no test required
class="object-contain" class="object-cover" class="object-fill" class="object-none" class="object-scale-down"
Use these classes on <img> or <video> tags.
They control how the media fits inside its container.
<img src="photo.jpg" class="object-cover w-48 h-32" alt="Cover example">
<img src="photo.jpg" class="object-contain w-48 h-32" alt="Contain example">
<img src="photo.jpg" class="object-fill w-48 h-32" alt="Fill example">
<img src="photo.jpg" class="object-none w-48 h-32" alt="None example">
This page shows four images with different object-fit styles using Tailwind CSS. Each image is inside a fixed size box (width 12rem, height 8rem). The border helps see the container edges.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Object-fit Tailwind Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="p-6"> <h1 class="text-xl font-semibold mb-4">Object-fit examples with Tailwind CSS</h1> <div class="flex gap-6"> <div> <p class="mb-2">object-cover</p> <img src="https://via.placeholder.com/400x200" alt="Cover" class="object-cover w-48 h-32 border"> </div> <div> <p class="mb-2">object-contain</p> <img src="https://via.placeholder.com/400x200" alt="Contain" class="object-contain w-48 h-32 border"> </div> <div> <p class="mb-2">object-fill</p> <img src="https://via.placeholder.com/400x200" alt="Fill" class="object-fill w-48 h-32 border"> </div> <div> <p class="mb-2">object-none</p> <img src="https://via.placeholder.com/400x200" alt="None" class="object-none w-48 h-32 border"> </div> </div> </body> </html>
Always add alt text for images for accessibility.
Use fixed width and height on the container or image to see object-fit effects clearly.
Object-fit works best with replaced elements like <img> and <video>.
Object-fit controls how images or videos fill their containers.
Tailwind CSS provides easy classes like object-cover and object-contain to use this.
Use object-fit to keep media looking good without stretching or cutting important parts.
Practice
object-cover do to an image inside a container?Solution
Step 1: Understand
Theobject-coverbehaviorobject-coverclass makes the image fill the container fully, keeping aspect ratio but cropping parts if needed.Step 2: Compare with other options
Other options describe shrinking or stretching, whichobject-coverdoes not do.Final Answer:
It makes the image fill the container completely, cropping if needed. -> Option AQuick Check:
object-cover = fill with cropping [OK]
- Confusing object-cover with object-contain
- Thinking object-cover stretches image
- Assuming object-cover hides overflow
Solution
Step 1: Identify the class for fitting inside container
Theobject-containclass makes the image fit inside the container without cropping, preserving aspect ratio.Step 2: Check other options for validity
object-stretchandobject-scaleare not valid Tailwind classes;object-fillstretches the image, which is not fitting without cropping.Final Answer:
object-contain -> Option DQuick Check:
Contain fits inside without crop = object-contain [OK]
- Using non-existent classes like object-stretch
- Confusing object-fill with object-contain
- Assuming object-fill preserves aspect ratio
<div class="w-40 h-40"> <img src="photo.jpg" class="object-contain w-full h-full" alt="Sample" /> </div>
How will the image appear inside the 160x160 pixel container?
Solution
Step 1: Analyze the container and image classes
The container is 10rem by 10rem (w-40 h-40). The image usesobject-containwith full width and height.Step 2: Understand
object-containeffectobject-containscales the image to fit inside the container fully without cropping, preserving aspect ratio, so some empty space may appear.Final Answer:
The image will fit inside the container entirely, showing all parts with possible empty space. -> Option BQuick Check:
object-contain = fit inside with no crop [OK]
- Thinking object-contain crops image
- Assuming image stretches ignoring aspect ratio
- Ignoring container size constraints
<img src="pic.jpg" class="object-contain w-full h-full" alt="Pic" />
Why does the image not crop and instead show empty space?
Solution
Step 1: Understand
object-containbehaviorobject-containscales the image to fit inside the container fully without cropping, preserving aspect ratio, so empty space can appear.Step 2: Check other options for correctness
w-fullandh-fullare present, image size does not affect cropping behavior, andobject-containdoes not crop by design.Final Answer:
Becauseobject-containfits the whole image inside, no cropping. -> Option CQuick Check:
object-contain = no crop, fits inside [OK]
- Thinking object-contain crops image
- Missing width or height classes
- Blaming image size for cropping
<img src="landscape.jpg" alt="Landscape" class="?" />
Solution
Step 1: Identify requirement for full container fill with cropping
The image must fill the container fully, cropping if needed, without distortion.Step 2: Match Tailwind classes to requirements
object-coverfills container fully with cropping and preserves aspect ratio (no distortion).w-full h-fullensures it fills container size responsively.Step 3: Check other options
object-containfits inside without cropping,object-fillstretches (distorts),object-noneshows original size without fitting.Final Answer:
object-cover w-full h-full -> Option AQuick Check:
Cover + full width/height = fill with crop, no distortion [OK]
- Using object-contain when cropping is needed
- Using object-fill causing distortion
- Not setting both width and height to full
