What will the background image look like inside the div?
medium
A. The image will fill the div completely, cropping parts if needed.
B. The whole image will fit inside the div, centered, with possible empty space.
C. The image will repeat to fill the div area.
D. The image will stretch to exactly 200px by 100px, ignoring aspect ratio.
Solution
Step 1: Analyze background-size: contain; effect
This makes the entire image fit inside the div without cropping, preserving aspect ratio.
Step 2: Consider other properties
background-repeat: no-repeat; prevents tiling, and background-position: center; centers the image. So empty space may appear if aspect ratios differ.
Final Answer:
The whole image will fit inside the div, centered, with possible empty space. -> Option B
Quick Check:
contain fits fully, no repeat, centered [OK]
Hint: Contain fits whole image, no repeat means no tiling [OK]
Common Mistakes:
Assuming contain crops the image
Thinking image repeats by default
Confusing stretch with contain
4. This CSS code is intended to make a background image fill the element without repeating, but it doesn't work as expected:
div {
background-image: url('tree.jpg');
background-size: 100%;
background-repeat: no-repeat;
}
What is the problem?
medium
A. background-size: 100%; only sets width, height is auto, so it may not fill the entire element.
B. The URL is missing quotes around the image path.
C. background-repeat must be set to repeat-x to fill horizontally.
D. The property background-size does not accept percentages.
Solution
Step 1: Understand background-size: 100%; meaning
Setting only one value means width is 100% of element, height auto to keep aspect ratio, so it may not fill the entire element.
Step 2: Check other options
URL quotes are optional but recommended, background-repeat: no-repeat; disables tiling correctly, and background-size accepts percentages.
Final Answer:
background-size: 100%; only sets width, height is auto, so it may not fill the entire element. -> Option A
Quick Check:
One value sets width only, height auto [OK]
Hint: Two values needed to set both width and height [OK]
Common Mistakes:
Using one value thinking it sets both width and height
Ignoring aspect ratio effects
Misunderstanding background-repeat options
5. You want a background image to always cover the entire element area on all screen sizes, but never be cropped. Which CSS approach is best?
hard
A. Use background-size: contain; and background-repeat: no-repeat;.
B. Use background-size: cover; and background-position: center;.
C. Use background-size: auto; and background-repeat: repeat;.
D. Use background-size: 100% 100%; to stretch image exactly.
Solution
Step 1: Understand the requirement
The image must always fill the entire element area (no empty space) on all screen sizes without cropping (whole image visible).
Step 2: Evaluate options
contain fits whole image but may leave empty space; cover fills but crops; 100% 100% stretches whole image to exactly fill without cropping or space; auto repeat tiles.
Final Answer:
Use background-size: 100% 100%; to stretch image exactly. -> Option D
Quick Check:
100% 100% fills exactly, no crop [OK]
Hint: 100% 100% stretches to fill exactly, no crop or space [OK]