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
Using CSS Object Fit to Control Image Display
📖 Scenario: You are creating a simple webpage that shows a profile picture inside a fixed-size box. The image might have different sizes and shapes, so you want to control how it fits inside the box without stretching or cutting off important parts.
🎯 Goal: Build a webpage with a fixed-size image container and use the CSS object-fit property to make the image fit nicely inside the container.
📋 What You'll Learn
Create an HTML structure with a container and an image inside it.
Set the container size to 200px by 200px.
Use CSS to apply object-fit property to the image.
Try different object-fit values: cover, contain, and fill.
Make sure the image is responsive and accessible.
💡 Why This Matters
🌍 Real World
Websites often need to display images of different sizes inside fixed spaces without distortion. Using object-fit helps keep images looking good and consistent.
💼 Career
Front-end developers use CSS object-fit to create visually appealing and accessible image layouts in real projects.
Progress0 / 4 steps
1
Create the HTML structure with a container and an image
Write the HTML code to create a <div> with class image-container and inside it place an <img> tag with src="https://via.placeholder.com/300x150" and alt="Profile picture".
CSS
Hint
Use a <div> with class image-container and inside it add an <img> tag with the exact src and alt attributes.
2
Set the container size with CSS
Add CSS to set the .image-container class width and height to 200px each, and add a border of 2px solid black to see the container edges.
CSS
Hint
Use CSS properties width, height, and border inside the .image-container selector.
3
Apply object-fit: cover to the image
Add CSS to the img inside .image-container to set its width and height to 100% and apply object-fit: cover; so the image covers the container without distortion.
CSS
Hint
Inside the .image-container img selector, set width and height to 100% and add object-fit: cover;.
4
Try object-fit: contain and add accessibility improvements
Change the object-fit property value to contain in the CSS for the image. Also, add aria-label="Profile picture container" to the div with class image-container for accessibility.
CSS
Hint
Change object-fit to contain and add aria-label="Profile picture container" to the div with class image-container.
Practice
(1/5)
1. What does the CSS property object-fit: cover; do to an image inside a container?
easy
A. It fits the entire image inside the container without cropping.
B. It stretches the image to fill the container, possibly distorting it.
C. It fills the container completely, cropping parts if needed.
D. It repeats the image to fill the container.
Solution
Step 1: Understand object-fit: cover; behavior
This property makes the image fill the container completely, cropping parts if the aspect ratio differs.
Step 2: Compare with other options
Unlike contain which fits without cropping, cover crops to fill fully.
Final Answer:
It fills the container completely, cropping parts if needed. -> Option C
Quick Check:
Cover = fill and crop [OK]
Hint: Cover fills and crops; contain fits without cropping [OK]
Common Mistakes:
Confusing cover with contain
Thinking cover stretches without cropping
Assuming cover repeats the image
2. Which of the following is the correct CSS syntax to make an image fit inside its container without cropping or distortion?
easy
A. object-fit: stretch;
B. object-fit: fill;
C. object-fit: crop;
D. object-fit: contain;
Solution
Step 1: Identify the property for fitting without cropping
object-fit: contain; fits the entire image inside the container without cropping or distortion.
Step 2: Check other options
fill stretches, stretch is invalid, and crop is not a valid value.
Final Answer:
object-fit: contain; -> Option D
Quick Check:
Contain = fit inside without cropping [OK]
Hint: Contain fits fully inside; fill stretches [OK]
Common Mistakes:
Using invalid value 'stretch'
Confusing fill with contain
Thinking crop is a valid value
3. Given this HTML and CSS, what will the image look like in the browser?
A. The entire image fits inside the container without cropping, possibly leaving empty space.
B. The image fills the container and may be cropped.
C. The image stretches and distorts to fill the container.
D. The image repeats to fill the container.
Solution
Step 1: Analyze the container and image size
The container is 200px wide and 100px tall. The image is set to 100% width and height of the container.
Step 2: Understand object-fit: contain; effect
This makes the image scale to fit inside the container fully without cropping or distortion, preserving aspect ratio. Empty space may appear if aspect ratios differ.
Final Answer:
The entire image fits inside the container without cropping, possibly leaving empty space. -> Option A
Quick Check:
Contain = fit inside with possible empty space [OK]
Hint: Contain fits fully, may leave space; cover crops [OK]
Common Mistakes:
Assuming image will crop with contain
Thinking image will distort with contain
Confusing contain with fill
4. You want an image to fill its container without distortion, but it currently stretches oddly. Which CSS fix will solve this?
Step 1: Identify the problem with object-fit: fill;
This stretches the image to fill container width and height, causing distortion.
Step 2: Use object-fit: cover; to fix distortion
cover fills the container while preserving aspect ratio, cropping if needed, preventing distortion.
Final Answer:
Change object-fit to cover. -> Option A
Quick Check:
Cover fills without distortion by cropping [OK]
Hint: Use cover to fill without distortion, fill stretches [OK]
Common Mistakes:
Using fill causes distortion
Removing width/height breaks layout
Thinking object-position fixes distortion
5. You have a video inside a 400px by 300px container. You want the video to fill the container fully without distortion, but keep the center visible if cropping happens. Which CSS is best?