Card images and overlays help you show pictures with text or buttons on top. This makes your content look nice and easy to understand.
Card images and overlays in Bootsrap
<div class="card bg-dark text-white"> <img src="image.jpg" class="card-img" alt="..."> <div class="card-img-overlay"> <h5 class="card-title">Card title</h5> <p class="card-text">Some overlay text.</p> </div> </div>
The card-img class makes the image fill the card.
The card-img-overlay class places text on top of the image.
<div class="card bg-dark text-white"> <img src="https://via.placeholder.com/300x200" class="card-img" alt="Sample Image"> <div class="card-img-overlay"> <h5 class="card-title">Overlay Title</h5> <p class="card-text">Overlay text on image.</p> </div> </div>
<div class="card"> <img src="https://via.placeholder.com/300x200" class="card-img-top" alt="Sample Image"> <div class="card-body"> <h5 class="card-title">Card Title</h5> <p class="card-text">Text below the image, not overlay.</p> </div> </div>
<div class="card bg-primary text-white"> <img src="https://via.placeholder.com/300x200" class="card-img" alt="Sample Image"> <div class="card-img-overlay d-flex flex-column justify-content-end"> <h5 class="card-title">Bottom Overlay</h5> <p class="card-text">Text aligned at the bottom of the image.</p> </div> </div>
This complete example shows a card with a dark overlay on an image. The text and a button appear on top of the image, centered vertically. The button has an accessible label.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Bootstrap Card Image Overlay Example</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <main class="container py-4"> <h2 class="mb-4">Card with Image and Overlay</h2> <div class="card bg-dark text-white" style="max-width: 300px;"> <img src="https://via.placeholder.com/300x200" class="card-img" alt="Sample Image"> <div class="card-img-overlay d-flex flex-column justify-content-center"> <h5 class="card-title">Beautiful View</h5> <p class="card-text">This is an example of text over an image.</p> <a href="#" class="btn btn-light btn-sm mt-2" aria-label="Learn more about Beautiful View">Learn More</a> </div> </div> </main> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> </body> </html>
Use alt text on images for accessibility.
Overlay text color should contrast well with the image for readability.
Use Bootstrap utility classes like d-flex and justify-content-center to position overlay content easily.
Card images with overlays let you place text or buttons on top of pictures.
Use card-img for the image and card-img-overlay for the overlay content.
Make sure overlay text is readable and accessible.
Practice
card-img-overlay class in Bootstrap cards?Solution
Step 1: Understand the role of
This class is used to add content that appears over the card image, such as text or buttons.card-img-overlayStep 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 BQuick Check:
Overlay content = card-img-overlay = B [OK]
- Confusing overlay with image resizing
- Thinking overlay adds borders or shadows
- Ignoring that overlay is for layering content
Solution
Step 1: Check correct structure for image and overlay
The image usescard-imgclass and the overlay content usescard-img-overlayinside the card container.Step 2: Validate
Textstructurecorrectly places the image first withTextcard-imgand overlay div after withcard-img-overlay.Final Answer:
<div class='card'><img src='img.jpg' class='card-img'><div class='card-img-overlay'>Text</div></div> -> Option DQuick Check:
Image class = card-img, Overlay class = card-img-overlay [OK]
- Putting overlay class on the image tag
- Placing overlay outside the card container
- Mixing up class names between image and overlay
<div class='card' style='width: 18rem;'>
<img src='photo.jpg' class='card-img' alt='Sample Image'>
<div class='card-img-overlay'>
<h5 class='card-title'>Hello</h5>
<p class='card-text'>Overlay text</p>
</div>
</div>Solution
Step 1: Understand card-img and card-img-overlay behavior
The image fills the card area and the overlay places text on top of the image.Step 2: Visualize the overlay content
Thecard-img-overlaycontent appears layered on the image, so text is visible over the picture.Final Answer:
A card with the image and text displayed on top of the image -> Option AQuick Check:
Overlay text on image = card-img-overlay = C [OK]
- Thinking overlay text appears below image
- Assuming image is hidden by overlay
- Confusing overlay with separate card sections
<div class='card'> <div class='card-img-overlay'>Overlay Text</div> <img src='pic.jpg' class='card-img' alt='Pic'> </div>
Solution
Step 1: Check the order of image and overlay
The overlay content must come after the image tag to appear on top visually.Step 2: Verify class usage
The image should havecard-imgand overlay a separate div withcard-img-overlay. The order matters for layering.Final Answer:
The overlay div should come after the image tag inside the card -> Option CQuick Check:
Overlay after image = layering correct = A [OK]
- Placing overlay before image causing hidden text
- Misusing classes on wrong elements
- Ignoring layering order in HTML structure
Solution
Step 1: Understand layering with card-img and card-img-overlay
The image usescard-imgand overlay content usescard-img-overlayfor layering text on image.Step 2: Add dark semi-transparent background and white text
Custom CSS oncard-img-overlaycan add a dark background with opacity. White text classes ensure readability.Final Answer:
Use card-img for image, card-img-overlay with custom dark background and white text -> Option AQuick Check:
Overlay + custom dark bg + white text = D [OK]
- Trying to filter image without overlay div
- Placing image inside overlay div incorrectly
- Not adding text color for contrast
Ready to prove it?
Pass the test and this pattern moves unknown
