css issue with styling on a and a:hover elements

I'm having trouble getting the CSS for 'a' and 'a:hover' to work properly. The background of the li elements isn't changing to blue when I hover over them. Can anyone provide some assistance? Your help would be greatly appreciated.

Here is the HTML:

<body>
    <div id="navbar">
         <ul>
             <li><a href="#">home</a></li>
             <li><a href="#">nature</a></li>
             <li><a href="#">travel</a></li>
             <li><a href="#">people</a></li>
             <li><a href="#">random</a></li>
             <li><a href="#">bio</a></li>
             <li><a href="#">contact</a></li>
          </ul>
     </div>
 </body>    

The corresponding CSS is:

#navbar ul{
    float:left;
    margin:0;
    width:600px;
    list-style : none;
    padding-top:30px;
 }

 #navbar ul li
 {
      vertical-align: middle;
      display: inline;
      margin: 0;
      text-align:center;
      font-family: Calibri;
      padding-left:15px;
      font-size:20px;
      font-weight:normal;
      list-style-type: none;
      color:#808080;
}

#navbar ul li a
{
    text-decoration: none;
    white-space: nowrap;
    line-height: 45px;
    padding: 5px 5px;
    -webkit-border-radius: 4px;
    -moz-border-radius: 4px;
     border-radius: 4px;
     color: #666;
 }

 #navbar ul li a : hover 
 {
     color:#FFFF00;
     background-color: #4caef2; 
 } 

Answer №1

Avoid including unnecessary spaces in your selectors.

For example, consider this mistake:

#main .section div : hover

To correct it, eliminate all extra spaces except for the necessary descendant combinators:

#main .section div:hover

This issue could have been caught by running your code through a CSS validator.

Answer №2

Kindly ensure there is no space between the colon and 'hover', like so:

To make navbar ul li a: hover correctly render, it should be changed to #navbar ul li a:hover

Answer №3

modify

#navbar ul li a : hover 

into

#navbar ul li a:hover 

kindly

If you wish to use a pseudo class like hover, it must be added directly after the selector with no extra spaces, separated by a ':'.

Answer №4

When using pseudo-classes, remember this syntax:

selector:pseudoclass

For example, in your situation:

#menu ul li a:hover 

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

CSS container design with customized paragraph formatting

How can I format a CSS container for quotes that span multiple paragraphs? Here is an example of the code: #quotebox { width: 80%; border-top: 1px solid #242729; padding-left: 10%; padding-bottom: 2%; } I am currently using this CSS container for quotes, ...

jQuery's visibility check function is not functioning properly

I am developing a web application for managing orders and finance in a restaurant. To enhance the functionality of my application, I require more screens to operate with. To facilitate this, I have implemented a small function to toggle between visibility: ...

Ebook document featuring a dual-column layout

Seeking recommendations for EPUB files that contain two columns. I'm struggling to implement two columns in my CSS file. ...

Why does the styling of the inner Span adhere to the style of the outer Span?

How can I adjust the opacity of the color "blue" to 1 in the code snippet below? <!DOCTYPE html> <html> <body> <p>My mom's eyes are <span style="color:blue;font-weight:bold;opacity:0"> <span style="color:blue;fo ...

Make the HTML element expand to the remaining height of the viewport

Need help with creating a section that automatically fills the remaining viewport height below the navigation bar. The section includes a centered div and background image, along with a button at the bottom for scrolling down by one full viewport height. H ...

What is the best way to create horizontal lines that span multiple Bootstrap columns?

I am working with a Bootstrap grid that consists of three columns in a row. My goal is to add a line across all columns under every row of content written in the columns. <!-- Bootstrap CSS --> <link rel="stylesheet" href="https://stackpath.boo ...

Adjust dimensions of an image retrieved from a URL

Is there a way to adjust the size of the image displayed here?: var picture = new Image(); picture.src = 'http://www.example.com/images/logo.png'; picture.width = 200; //trying to change the width of the image $('canvas').css({ ...

What is the best way to alter the color of the text cursor using html/css?

I'm a beginner when it comes to HTML and CSS, so my knowledge about them is quite limited. I recently encountered an issue where my text cursor disappears when the background-color of a <div> element is dark. Here's an example: <style&g ...

Working with double quotes within variable in JavaScript

I am currently working on dynamically creating HTML tags using JavaScript. Please take a look at the code snippet below: function getPhotosSuccess(data) { if (data.d.results.length > 0) { var response = data.d.results; var innerht ...

Challenges with moving images in Unslider

There seems to be an issue with the unslider-arrows on the unslider banner. The images are not sliding properly when transitioning to the next image without user input. Instead, they resize from small to full size starting at the top of the .banner div. ...

Utilizing AngularJS to display an array of user inputs

Hey there, hope all is well with you! I have been working on an HTML form that looks like this: <div class="table-responsive " > <table class="table table-bordered"> <tr ng-repeat="x in [1, 2, 3, 4, 5]"> <td> <td align="center" ...

Tornadofx highlight selected row with CSS

I am attempting to modify the background color of a selected row and apply the same change to a listview. When I use cell{backgroundColor += Color.BLACK}, it successfully changes the color, but it does not retain the selection color as intended.I have al ...

Stop Wordpress from automatically preloading html5 videos

I have a WordPress page containing numerous videos and I am looking to prevent them from preloading using HTML5. The challenge is that I am inserting the videos through the featured media options, which means I cannot directly add the preload="none" attrib ...

Separate compound words without hyphens at the boundaries of each word

I am currently incorporating the use of Bootstrap for my project. Let's assume that I have text displayed in this way: "Stackoverflow" Is there a method to automatically ensure that it breaks like below if the text exceeds the container: "Stack ...

Discover how to prevent a link or text from appearing if a user has already browsed a particular webpage

Is there a way to hide a link to another page if the user has previously visited that page? For example, I have 3 options - A, B, and C. If a user selects option A, links to pages B and C are displayed at the bottom of the page. However, if they then navi ...

A guide to revealing the value of a button or anchor tag using AngularJS

I'm working on a way to show a button or anchor tag value depending on the true or false value returned by my angular variable. Here's a snippet of my code: <a href="#" id="verify">{{userInformation.value}}</a> The userInformation.v ...

How can I make a span take up the full width within a button using CSS?

Bootstrap and custom CSS are being used for buttons, but there is an issue. <button type="button" class="dropdown-item"> <span (click)="showI('test_data)" class="text-capitalize&q ...

How to center a fixed div horizontally using CSS?

#menu { position: fixed; width: 500px; background: rgb(255, 255, 255); /* The Fallback */ background: rgba(255, 255, 255, 0.8); margin-top: 30px; } I've searched high and low for a solution to my problem, but nothing seems to work ...

Leveraging jQuery to implement onclick functionality within a Jinja2 loop

I'm currently working with Python, Flask, and Jinja2. When using a for loop, I want to be able to click on the {{ place.place_photo }} element to toggle its details. Initially, I had it functioning correctly but ran into an issue where clicking on on ...

Tips for creating a hyperlink to a particular tab

Please click here for the demo I am attempting to generate links like this page1.html#section1, page2.html#section2, but these links are not functioning correctly. Relevant code snippet function showSection( sectionID ) { $('div.section'). ...