Achieve button highlighting by hovering over the card - Bootstrap 5

Is there a way to make a button focus when hovering over a card in Bootstrap from any place on the card? Currently, the button only focuses when directly hovered over. I want to achieve the effect shown in the second image after hovering over any part of the card.

https://i.sstatic.net/dDbs5.png

Code:

  <style>
  .hovernow {
    transition: box-shadow 0.3s ease-in-out;
  }

  .hovernow:hover {
    box-shadow: 0 5px 35px rgba(0, 0, 0, 1);
    border:2px solid #3E7DC0;
  }
  </style>

  <div class="col-sm-6 col-xl-3">
    <a href="#">
      <div class="bg-body hovernow rounded-3 text-center p-2 position-relative" style="border:2px solid #6f42c1;">
        <!-- Image -->
        <img src="{% static 'assets/images/persons/5.png' %}" class="img-fluid mb-3" alt="">
        <button class="btn btn-primary-soft text-white w-100">Age: 18-24</button>
      </div>
    </a>
  </div>

Answer №1

It is not recommended to use a button within an a tag. According to the HTML5 specification document by W3C, this is not valid HTML5:

Instead, you can structure it like this:

  <div class="col-sm-6 col-xl-3">
    <a href="#">
      <div class="bg-body hovernow rounded-3 text-center p-2 position-relative" style="border:2px solid #6f42c1;">
        <!-- Image -->
        <img src="{% static 'assets/images/persons/5.png' %}" class="img-fluid mb-3" alt="">
        <span class="btn btn-primary-soft text-white w-100">Age: 18-24</span>
      </div>
    </a>
  </div>

Answer №2

Implementing JS is necessary to ensure that button focus is set when hovering over. Consider using the following code snippet:

$(document).ready(function(){
$('div.hovernow').mouseover(function(){
    $(this).children().closest('button').focus();
});
});

Answer №3

If you want to create a stretched link in Bootstrap, you can use the .stretched-link class. Simply include it in your code like so:

<div class="col-sm-6 col-xl-3">
      <div class="bg-body hovernow rounded-3 text-center p-2 position-relative" style="border:2px solid #6f42c1;">
        <!-- Image -->
        <img src="{% static 'assets/images/persons/5.png' %}" class="img-fluid mb-3" alt="">
        <a href="#" class="btn btn-primary-soft text-white w-100 stretched-link">Age: 18-24</a>
      </div>
  </div>

For more information on using stretched links in Bootstrap, you can reference the Bootstrap documentation here.

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

Angular JS: Grabbing Text from a Specific div and Copying it to Clipboard

I recently developed a Random Password Generator using Angular JS. Here is how the output appears in the application: <div data-ng-model="password" id="password"> <input class="form-control" data-ng-model="password" id="password" placeholder=" ...

The feature for automatically hiding other dropdowns when one is open is not functioning properly

Trying to finalize a mega menu that almost works. I need help with closing all other open mega menus when a user clicks on one. Check out the JavaScript code snippet I've written below. The mega menu currently toggles correctly, but it doesn't h ...

How do three buttons display identical content?

I have three buttons on my website, each with its own unique content that should display in a modal when clicked. However, I am experiencing an issue where regardless of which button I click, the same content from the last button added is displayed in the ...

Is there a way for me to view the contents within the box?

Hey there! I have a question about how to access the content inside this mysterious box "" within the style sheet. For instance, I am curious to find out the link of the image: .facebookicon::before { content: ""; } ...

How can one selectively apply a class to the initial DIV within a collection of dynamically generated DIV elements without relying on jQuery?

I am currently working on implementing a slideshow within a modal using AngularJS and Bootstrap. My challenge lies in dynamically creating DIVs, but specifically adding the active class to only the first DIV. Is there a way to achieve this without relying ...

How can I apply a unique CSS class to specific rows within an h:dataTable?

I am looking to apply unique CSS classes to certain rows within my <h:dataTable>. Is it possible to manipulate and style the individual table rows in a customized manner? ...

Tips for customizing the appearance of Java FX TableView column headers with CSS

I am relatively new to JavaFX and delving into CSS stylesheets, as well as using stackoverflow. I am curious about how one can customize the styling of a table column header. This is what my current column header looks like: Current appearance of my table ...

apply full width styling to bootstrap select dropdown

<div align="center" class="form-group"> <select v-model="subject" class="form-control"> <option v-for="n in papertypes" class="">{{ n.type }}</option> </select> <br> By default, Bootstrap makes the s ...

Utilizing Bootstrap 5: Breaking a Page into Two Separate Vertical Sections

I am looking to design a unique grid system that divides the page into either 2 columns or 2 rows that are completely separate from each other. This means that the content on one side of the page does not expand to match the height of the content on the ot ...

Is there a way to store a SAFEARRAY (an array of bytes) into an HTML hidden field?

Is there a way to extract an array of bytes from an active-x component, save it in an html-form input hidden field, and then send it to the server using form-submit? I'm not sure how to accomplish this. MIDL: HRESULT Data([out, retval] SAFEARRAY(VAR ...

As the div elements stack up and you scroll towards the top or bottom

I am dealing with two nested DIVs that are both scrollable. When I scroll the "inside div" (the green one in the attached file), and reach the bottom of the DIV, it starts scrolling the DIV beneath it. How can I prevent the DIV beneath from scrolling only ...

Step-by-step guide to creating a dropdown menu within a form element, with a variety of selectable values generated in

I am dealing with an array of employees who are assigned tests by a Lead. When tests are being assigned, the selected employee is allocated correctly. Upon review, the Lead can easily see which test is assigned to each employee. However, when the table is ...

What is the best way to extract additional values linked to the query within the array?

I have successfully retrieved all subcategories of a category using the code below. However, I am now facing an issue where I also want to fetch other fields associated with each subcategory, such as the price if the subcategory is a Product like Smartphon ...

Styling DL and DD elements with Zend Framework decorators

When incorporating a Subform named "Step1," your code will look like this: <dl class="zend_form"> <dt id="Step1-label"></dt> <dd id="Step1-element"> <fieldset id="fieldset-Step1" class="Step"> < ...

I am looking to verify a time input using Joi

I have created a form with date and time input fields. I utilized the joi library for input validation to ensure that the date and time inputs are not left empty separately. Although I have managed to apply date validation successfully, I am facing challen ...

Applying CSS styles to my container through a button click event

Currently, I am attempting to apply a padding-top to my container when a button is clicked. Unfortunately, the padding-top is not being added as expected. $('#login').on('click', function(){ $('#loginform1').css('padd ...

Display the initial page and activate the first navigation button when the page loads

Greetings to everyone. I am new to the world of jquery and currently in the process of learning. This is my script: <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"> </script> <script> $(function () { $ ...

Switch the dropdown menu to a button if it consists of only one option

I have been using a database within the Moodle Learning Management System that generates a bootstrap table. Here is an example of what it looks like: https://i.sstatic.net/UJc4M.png The last row in the table contains a dropdown menu. When viewing your ow ...

Animating a dropdown menu with jQuery

I have a typical submenu structure set up like this: <ul> <li> <a href="#">Parent link</a> <ul> <li><a href="#">Submenu link</a></li> </ul> </li> </ul> In my de ...

- bullet point: tabulated

I need to create an unordered list with URLs included and indented, while keeping all lines justified to the left. * line one text text ted more text text text * line two text ted more text text text * line three text ted more text text text ...