Steps to incorporate hover buttons into a card layout

I'm looking to enhance the card view by adding edit and delete buttons that appear after hovering over it. Here's my code:

<div class="card">  //image section
    <img class="card-img-top" alt="Card image" style="width:50%" src="images/img1">
    <div class="card-body">
    <div class="button" onClick="updateData()">
        <i class="fa fa-edit" style="font-size:24px"></i>
    </div>
    <div class="button" onClick="deleteData()">
        <span aria-hidden="true" style="font-size:40px">&times;</span>
    </div>'
    <p class="card-text">Text description</p>
    </div>
</div>

I need help figuring out how to make those two buttons visible when I hover over the card. Can anyone advise me on this?

Answer №1

Implement the following selector

   button {
   display: none; /* Button hidden */
   }

   .divbutton:hover button {
   display: block; /* Show button on div hover */
   }

Example

.card {
    height: 30px;
    background: #000;
}

button {
    display: none;
}

.card:hover button {
    display: block;
}
<script src="https://use.fontawesome.com/3aa0d20210.js"></script>
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="20424f4f54535452415060140e150e13">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" />
<div class="card"> 
    <img class="card-img-top" alt="Card image" style="width:50%" src="images/img1">
    <div class="card-body">
    <button class="button" onClick="updateData()">
        <i class="fa fa-edit" style="font-size:24px"></i>
    </button>
    <button class="button" onClick="deleteData()">
        <span aria-hidden="true" style="font-size:40px">&times;               </span>
    </button>'
    <p class="card-text">Text description</p>
    </div>
</div>

Ensure to set a height or min-height for your div element, otherwise it will have a height of 0 if no content is present. Avoid using display: none; as inline styling, as inline styles have high specificity, leading to the need for using !important which is not recommended.

In the provided code snippet, I am styling the button {/*Styles*/}, but this is a general element selector, so remember to assign a class to your button element.

Answer №2

This code snippet should meet your needs:

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="Description" content="Enter your description here" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.0/css/all.min.css">
    <link rel="stylesheet" href="assets/css/style.css">
    <title>

    </title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/js/bootstrap.min.js"></script>

</head>

<body>
    <div class="card" style="width: 18rem;">
        <img class="card-img-top" src="..." alt="Card image cap">
        <div class="card-body">
            <h5 class="card-title">Card title</h5>
            <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.
            </p>
            <a href="#" class="btn btn-primary">Go somewhere</a>
        </div>
        <div id="outer" style="display:none">
            <button type="button" class="btn btn-primary">Edit</button>
            <button type="button" class="btn btn-primary">Delete</button>
        </div>
    </div>
    <script>
        $(document).ready(
            $(".card").mouseenter(function() {
                $("#outer").css("display", "block");
            }),
            $(".card").mouseleave(function() {
                $("#outer").css("display", "none");
            })
        )
    </script>
</body>

</html>

verify: https://jsfiddle.net/L46ug3bt/

Many thanks!

Answer №3

Check out this handy block of Javascript code:

$(() => {
  $(".card").on("mouseenter", () => { // Trigger when mouse enters card
    $(".btn").removeClass("d-none") // Remove the d-none class to show button
  });
  $(".card").on("mouseleave", () => { // Opposite action on mouse leave
    $(".btn").addClass("d-none")
  });
});
<script src="https://use.fontawesome.com/3aa0d20210.js"></script>
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4b2924243f383f392a3b0b7f657e6578">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="card">
  <!-- image section -->
  <img class="card-img-top" alt="Card image" style="width:50%" src="images/img1">
  <div class="card-body">
    <div class="btn btn-success d-none" onClick="updateData()">
      <i class="fa fa-edit" style="font-size:24px"></i>
    </div>
    <div class="btn btn-danger d-none" onClick="deleteData()">
      <span aria-hidden="true" style="font-size:40px">&times;</span>
    </div>'
    <p class="card-text">Text description</p>
  </div>
</div>

The "d-none" class effectively hides the buttons by adding a display: none. Also, note that in Bootstrap 4, there's no .button, so make sure you're using the correct version!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I am having trouble with the spacing on my website, even after I have tried declaring it

I'm having trouble formatting the footer to appear at the bottom of the page. I've tried adjusting the CSS code for the .container and .footer classes, but nothing seems to work. As a newbie in website development, any helpful tips or suggestions ...

Creating a Simple HTML Table Using PHP Arrays

I attempted to create a simple PHP array table, but the output of my code did not meet my expectations. <?php $names = array ( 'First Name' => array('Alfre', 'Beka', 'Charlie'), ...

In IE11, the fixed position does not depend on the parent container

In Chrome and Firefox, when a transform is defined in the parent container, the child's position will inherit from the parent container. However, this behavior does not occur in IE. So, how can I ensure that it works in IE11 so that the child's ...

unable to insert HTML image

In my current project, I am facing an issue with embedding a banner image into HTML emails that are sent out after a forum is filled out. Previously, the code worked fine when using the latest version of PHP, but due to server requirements, I had to downgr ...

Accessing HTML generated from a PHP file through AJAX

I'm currently attempting to retrieve the file content of an HTML rendering from a specific URL. Below is the code I am utilizing, which consistently results in an error: $.ajax({ type: 'POST', url: 'http://www.withhold ...

Is there a way to share the Username and Password without the need to manually input it?

My goal is to develop a C++ application for my classmates at school. Currently, they are required to visit our school's website and navigate to the login page. Once there, they enter their username and password, log in, and proceed to find their spec ...

Proper implementation of the HTML5 <hr> element

Currently working on a fresh webpage design for a company, keeping in mind the sensible guidelines of HTML5/W3C. After delving into the semantic significance of <hr />, I find myself pondering its suitable usage: <section name="historyItem"> ...

Customize the color of arrows within an ng-bootstrap carousel

I am currently working on a carousel using ng-bootstrap, and I need to change the color or image of the arrows from white to black because my background images are white and not visible. The issue is that I am having trouble accessing span, and I am unsure ...

Loading and refreshing iframe content in real-time using JQuery on change() and keyup() events

I have been tackling a unique challenge with my custom CMS in Drupal for a couple of weeks now. I am facing an issue where I need to dynamically load a URL by extracting the node ID from the target Drupal page and appending it to the base URL of the websit ...

Troubleshooting: Issue with AJAX xmlhttp.send() functionality

I'm new to AJAX and have been stuck on the same issue for hours. Here is my script code: <script language='javascript'> function upvote(id ,username) { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = fun ...

Displaying image issues persist on Safari browsers for Mac and iPhone

I am currently working on a website and facing an issue with the responsive image display on Safari for Mac/iPhone. Here is the code snippet I have been using: <head> <meta charset="utf-8"> <meta http-equiv="Content-Language" co ...

Tips for Presenting Json Information in Bootstrap Columns

I am working on a React App that showcases Json Data on a webpage Displayed below is the JSON file: [ { "name":"Apple", "price":"3,99", "description":"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod te ...

Leveraging PHP for generating a list of items without a specific order from data retrieved

I am facing a challenge with a MySQL query that is returning multiple records. Here is an example of the data: +-------+------------+-----------+ | group | first_name | last_name | +-------+------------+-----------+ | Red | John | Doe | +--- ...

Why is the current Menu Item highlight feature not functioning properly?

Why isn't the highlight current Menu Item feature working? I've checked my code, but it doesn't seem to be functioning as expected. Could you lend me a hand? Html: <section id="menu-container"> <div id="bar"><img src="b ...

Blending PHP with jQuery

I'm currently working on the same page as before but this time, I'm using it to experiment with jQuery. I'm trying to learn it for my 'boss', but unfortunately, I can't seem to get the JavaScript in this file to run at all, le ...

Adjusting the alignment of table cell contents without the use of JavaScript or external CSS files is necessary

I need help creating a table that resembles the design shown in the image below. I want each td cell in the outer table to have a number at the top right with a border, and text at the bottom left without a border: https://i.sstatic.net/1YTkR.png Current ...

Challenges with border-color and CSS input:focus

The dilemma Currently, I am immersed in a project focusing on constructing a front end using bootstrap4. In order to align with the company's main color scheme, I decided to alter the highlighting color of inputs. To achieve this, I made the followi ...

Having Trouble with Form Submission Button Across Different Web Browsers

Having some trouble with my form - all fields are properly closed with tags, but when I click the submit button, nothing happens. The page is loaded with code, so here's the link for you to check it out. Unfortunately, right-click is disabled, so ple ...

What is the method for updating the content within a div element?

I am trying to add multiple elements to my content, including an h1 element, an input field, and some buttons. I have written a function to create the h1 and input elements, but when trying to add them to my content div, I encountered an error (TypeError ...

A beginner's guide to implementing the GET method with Express.js

Hey there, I have a folder named "vehicles" on my PC and I'm looking to set up a localhost to generate links for each image in that folder. For example: localhost:800/vehicle/ So when I click on the link, it should display the image. However, bein ...