📖 Scenario: You are creating a simple webpage section to showcase a product with a card. The card should have an image with text overlay on top of it, similar to a product highlight or promotion.
🎯 Goal: Build a Bootstrap card that displays an image with a dark overlay containing a title and some descriptive text. The overlay text should be readable on top of the image.
📋 What You'll Learn
Use Bootstrap 5 classes for the card and overlay
Include an image inside the card
Add a dark overlay with text on top of the image
Use semantic HTML and accessible attributes
Make sure the overlay text is visible and readable
💡 Why This Matters
🌍 Real World
Cards with image overlays are common in product showcases, portfolios, and marketing websites to highlight important information attractively.
💼 Career
Knowing how to use Bootstrap cards and overlays helps you build professional, responsive UI components quickly, a valuable skill for front-end web development jobs.
Progress0 / 4 steps
1
Create the basic Bootstrap card structure with an image
Write the HTML code to create a div with class card. Inside it, add an img tag with class card-img and set its src attribute to https://via.placeholder.com/400x200 and alt attribute to Sample image.
Bootsrap
Hint
Use a div with class card and an img tag with class card-img.
2
Add a dark overlay container inside the card
Inside the div with class card, add another div with class card-img-overlay to hold the overlay content.
Bootsrap
Hint
The overlay container uses the Bootstrap class card-img-overlay.
3
Add a title and text inside the overlay with dark background
Inside the div with class card-img-overlay, add an h5 element with class card-title containing the text Card Title. Below it, add a p element with class card-text containing the text This is a description overlay.. Also add the Bootstrap class bg-dark and bg-opacity-50 to the overlay div to create a semi-transparent dark background behind the text.
Bootsrap
Hint
Use bg-dark and bg-opacity-50 on the overlay container for a transparent dark background.
4
Add accessibility and responsive container
Wrap the entire card inside a section element with aria-label="Product highlight card" for accessibility. Also add the Bootstrap class container to the section to center the card on the page responsively.
Bootsrap
Hint
Use a section with aria-label and Bootstrap container class to wrap the card.
Practice
(1/5)
1. What is the purpose of the card-img-overlay class in Bootstrap cards?
easy
A. To add a border around the card image
B. To place text or content on top of the card image
C. To resize the card image automatically
D. To add a shadow effect to the card image
Solution
Step 1: Understand the role of card-img-overlay
This class is used to add content that appears over the card image, such as text or buttons.
Step 2: Compare with other options
Other options describe effects or styles not related to overlays, so they are incorrect.
Final Answer:
To place text or content on top of the card image -> Option B
Quick Check:
Overlay content = card-img-overlay = B [OK]
Hint: Overlay means content on top of image [OK]
Common Mistakes:
Confusing overlay with image resizing
Thinking overlay adds borders or shadows
Ignoring that overlay is for layering content
2. Which of the following is the correct way to add an image inside a Bootstrap card with an overlay?
easy
A.
Text
B.
Text
C.
Text
D.
Text
Solution
Step 1: Check correct structure for image and overlay
The image uses card-img class and the overlay content uses card-img-overlay inside the card container.
Step 2: Validate
Text
structure
Text
correctly places the image first with card-img and overlay div after with card-img-overlay.
Final Answer:
<div class='card'><img src='img.jpg' class='card-img'><div class='card-img-overlay'>Text</div></div> -> Option D
Quick Check:
Image class = card-img, Overlay class = card-img-overlay [OK]