Show Button on Image Hover in Bootstrap

Show Button on Image Hover Bootstrap
Code Snippet:CSS Image hover show button
Author: Phil Cheng
Published: January 10, 2024
Last Updated: January 22, 2024
Downloads: 1,310
License: MIT
Edit Code online: View on CodePen
Read More

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/bootstrap@5.3.2/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.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X