Bird
Raised Fist0
Tailwindmarkup~20 mins

Object-fit for images and media in Tailwind - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Object-fit Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
rendering
intermediate
2:00remaining
What will the image look like with 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?

Tailwind
<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>
AThe image will stretch to fill the container, ignoring its aspect ratio.
BThe image will be fully visible, scaled down to fit inside the container with empty space around it.
CThe image will fill the container completely, cropping parts that overflow while keeping aspect ratio.
DThe image will be centered but not resized, showing only the top-left part.
Attempts:
2 left
💡 Hint

Think about how cover behaves: it fills the container while keeping the image's shape, even if some parts get cut off.

selector
intermediate
1:30remaining
Which Tailwind class sets 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?

Aobject-contain
Bobject-fill
Cobject-cover
Dobject-scale-down
Attempts:
2 left
💡 Hint

Remember, contain keeps the whole image visible inside the container.

🧠 Conceptual
advanced
2:00remaining
What happens if you use object-fit: scale-down on an image?

Given an image inside a container, what is the behavior of object-fit: scale-down?

AThe image is always scaled up to fill the container, cropping if needed.
BThe image is scaled down to fit inside the container only if it is larger; otherwise, it keeps its original size.
CThe image is centered and clipped without resizing.
DThe image is stretched to fill the container ignoring aspect ratio.
Attempts:
2 left
💡 Hint

Think about when the image is smaller or larger than the container.

accessibility
advanced
1:30remaining
How to ensure images with object-fit remain accessible?

You use object-fit to style images on your webpage. What is the best practice to keep images accessible?

AAlways add descriptive <code>alt</code> text and ensure images are keyboard focusable if interactive.
BUse empty <code>alt</code> attributes to hide images from screen readers.
CAvoid using <code>alt</code> attributes because <code>object-fit</code> changes image shape.
DAdd <code>aria-hidden="true"</code> to all images styled with <code>object-fit</code>.
Attempts:
2 left
💡 Hint

Think about screen readers and users who rely on them.

layout
expert
2:30remaining
Which Tailwind classes create a responsive square container with an image that covers it fully?

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?

AUse <code>aspect-square max-w-xs</code> on the container and <code>object-scale-down w-full h-full</code> on the image.
BUse <code>w-64 h-64</code> on the container and <code>object-contain w-full h-full</code> on the image.
CUse <code>aspect-video w-full</code> on the container and <code>object-fill w-full h-full</code> on the image.
DUse <code>aspect-square w-full max-w-sm</code> on the container and <code>object-cover w-full h-full</code> on the image.
Attempts:
2 left
💡 Hint

Remember aspect-square keeps a square ratio and object-cover fills the container cropping if needed.

Practice

(1/5)
1. What does the Tailwind CSS class object-cover do to an image inside a container?
easy
A. It makes the image fill the container completely, cropping if needed.
B. It shrinks the image to fit inside the container without cropping.
C. It stretches the image to fill the container, ignoring aspect ratio.
D. It hides the image if it is larger than the container.

Solution

  1. Step 1: Understand object-cover behavior

    The object-cover class makes the image fill the container fully, keeping aspect ratio but cropping parts if needed.
  2. Step 2: Compare with other options

    Other options describe shrinking or stretching, which object-cover does not do.
  3. Final Answer:

    It makes the image fill the container completely, cropping if needed. -> Option A
  4. Quick Check:

    object-cover = fill with cropping [OK]
Hint: Remember: cover fills and crops, contain fits inside [OK]
Common Mistakes:
  • Confusing object-cover with object-contain
  • Thinking object-cover stretches image
  • Assuming object-cover hides overflow
2. Which Tailwind CSS class correctly applies the object-fit property to make an image fit inside its container without cropping?
easy
A. object-scale
B. object-stretch
C. object-fill
D. object-contain

Solution

  1. Step 1: Identify the class for fitting inside container

    The object-contain class makes the image fit inside the container without cropping, preserving aspect ratio.
  2. Step 2: Check other options for validity

    object-stretch and object-scale are not valid Tailwind classes; object-fill stretches the image, which is not fitting without cropping.
  3. Final Answer:

    object-contain -> Option D
  4. Quick Check:

    Contain fits inside without crop = object-contain [OK]
Hint: Contain fits inside, cover fills and crops [OK]
Common Mistakes:
  • Using non-existent classes like object-stretch
  • Confusing object-fill with object-contain
  • Assuming object-fill preserves aspect ratio
3. Given this HTML snippet:
<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?
medium
A. The image will fill the container fully, cropping parts if needed.
B. The image will fit inside the container entirely, showing all parts with possible empty space.
C. The image will stretch to fill the container ignoring aspect ratio.
D. The image will overflow outside the container.

Solution

  1. Step 1: Analyze the container and image classes

    The container is 10rem by 10rem (w-40 h-40). The image uses object-contain with full width and height.
  2. Step 2: Understand object-contain effect

    object-contain scales the image to fit inside the container fully without cropping, preserving aspect ratio, so some empty space may appear.
  3. Final Answer:

    The image will fit inside the container entirely, showing all parts with possible empty space. -> Option B
  4. Quick Check:

    object-contain = fit inside with no crop [OK]
Hint: object-contain fits image fully inside container [OK]
Common Mistakes:
  • Thinking object-contain crops image
  • Assuming image stretches ignoring aspect ratio
  • Ignoring container size constraints
4. You want an image to fill its container without distortion and crop overflow parts. You wrote:
<img src="pic.jpg" class="object-contain w-full h-full" alt="Pic" />

Why does the image not crop and instead show empty space?
medium
A. Because the image file is too small.
B. Because w-full and h-full are missing.
C. Because object-contain fits the whole image inside, no cropping.
D. Because object-contain crops the image by default.

Solution

  1. Step 1: Understand object-contain behavior

    object-contain scales the image to fit inside the container fully without cropping, preserving aspect ratio, so empty space can appear.
  2. Step 2: Check other options for correctness

    w-full and h-full are present, image size does not affect cropping behavior, and object-contain does not crop by design.
  3. Final Answer:

    Because object-contain fits the whole image inside, no cropping. -> Option C
  4. Quick Check:

    object-contain = no crop, fits inside [OK]
Hint: Contain never crops; cover crops to fill [OK]
Common Mistakes:
  • Thinking object-contain crops image
  • Missing width or height classes
  • Blaming image size for cropping
5. You want a responsive image that always fills its container fully on all screen sizes, cropping if needed, but never distorts. Which Tailwind classes should you use?
<img src="landscape.jpg" alt="Landscape" class="?" />
hard
A. object-cover w-full h-full
B. object-contain w-full h-auto
C. object-fill w-auto h-auto
D. object-none w-full h-full

Solution

  1. Step 1: Identify requirement for full container fill with cropping

    The image must fill the container fully, cropping if needed, without distortion.
  2. Step 2: Match Tailwind classes to requirements

    object-cover fills container fully with cropping and preserves aspect ratio (no distortion). w-full h-full ensures it fills container size responsively.
  3. Step 3: Check other options

    object-contain fits inside without cropping, object-fill stretches (distorts), object-none shows original size without fitting.
  4. Final Answer:

    object-cover w-full h-full -> Option A
  5. Quick Check:

    Cover + full width/height = fill with crop, no distortion [OK]
Hint: Use object-cover with full width and height for cropping fill [OK]
Common Mistakes:
  • Using object-contain when cropping is needed
  • Using object-fill causing distortion
  • Not setting both width and height to full