What is the best way to modify the background color of a whole div when hovering over it

Recently, I encountered a challenge while working on my website. I want to set up a link inside a div that changes color when hovered over. However, the desired effect is not quite coming out as expected. Before hovering, I aim to have a border around the div matching the color of the text in the link. The background should be white. Upon hover, the background must shift to the color of the text and border, with the text turning white. Unfortunately, due to the padding between the link and div border, the outcome is not ideal. Here is the source code in HTML/CSS:

HTML:

<div id="home">
    <a href="index.html">HOME</a>
</div>

CSS:

#home {
    border: 4px solid #00a651;
    font-size: 30px;
    padding: 10px 20px 10px 20px;
    margin: 20px 100px 20px 100px;
    display: inline-block;
}
#home a {
    background: #ffffff;
    color: #00a651; 
    text-align: center;
}
#home a:hover {
    background: #00a651;
    color: #ffffff;
}

Upon hover, only the link changes color while the padding stays white when the cursor hovers over the link or anywhere else within the div. How can I adjust the code so the color changes take place when any part of the div is hovered over, and the entire div shifts colors? Your help would be greatly appreciated. Thank you, Brandon :)

Answer №1

#page {
    font-size: 30px;
    margin: 20px 100px 20px 100px;
    display: inline-block;
}
#page a {background: #ffffff;
    border: 4px solid #00a651;
    padding: 10px 20px 10px 20px;
         color: #00a651; 
         text-align: center;
}
#page a:hover {background: #00a651;
               color: #ffffff;
}
<div id="page">
    <a href="index.html">PAGE</a>
</div>

Answer №2

To implement the hover effect, make sure to apply it to both the div and the anchor elements:

#home {
    border: 4px solid #00a651;
    font-size: 30px;
    padding: 10px 20px 10px 20px;
    margin: 20px 100px 20px 100px;
    display: inline-block;
}
#home a {
  background: #ffffff;
  color: #00a651; 
  text-align: center;
}
#home:hover {
  background: #00a651;
  color: #ffffff;
}

#home:hover a {
    background: #00a651;
    color: #ffffff;
}
<div id="home">
    <a href="index.html">HOME</a>
</div>

Answer №3

If you are familiar with how to utilize it, I suggest making use of the jQuery library:

You can either link your jQuery download directly to your main html file or opt for a CDN.

To achieve a hover effect on your link in JavaScript, you can alter the color of the div when hovering over the link and simultaneously change the color of the link as well. Here's an example:

 $(document).ready(function(){
   $('a').hover(function(){
     $('#home').css("background-color","#00a651");
     $('a').css("color", "chooseacolor");
  });
});

Without full clarity on your query, this response is the best guidance I can provide!

Answer №4

Utilizing :hover on DIV elements may not be the most reliable choice, considering it is not universally supported across all browsers and versions. Perhaps using some JavaScript would be a more stable solution?

Alternatively, if you require the entire area (including padding) within the DIV to be clickable, you could adjust the CSS by setting it to "display:block".

$(document).ready(function() {
    $('#home').hover(function() {
        $(this).css('background-color', '#00a651').find('a').css({
          backgroundColor: '#00a651',
          color: '#FFFFFF'});        
    }, function() {
        $(this).css('background-color', '#FFFFFF').find('a').css({
          backgroundColor: '#ffffff',
          color: '#00a651'});        
    });
});
#home {
    border: 4px solid #00a651;
    font-size: 30px;
    padding: 10px 20px 10px 20px;
    margin: 20px 100px 20px 100px;
    display: inline-block;
}
#home a {background: #ffffff;
        color: #00a651; 
        text-align: center;
}
/* Utilize a script instead of :hover */
<!-- Add this to the HEAD section if it's not already there -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

<div id="home">
    <a href="index.html">HOME</a>
</div>    

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

Collaborating with interactive icon in input group

I'm having trouble getting the textbox and icon to be in the same line, like a bootstrap input group. The icon is also clickable. <div class="input-group"> <asp:textbox id="txtParentCategoryCode" runat="server" cssclass="input-text normal" m ...

Passing information using AJAX, utilize the data as a paragraph in PHP

As someone with limited experience in PHP, I've been attempting to create a basic "adminpage" on my own. However, I've run into an issue when passing a value via AJAX from the adminpage to the index page - the value does not appear in the paragra ...

I'm currently on the hunt for the most efficient method of parsing through HTML code

Currently, I am developing a school application that reads the subject name and class name to determine attendance. The goal is to calculate the attendance rate for each school subject throughout the year. This piece of code represents just one day, but I ...

JavaScript: Navigating Through Frames and iFrames in Internet Explorer

Is there a way to run JavaScript code after my iFrames or Frames have finished loading? It seems like document.onReady fires before the frames are actually displayed on the page. Any ideas on how to make this work? I came across a plugin called FrameReady ...

Alteration of icon size in input field using jQuery unintentionally

My issue involves an input field where users can input text from an array of emojis. When a user selects an emoji, it should display as an icon styled by an external stylesheet. However, there are two problems: The name of the icon appears on top of the ...

Customized input field design for a discussion board platform

I am in the process of creating a forum-style website and I am in search of a unique open source HTML template that includes an editable text box where users can input their posts, similar to what is seen when posting a question or answer. I believe there ...

How do I eliminate excess space following the resizing of the Material UI Drawer component?

I adjusted the size of my MUI drawer to a specific width and now I am facing an issue where there is extra space left after resizing. This results in a margin-like space between the drawer and the Lorem Ipsum text content. My goal is to eliminate this rema ...

Ensure that the text within the ng-repeat is wrapped in a span element and each

When using ng repeat in span, I encountered a word breaking into a new line. Here is the actual output: https://i.stack.imgur.com/k4c9k.png To style this, I implemented the following CSS: border: 1px solid #ECE9E6; border-radius: 3px; text- ...

Accessing JavaScript results in PHP

Hello everyone! I am working on creating a dropdown menu for selecting time using JavaScript to get the computer's current time. My goal is to have an output that automatically selects the option in the dropdown if it matches the computer's time. ...

.value not showing correct data in JavaScript

I am attempting to retrieve the value entered by the user in an HTML input field. While I can display a fixed, predetermined value with ease, this is not my intention. My goal is for users to input a numerical value and for that value to be displayed/adj ...

Getting the text from an HTML input field requires accessing the value property of the

My goal is to generate a pdf report using php. The user will enter their name in an input box, which will then be passed to the second php page that searches the mysql database. Issue: When the user inputs their name, attempting to retrieve the data (var ...

A guide on converting array values to objects in AngularJS HTML

Here is my collection of objects: MyCart = { cartID: "cart101", listProducts : [ {pid:101, pname:"apple", price: 200, qty:3}, {pid:102, pname:"banana", price: 100, qty:12} ] } I have incorporated a form in ...

Implementing JavaScript functionality upon page load with ASP.NET

I have come across a situation where I am trying to integrate working code (jQuery/Javascript) that makes an API call and sends data to it. The API service then responds with a success or failure message based on the data insertion into the API db. Everyth ...

The overlay background is not covering the entire height of the container

The CSS includes a .wrapper with the style min-height: calc(50% - 30px), along with an overlay blur div to cover the entire height of the .wrapper element. Here's a link to a Codepen example showcasing this setup: https://codepen.io/palaniichukdmytro/ ...

How can I make text wrap instead of overflowing to ellipsis in a list when using vue-material?

Question: <md-list-item> <md-icon v-bind:style="{color: transcript.color}">account_circle</md-icon> <div class="md-list-item-text"> <p>{{ transcript.text }}</p> </di ...

Get the username from Parse instead of using the ObjectID

When using angular and Parse for JavaScript, I have implemented a search field where an admin can input the objectid of a user to display their details. However, I would like to modify this functionality so that the admin can enter the username instead of ...

Centered vertically on the right side of a div, a CSS arrow is positioned

Hello, I am seeking a way to vertically center a CSS triangle/arrow on the right side of a div using pure CSS. Unfortunately, I can't include images due to my low reputation. Currently, my code is not functioning properly and lacks cross-browser comp ...

Tips for Placing a Div in a Precise Location

I'm currently working with Bootstrap and I'm attempting to replicate a layout similar to the one used by Twitter, as shown in the screenshot below. The specific part I'm struggling with is where the profile picture is located. I want to add ...

The ng-repeat function is unable to fetch data from a JSON file

Within my AngularJS framework template, I have the following snippet of html code: <ul class="sb-parentlist"> <h1 class={{headerClass}}> Popular</h1><div data-ng-repeat="data in data track by $index"> <li> ...

Hover effect transition malfunctioning

I've been attempting to incorporate a hover image effect on my website. I included the script in the link below. However, I'm struggling to achieve a smooth fade transition and a 2-second delay on the hover image. Can someone please assist me wit ...