This code snippet demonstrates how to show a button on image hover using Bootstrap. It uses the overlay
and button
classes to create a transparent overlay that appears when the user hovers over the image. The button is then positioned inside the overlay so that it is visible when the overlay is displayed.
You can use this code in your website projects to add an interactive element to image cards.
How to Show Button On Image Hover Using Bootstrap
1. First of all, Include the Bootstrap CSS library by adding the following CDN link in the head of your HTML document. This ensures you have the necessary styles for the card layout.
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
2. Create a container and a card with an image, title, overlay, and a hidden button. Customize the content within the card to suit your needs.
<div class="container"> <div class="card"> <img src="https://images.unsplash.com/photo-1488628075628-e876f502d67a?dpr=1&auto=format&fit=crop&w=1500&h=1000&q=80&cs=tinysrgb&crop=&bg=" alt="" /> <p class="title">card title</p> <div class="overlay"></div> <div class="btn button"><a href="#"> BUTTON </a></div> </div> </div>
3. Finally, Copy and paste the following CSS code into your stylesheet. This code controls the layout, hover effects, and button visibility. Feel free to modify the styles to match your website’s theme.
.card{ position: relative; min-height: 300px; width: 500px; margin: 50px auto; } .overlay { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0, 0, 0, 0); transition: background 0.5s ease; } .card:hover .overlay { display: block; background: rgba(0, 0, 0, .3); } .card img { position: absolute; width: 500px; height: 300px; left: 0; } .title { position: absolute; width: 500px; left: 0; top: 120px; font-weight: 700; font-size: 30px; text-align: center; text-transform: uppercase; color: white; z-index: 1; transition: top .5s ease; } .card:hover .title { top: 90px; } .button { position: absolute; width: 500px; left:0; top: 180px; text-align: center; opacity: 0; transition: opacity .35s ease; } .button a { width: 200px; padding: 12px 48px; text-align: center; color: white; border: solid 2px white; z-index: 1; } .card:hover .button { opacity: 1; }
Feel free to experiment with different images and styles to match your website’s design.
That’s all! hopefully, you have successfully created a functionality to show a button on card hover using Bootstrap. If you have any questions or suggestions, feel free to comment below.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.