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
Background size and position with Tailwind CSS
📖 Scenario: You are creating a simple webpage section that uses a background image. You want to control how the background image fits and where it is placed inside the section.
🎯 Goal: Build a webpage section with a background image using Tailwind CSS. You will set the background size to cover the entire section and position the background image to the center.
📋 What You'll Learn
Create a <section> element with a background image using Tailwind CSS classes
Add a Tailwind class to set the background size to cover
Add a Tailwind class to position the background image at the center
Include accessible text inside the section describing the background
💡 Why This Matters
🌍 Real World
Background images are common in website headers, banners, and sections to create visually appealing layouts.
💼 Career
Knowing how to control background size and position with Tailwind CSS is useful for front-end developers building responsive and accessible web pages.
Progress0 / 4 steps
1
Create the section with a background image
Create a <section> element with the Tailwind class bg-[url('https://images.unsplash.com/photo-1506744038136-46273834b3fb')] to set the background image.
Tailwind
Hint
Use the Tailwind class bg-[url('...')] inside the class attribute of the <section> tag.
2
Add background size cover
Add the Tailwind class bg-cover to the <section> element to make the background image cover the entire section area.
Tailwind
Hint
Add bg-cover next to the background image class inside the class attribute.
3
Add background position center
Add the Tailwind class bg-center to the <section> element to position the background image at the center.
Tailwind
Hint
Add bg-center after bg-cover inside the class attribute.
4
Add height and accessibility text
Add the Tailwind class h-64 to the <section> element to give it height. Also, add an aria-label attribute with the value Background image section for accessibility.
Tailwind
Hint
Add h-64 to the class list and add aria-label="Background image section" inside the <section> tag.
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
Step 1: Understand background size classes
bg-cover makes the background image fill the entire element, cropping if needed.
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.
bg-auto keeps original size, bg-cover fills the area. Using both together conflicts because they set different sizes.
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.
Final Answer:
Using both bg-auto and bg-cover together causes conflict -> Option A
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
Step 1: Choose background size to fit fully inside
bg-contain fits the entire image inside without cropping, perfect for responsive cards.
Step 2: Set position and repeat
bg-top-left places image at top left corner, and bg-no-repeat prevents repeating.