What is the best way to change the color of a website button upon hovering over it?

Hello everyone, I'm new to stackoverflow and seeking some guidance.

I am currently working on my website and I would like the background color of the top navigation links to change when hovered over. Specifically, not just the text but the entire area from top to bottom in the navigation bar. This will help users easily identify which link they are hovering over and also enhance the overall look of the website. Can anyone provide me with instructions on how to achieve this?

.header {
  top: 0;
  position: sticky;
  width: 100%;
  background: #f0f0f0; 
  clear: both;
  height: 80px;
  align-content: center;
  z-index: +2;
  overflow: hidden;
}
.header a:hover {
    background-color: #f2f2f2;
}
  #logo {
    position: sticky;
    float:left;
    margin:0;
    width: 220px;
    height: 60px;
    margin-left: 20px;
    margin-top: 10px; /* if you want it vertically middle of the navbar. */
  }
  
  #navlist {
    float: right;
      /* if you want it vertically middle of the navbar */
}
    
    /* nav list style */
    li {
    display: inline;
    list-style-type: square;
    padding-right: 20px;
    font-family: "Verdana", sans-serif;
    font-size: 25px;
    display: inline-block;
    vertical-align: top;
    line-height: 47.5px;  
    height: 80px;
}
        
/* link style */
a:link {
  text-decoration: none;
  color: black;
}

a:visited {
  text-decoration: none;
  color: black;
}

a:hover {
  text-decoration: none;
  color: #ffffff;
}

a:active {
  text-decoration: none;
}
<div class="header">
  <a href="homepage.html"><img id="logo" href="homepage.html" src="https://i.imgur.com/PTxdlc0.png"></a>
  <ul id="navlist">
    <li id="active"><a href="alliance.html">ALLIANCE</a></li>
    <li><a href="asp.html">ASP</a></li>
    <li><a href="diamondback.html">DIAMONDBACK</a></li>
    <li><a href="type.html">TYPE</a></li>
    <li><a href="info.html">INFO</a></li>
  </ul>
</div>

Answer №1

Insert the specified code into your CSS file:

.active {
  background-color: blue;
}

Modify the following:

<li id="active"><a href="alliance.html">ALLIANCE</a></li>

to

<li class="active"><a href="alliance.html">ALLIANCE</a></li>

Now, when a link with the class "active" is clicked, it will have a blue background color. Feel free to customize the color as needed.

Example solution can be found here

Answer №2

To achieve the desired effect, you can simply transfer the :hover selector to the parent <li></li> element that contains the <a></a> tag duo, and then ensure these list items span from top to bottom.

ul#navlist li:hover { background-color: red; }

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

HTML/CSS: There is a sentence that has no spaces that is exiting outside a div

HTML: <div> bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb </div> CSS: div { width: 150px; border: 1px solid blue } DEMO: http://example.com QUESTION: How can we solve this issue? ...

"Troubleshooting: Why is the JavaScript canvas.toDataURL method returning an

While I know this question has been asked multiple times before, I still haven't been able to find a solution. My goal is to take an image, rotate it, and place it in an HTML canvas. To achieve this, I am utilizing another canvas where I rotate the i ...

Tips for maintaining a single-line div while adding ellipses:

What is the CSS way to ensure that a div or class contains only one line of text, with ellipses added if it exceeds that limit? I am aware that setting a specific height and using overflow:hidden can achieve this, but it doesn't quite work for my need ...

Ways to include form fields dynamically by using the field type

I need assistance creating a dynamic form that allows users to add form fields with various input types. An example is shown below: https://i.sstatic.net/JZ1Ye.png Although I have made an attempt, I am struggling with implementing the input type function ...

Resetting values in Javascript and JQuery when clicking a button

I've recently implemented a feature on my website header where clicking on the search button reveals a search bar, and clicking on the account button reveals an account bar. With some valuable assistance from Madalin, I was able to achieve this functi ...

chart.js version 3 does not display the correct date data on the time axis

Struggling to make chart.js work with a time axis is proving to be quite challenging for me. The code I have is as follows: <html> <head> <script src="https://cdn.jsdelivr.net/npm/moment"></script> <script src="https://cdnjs.clo ...

Bootstrap 5 alert: The function `$(...).carousel` is not recognized

Despite browsing through numerous similar questions, none of the solutions seem to work for my issue. Listed below are some points I have checked: Jquery is loaded before bootstrap Bootstrap libraries are up to date Confirmation that bootstrap.min. ...

Guide to building a dynamic Slick slider complete with a sleek progress bar

I'm looking to customize my slider with a timer pagination feature using progress bars instead of dots, similar to how it is on . Currently, I am using slick.js for my slider implementation. What is the best way to replace the default pagination butt ...

Does the downloading of images get affected when the CSS file has the disabled attribute?

Is it possible to delay the download of images on a website by setting the stylesheet to 'disabled'? For example: <link id="imagesCSS" rel="stylesheet" type="text/css" href="images.css" disabled> My idea is to enable the link later to tri ...

The mysterious workings of the parseInt() function

As I begin my journey to self-teach JavaScript using HeadFirst JavaScript, I've encountered a minor obstacle. The chapter I'm currently studying delves into handling data input in forms. The issue arises when I attempt to utilize the updateOrder( ...

Why do the fontawesome icons fail to appear in my SVG file?

I've encountered a peculiar issue where icons (from fontawesome) are not appearing in my svg. They have always displayed correctly on my personal computer, so for a while I didn't realize that others were having trouble seeing them. For instance ...

The issue with the CSS combination of :after and :hover:after across different HTML elements

Encountering an issue while attempting to create a rollover effect using CSS :after and :hover pseudo-elements. Check out the clock and facebook icons on the right side by visiting: See below for the CSS code: .icon { background: url('. ...

Applying a specified style to a pseudo element using the ::after selector to display an

Can anyone help me with adding a pseudo element ::after containing an icon (from FontAwesome) to my Bootstrap Vue ProgressBar? I want this icon to be dynamic and move along the progress bar line when I click. Here is my current implementation, where the Pr ...

Error 405: Angular encounters a method not supported while attempting to delete the entity

I have developed an application that performs CRUD operations on a list of entities. However, when attempting to delete an entity, the dialog box does not appear as expected. To start, I have a HttpService serving as the foundation for the CRUD operations ...

Show the data from the chosen array using Vue JS

Just diving into Vue JS and eager to master it through creating a simple note-taking app. The concept is straightforward - a list of all notes (their titles) on the left, and when you click on a note title, the corresponding note text is displayed in a te ...

Incorporating Local Storage into a To-Do List Application

I followed a tutorial from w3schools to create a custom task list, tweaking the code to fit my requirements. My goal is to ensure that when I click the save button in the toolbar at the top and then refresh the page, the added tasks are preserved. I&apos ...

Using htmlentities in PHP while maintaining the integrity of HTML tags

I am looking to convert text within a string into HTML entities while still maintaining the HTML tags. For instance: <p><font style="color:#FF0000">Camión español</font></p> The desired output would be: <p><font style= ...

Ways to prevent a page from scrolling

I'm currently working on a webpage that features a vertical drop-down menu. The issue I'm facing is that when the menu drops down, it causes the text below it to be pushed downwards and off the page. While this behavior is anticipated, it also tr ...

In the Bootstrap grid, the first two rows are tightly packed with no empty space between them

Currently working on coding a footer with a grid/table using Bootstrap 4. However, encountering an issue where there's no spacing between the first and second rows in the grid. Oddly enough, there is spacing between all other columns. Can anyone provi ...

Tips for dynamically setting up an inputStream in a Java <audio> tag within an HTML5 environment

I need to incorporate a dynamic inputStream from a web service into an HTML5 audio tag so that users can play it. How can I programmatically set the content of the HTML5 tag to achieve this? ...