Bird
Raised Fist0
Tailwindmarkup~10 mins

Background size and position in Tailwind - Interactive Code Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set the background size to cover the entire element.

Tailwind
<div class="bg-[url('/image.jpg')] [1] h-64 w-64"></div>
Drag options to blanks, or click blank then click option'
Abg-contain
Bbg-repeat
Cbg-cover
Dbg-center
Attempts:
3 left
💡 Hint
Common Mistakes
Using bg-contain which fits the image but may leave empty space.
Using bg-repeat which repeats the image instead of resizing.
2fill in blank
medium

Complete the code to center the background image inside the element.

Tailwind
<div class="bg-[url('/photo.png')] h-48 w-48 [1]"></div>
Drag options to blanks, or click blank then click option'
Abg-left
Bbg-top
Cbg-bottom
Dbg-center
Attempts:
3 left
💡 Hint
Common Mistakes
Using bg-top or bg-bottom which align the image to edges.
Using bg-left which aligns the image to the left side.
3fill in blank
hard

Fix the error in the code to make the background image not repeat.

Tailwind
<div class="bg-[url('/pattern.svg')] [1] h-40 w-40"></div>
Drag options to blanks, or click blank then click option'
Abg-repeat-x
Bbg-no-repeat
Cbg-repeat
Dbg-repeat-y
Attempts:
3 left
💡 Hint
Common Mistakes
Using bg-repeat which causes the image to repeat.
Using bg-repeat-x or bg-repeat-y which repeat only in one direction.
4fill in blank
hard

Fill both blanks to make the background image cover the element and position it at the top right.

Tailwind
<div class="bg-[url('/scenery.jpg')] [1] [2] h-72 w-72"></div>
Drag options to blanks, or click blank then click option'
Abg-cover
Bbg-center
Cbg-top-right
Dbg-no-repeat
Attempts:
3 left
💡 Hint
Common Mistakes
Using bg-center instead of bg-top-right for position.
Using bg-no-repeat which only controls repetition.
5fill in blank
hard

Fill all three blanks to create a background image that is contained inside the element, positioned at bottom left, and does not repeat.

Tailwind
<div class="bg-[url('/texture.png')] [1] [2] [3] h-56 w-56"></div>
Drag options to blanks, or click blank then click option'
Abg-contain
Bbg-no-repeat
Cbg-bottom-left
Dbg-cover
Attempts:
3 left
💡 Hint
Common Mistakes
Using bg-cover instead of bg-contain which may crop the image.
Using bg-repeat which causes the image to repeat.
Using bg-center instead of bg-bottom-left for position.

Practice

(1/5)
1. Which Tailwind class makes a background image cover the entire element, possibly cropping parts of the image?
easy
A. bg-cover
B. bg-contain
C. bg-auto
D. bg-center

Solution

  1. Step 1: Understand background size classes

    bg-cover makes the background image fill the entire element, cropping if needed.
  2. Step 2: Compare with other classes

    bg-contain fits the whole image inside without cropping, bg-auto keeps original size, and bg-center is about position, not size.
  3. Final Answer:

    bg-cover -> Option A
  4. Quick Check:

    Full coverage = bg-cover [OK]
Hint: Cover fills area, contain fits inside, auto keeps size [OK]
Common Mistakes:
  • Confusing bg-cover with bg-contain
  • Thinking bg-center changes size
  • Using bg-auto to fill area
2. Which Tailwind class correctly sets the background image position to the top right corner?
easy
A. bg-top-right
B. bg-top-right is not a valid Tailwind class
C. bg-righttop
D. bg-top

Solution

  1. Step 1: Recall Tailwind background position classes

    Tailwind provides position classes like bg-top for top center, but does not have bg-top-right.
  2. Step 2: Understand other options

    bg-top sets top center, bg-righttop is invalid, and bg-top-right is not a valid Tailwind class.
  3. Final Answer:

    bg-top -> Option D
  4. Quick Check:

    Top right is not a valid class; top center = bg-top [OK]
Hint: Tailwind uses bg-top for top center; no bg-top-right class [OK]
Common Mistakes:
  • Using bg-top-right which is invalid
  • Mixing order of position words
  • Using invalid class names
3. What will be the visual effect of this Tailwind class combination on a div with a background image?
<div class="bg-contain bg-bottom bg-no-repeat w-64 h-64"></div>
medium
A. Background image keeps original size, centered, repeats
B. Background image covers the div, positioned at the bottom, repeats
C. Background image fits inside the div, positioned at the bottom, no repeat
D. Background image fits inside the div, centered, repeats

Solution

  1. Step 1: Analyze background size

    bg-contain makes the image fit inside the div without cropping.
  2. Step 2: Analyze position and repeat

    bg-bottom places the image at the bottom, and bg-no-repeat prevents repeating.
  3. Final Answer:

    Background image fits inside the div, positioned at the bottom, no repeat -> Option C
  4. Quick Check:

    Contain + bottom + no-repeat = Background image fits inside the div, positioned at the bottom, no repeat [OK]
Hint: Contain fits, bottom moves image down, no-repeat stops repeats [OK]
Common Mistakes:
  • Confusing bg-contain with bg-cover
  • Ignoring bg-no-repeat effect
  • Assuming default center position
4. Identify the error in this Tailwind class usage for background size and position:
<div class="bg-auto bg-center bg-cover"></div>
medium
A. Using both bg-auto and bg-cover together causes conflict
B. bg-center is invalid for background position
C. Missing bg-no-repeat causes image to repeat
D. The classes are correct and will work as expected

Solution

  1. Step 1: Understand background size classes conflict

    bg-auto keeps original size, bg-cover fills the area. Using both together conflicts because they set different sizes.
  2. Step 2: Check position and repeat classes

    bg-center is valid for position, and bg-no-repeat is optional; its absence doesn't cause an error.
  3. Final Answer:

    Using both bg-auto and bg-cover together causes conflict -> Option A
  4. Quick Check:

    Conflicting size classes = Using both bg-auto and bg-cover together causes conflict [OK]
Hint: Don't combine conflicting size classes like bg-auto and bg-cover [OK]
Common Mistakes:
  • Thinking bg-center is invalid
  • Assuming missing bg-no-repeat is an error
  • Ignoring class conflicts
5. You want a background image that fits fully inside a responsive card, stays at the top left corner, and never repeats. Which Tailwind classes should you use together?
hard
A. bg-cover bg-top-left bg-no-repeat
B. bg-contain bg-top-left bg-no-repeat
C. bg-auto bg-left-top bg-repeat
D. bg-contain bg-center bg-repeat

Solution

  1. Step 1: Choose background size to fit fully inside

    bg-contain fits the entire image inside without cropping, perfect for responsive cards.
  2. Step 2: Set position and repeat

    bg-top-left places image at top left corner, and bg-no-repeat prevents repeating.
  3. Final Answer:

    bg-contain bg-top-left bg-no-repeat -> Option B
  4. Quick Check:

    Contain + top-left + no-repeat = bg-contain bg-top-left bg-no-repeat [OK]
Hint: Contain fits inside, top-left positions, no-repeat stops repeats [OK]
Common Mistakes:
  • Using bg-cover crops image
  • Confusing bg-top (top center) with top left
  • Forgetting bg-no-repeat to stop repeats