Hovering over a dropdown menu results in the color not completely filling the background of the desired element

.nav-main {
    background-color:#09F;
    width:100%;
    height:100px;
    color:#FFF;
    line-height:40px;
}
.nav-main .logo{
    float:left;
    padding: 40px 5px;
    font-size:1.4em;
    line-height: 20px;
        }
.nav-main > ul {
    margin:0;
    padding:0;
    float:left;
    list-style-type:none;
    }
.nav-main > ul > li {
    float:left;
    }
.nav-item{
    display:inline-block;
    padding: 40px 15px;
    height:20px;
    line-height:20px;
    text-decoration:none;
    color:#FFF;
    }
.nav-item:hover {
    background-color:#03C;

}
.nav-content {
    position:absolute;
    top:100px;
    overflow:hidden;
    background-color:#09F;
    opacity: .9;
    max-height:0px; 
    border:1px;
    box-shadow: 1px 1px #FFF;
}
.nav-content a{
    color:#fff;
    text-decoration:none;
    }
.nav-content a:hover{
    text-decoration:underline;
    background-color:#03C;
        }
.nav-sub {
    padding:20px;

}
.nav-sub ul {
    padding:0;
    margin:0;
    list-style-type:none;
}
.nav-sub ul li a{
    display:inline-block;
    text-align:center;
    }

.nav-item:focus ~ .nav-content{
    max-height:400px;
    -webkit-transition:max-height 400ms ease-in;
    -moz-transition:max-height 400ms ease-in;
    }

Greetings, I am encountering an issue where the hover element's background is not filling entirely with #03C as desired. It seems to only cover the length of the text in the unordered list items. I recently began learning HTML5 & CSS, so I am unsure about what may be causing this problem. Your assistance in explaining this matter would be greatly appreciated. Thank you for your guidance.

Answer №1

To achieve the desired effect of a big background on-hover, it is necessary to apply padding to the anchor element rather than its container. I have also made a modification to the display property of the anchor element from inline-block to block in order for them to be displayed full width within the nav container.

The css has been updated as follows:

.nav-sub {
  // removed the padding from here
}

.nav-sub ul {
  padding: 0;
  margin: 0;
  list-style-type: none;
}

.nav-sub ul li a {
  display: block;
  text-align: center;
  padding: 20px;
}

Here's the fiddle https://jsfiddle.net/f1edrmo5/1/

I would suggest taking a look at HTML5 page structure as well - it can be very helpful in ensuring your page markup is correct. I refer to it regularly myself :-)

Best of luck!

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

Sass - Retain the original class hierarchy

Apologies for the vague title, I couldn't think of a better one. As I compile my scss, this piece of code: .foo { ... &__bar { ... } } transforms into the expected output below: .foo { ... } .foo__bar { ... } However, I actually need it t ...

Guide on navigating through various HTML pages with distinct parameters using Node.js (Express server)

Seeking assistance with a Node.js server that receives an ID as a query parameter. Each time a client connects with different parameters, I aim to serve them a unique HTML page containing a simple UI with 2 dynamic arrays. Everything seems to be working co ...

How to style a div for printing using CSS

Currently, I am working on a project that has already been completed but now requires some enhancements. To give you an overview, the project includes a search functionality that displays additional details upon clicking on the displayed name in the result ...

Decrease in font size observed after implementing Bootstrap 5

The issue arises when I include the Boostrap CDN link, resulting in a change in font size. I discovered that Bootstrap has a default font size, which is why attempts to adjust it using an external style sheet with !important do not succeed. Interestingly, ...

Ruby on Rails slider bar functionality

Currently, I am developing a video-focused application using Ruby 1.9.2 and Rails 3.1. One of the key features I need to implement is a slider bar that allows users to adjust the total duration of a video in real-time. Despite my attempts to use HTML5 fo ...

Customize your Bootstrap navbar dropdown to showcase menu items in a horizontal layout

I have been developing a new project that requires a navbar to be positioned at the center of the page, at the top. The current HTML code I am using is as follows... <div class="navbar navbar-inverse navbar-fixed-top center"> <div class="con ...

Center Items in Flexbox, Implementing React Material with React

In my React App, I am utilizing flexbox and React Material to align the searchbar and clear button on the same line with space between them. Here is the link for reference: CLICK HERE <div className={classes.searchBarSection}> <Paper com ...

Positioning images within a relatively positioned div using absolute positioning

I have come across multiple discussions on this topic, but I am still struggling to make the following code snippet function correctly: .container { position: relative; width: 100%; } .left { position: absolute; left: 0px; } .right { positio ...

Transmit data from list items in the HTML to a form

Based on my understanding, there are limited options available to style an <option> tag within a <select> dropdown and it appears quite plain. So I was thinking about creating a more visually appealing dropdown using <ul> <li> tags ...

personalized styles based on user preferences

My website features a gaming section where users can view quick status updates of their stats using colors like blue, red, and green. I am looking to generate something similar for each user. Here is what I have so far: <style> .box2 { height: ...

css webpack react component not displaying background images

I'm facing an issue where I can't seem to get the image to display as a background. If I just attach the image directly, it shows up without any problems. However, my intention is to make it a background image so that I can overlay text on top of ...

What is the method for obtaining a unique id that changes dynamically?

Having trouble accessing the dynamically generated ID in jQuery? It seems to work fine in JavaScript but not in jQuery. Here's an example of the issue: My jQuery code: var img = $("#MAP"+current_img_height); $("#map").css({'height': img.h ...

CSS - Positioning a rectangle between two squares

I need help creating a layout with 2 rows of 4 squares, each with a rectangle in the middle of two squares. https://i.sstatic.net/9pWBi.png This is the code I currently have: .main { width: 100%; height: auto; } .square { width: 25%; position: ...

Incorporating a recurring image beneath a navigation menu

I'm attempting to recreate a header that has a similar appearance to the following: https://i.stack.imgur.com/uVXs3.png However, I am facing challenges in generating the repeating diamond design beneath the black bar. The diamond pattern resembles t ...

Styling with CSS in Angular 2+ can be quite challenging

Hey there, I'm new to Angular 4 and running into some troubles with styling my application. I tried adding a background image to the body, which worked fine, and then added a component to display content, also looking good. Now, when I added a second ...

Limit a div to only accept a specific stylesheet

Currently, I am immersed in a project that involves HTML5, MVC 4, CSS 3, JQuery, and LINQ. We have meticulously designed styles for various ui, li, and other html controls within the project. However, I now face the challenge of integrating a JQ Grid (htt ...

Modify the position of the CSS background for the Y-axis using jQuery

Let's consider a scenario with the following table: <table> <tr> <td class="t"></td> <td class="e"></td> <td class="s"></td> <td class="t"></td> </ ...

Tips for customizing bootstrap's fixed navbar-default to ensure that the list items align downwards:

Is it possible to customize the bootstrap fixed navbar-default so that the li elements align downward instead of at the top? If you have any corrections or custom solutions, I'd love to see them! Feel free to share your code on platforms like CodePen, ...

Blending a background image with CSS

I've set a background image for the div, which is also used for responsive design. Check out the demo My goal is to ensure that the image doesn't appear cut off in the responsive view. I'm looking for a CSS solution to blend the image wit ...

What causes jQuery to not work correctly during keydown events?

I've been working on this external jQuery code: jQuery(document).one('keydown', 'g',function (evt){ if ($("#tb").html() == "0") { $("#tb").html("Testing the chicken.") } else {$("#tb").html("Chickens fart too." ...