Transitioning background color on hover using HTML and CSS styling techniques

My goal is to change the background color of an element when the cursor hovers over it. Oddly enough, the transition is successful for altering the font size but not for the background color. Here is my CSS code:

.ul01 a,p{
    height: 100%;
    display: inline-block;
    color: white;
    text-align: center;
    padding: 16px 18px;
    text-decoration: none;
}

.companyName{
    float: left;
}

.ul01 a{
    padding-left: 30px;
    padding-right: 30px;
    width: 90px;
    float: right;
    transition: font 1s, background-color 1s;
}

.ul01 a:hover{
    background-color: red;
    font-size: 30px;
}

Below is the HTML code I'm using:

<nav id="nav">
<ul class="ul01">
    <li><p class="companyName"> Company Name </p></li>
    <li><p class="blank"></p></li>
    <li><a href="#facebook"> Facebook </a></li>
    <li><a href="#Twitter"> Twitter </a></li>
</ul>

This navigation bar design is simple and straightforward.

During my troubleshooting process, I discovered this line of code:

li a:hover:not(.active) {
    background-color: darkgray;
}

After removing this particular snippet, everything seems to be functioning correctly. Thank you all for your guidance - I'll be sure to double-check more carefully in the future.

Answer №1

I have found that this code functions properly on Chrome. However, in order to ensure compatibility with all browsers, it is recommended to specify the background-color for the non-hover state. This way, the browser will have a clear reference point for how to animate the color change when the element is hovered over.

Answer №2

Check out this code snippet - make sure your li has a style of display:block.

.ul01 a,p{
    height: 100%;
    display: inline-block;
    color: black;
    text-align: center;
    padding: 16px 18px;
    text-decoration: none;
}

li {
   display:block;
}

.ul01 a{
    padding-left: 30px;
    padding-right: 30px;
    width: 90px;
    float: right;
    transition: font 1s, background-color 1s;
}

.ul01 a:hover{
    background-color: red;
    font-size: 30px;
}
<nav id="nav">
<ul class="ul01">
  <li><p><a href="#facebook"> Facebook </a></p></li>
    <li><p><a href="#Twitter"> Twitter </a></p></li>
    <li><p class="companyName"> Company Name </p></li>
    <li><p class="blank"></p></li>
    <li><p><a href="#facebook"> Facebook </a></p></li>
    <li><p><a href="#Twitter"> Twitter </a></p></li>
</ul>

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

Having difficulty modifying the width of the BODY element using CSS

For my webpage, I want the body to have a silver background that fills only 75% of the page. This means that only 75% of the page will be painted silver, while the remaining part will be left unused and painted according to the browser's defaults. The ...

How to embed HTML code within PHP using a conditional variable

I've encountered an issue with a PHP code that sends emails containing HTML templates. The HTML template includes PHP variables such as Name and Email. <strong>Name: {name} </strong> As the email template is lengthy, I have included ...

AngularJS Filter from "start to finish" in values

I have been searching everywhere for a solution to this problem, but haven't found one yet. Any help would be greatly appreciated. Angular: var app = angular.module('app', []); app.controller('theController', ['$scope', ...

A New Approach to Initializing Web Pages in ASP.NET (with a Surprising Twist)

Well, I've encountered quite the puzzling issue with a project I'm working on (it could even make it to the Daily WTF), and I'm hoping for some help in finding a solution. (Apologies in advance for the lengthy explanation...) About a month ...

I've exhausted all options from stackoverflow with no success. Google Chrome's Autofill feature is wreaking havoc on my theme. Any suggestions on how to tackle this issue?

I designed a template with a stunning CSS layout. All I want is to have a transparent background with white font color, but Google Chrome seems to be causing issues with my theme. What steps should I take? I've exhausted all resources available onlin ...

"Enhance Your Website with Creative Image Hover Effects using HTML

I'm looking to add a simple hover effect to the images AboutUsLink.png and AboutUsColourLink.png, but I'm unsure of the process. Here is the HTML code: <section class="link1"> <a href="#SecondLink"><img src="images/LogoAndLin ...

What is causing the discrepancy in CSS line-height when text spans multiple lines?

Can anyone explain why there is extra space above the first paragraph in this HTML and CSS code, but not the second or third? The line-height is consistent for all three paragraphs. How can I adjust it so that there is equal spacing around all three paragr ...

I aim to have the capability to read numbers entered into text fields within a form prior to the form being submitted

I am currently working on a website where users can create brackets for the World Cup by guessing scores and winning teams. Right now, they have to manually fill in the bracket. I would like to implement a feature that reads what users are inputting while ...

Can the content inside HTML tags be susceptible to XSS attacks?

While working with Codeigniter, I have noticed that the xss_clean() function is replacing tab characters with a single space character. This behavior ends up causing issues when the content is later displayed within <pre><code></code>< ...

confirmation box for deleting a row in a grid view

I am looking to enhance the delete confirmation box on my Gridview delete function. Currently, I am using a basic Internet Explorer box for confirmation but I want to display a more stylish confirmation box. Here is the JavaScript code snippet within my gr ...

Tips for implementing events with AngularJS Bootstrap Colorpicker

I am having trouble understanding how events function with Angular Bootstrap Colorpicker. I have forked a Plunker from the developer's example. Unfortunately, there are no examples provided for using events. It would be great if events like colorpick ...

Highlighted Navigation Options

When visiting , take note of the top navigation bar. The background color on this page is white, but as you navigate to other pages, the tab changes to a different color. How can you modify this behavior and change the class? Is PHP or jQuery necessary f ...

AngularJS routing that is tailored to form input

I am trying to create a dynamic html page where the user can input form variables. My goal is to use AngularJS routing to navigate to different URLs based on the form inputs. Does anyone have advice or guidance on how I can achieve this functionality? ...

What are the steps to design a curved gauge with a linear-gradient background?

Looking to create a fully responsive gauge using only CSS, ranging from 0-100% with a gradient color transition from green to red. After encountering issues with existing examples, I conducted tests and ultimately developed a solution that somewhat meets t ...

How to retrieve the image source using jQuery

<img src="../img/arnold.png" alt="Arnold"> Is there a way to retrieve the absolute path of this image using jQuery? Currently, when I use img.attr("src"), it only returns "../img/arnold.png", but I need it to return something like "http://site.com/ ...

Arranging Pictures Beside Text Using CSS

I have a puzzling issue that I cannot seem to find an answer for despite the countless times it has been asked. In my footer, there is aligned copyright text, and I am attempting to add 3 logos to the right of it without any line breaks. However, when the ...

Transform the functionality of DIV and UL to mimic that of a SELECT element

I am attempting to update the text of the <span class="hideSelect"> element to match the text of the selected <li>, so that it functions like a <select> element. CSS: .myDrop { border: 1px solid #ddd; border-radius: 3px; padding: ...

Confirming the authenticity of a newly added user input with the help of jQuery

Within my current project, I have implemented validation features for text boxes. When a user clicks on a text box, the border is highlighted. If they click elsewhere without entering any value, the border turns red and an alert pop-up appears. In additio ...

I created an image that can be clicked on, but unfortunately it only functions properly on the

I am currently working on creating an image that can be clicked to cycle through different images all within the same frame. While I have managed to get it to work, I am facing a limitation where it only responds to one click. count = 1; function myF ...

Retain chosen option after form submission in PHP/HTML

I have seen many questions asked about this before, but despite trying various solutions here, none seem to work for me. I am populating a list from my mysqli database: <select multiple="multiple" name="formCountries[]" size="5" id="keuzeproduct"> ...