When hovering, the <a> element does not fully encompass the background color

I am currently working on a navigation bar that changes the background color of the links when hovered. However, I encountered an issue where the dropdown menu in the navigation bar does not cover the entire area.

body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  background-color: black;
}

ul li {
  display: inline-block;
  position: relative;
  background-color: black;
}

ul li a {
  display: block;
  color: white;
  text-decoration: none;
  text-align: center;
  margin: -left:0;
  padding: 15px 25px;
  -webkit-transition-duration: 0.4s;
  transition-duration: 0.4s;
}

ul li a:hover {
  background-color: #333;
}

ul li ul#dropdowncontent {
  display: inline-block;
  position: absolute;
  text-align: center;
  min-width: 100%;
  font-size: 70%;
  z-index: 999;
  width: 90px;
}
<ul>
  <li><a href="#">Products</a>
    <ul id="dropdowncontent">
      <li><a href="#">Motherboards</a></li>
      <li><a href="#">Processors</a></li>
      <li><a href="#">Hard Drives</a></li>
      <li><a href="#">Graphics Cards</a></li>
    </ul>
  </li>
  <li><a href="#">Stores</a></li>
  <li><a href="#">Contact</a></li>
  <li><a href="#">FAQs</a></li>
</ul>

You can view the code on Jsfiddle at this link: https://jsfiddle.net/fdvcmnx6/

Answer №1

Make sure to set the display property of your dropdown list items to block.

#dropdowncontent li {
    display: block;
}

body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  background-color: black;
}

ul li {
  display: inline-block;
  position: relative;
  background-color: black;
}

ul li a {
  display: block;
  color: white;
  text-decoration: none;
  text-align: center;
  margin: -left:0;
  padding: 15px 25px;
  -webkit-transition-duration: 0.4s;
  transition-duration: 0.4s;
}

ul li a:hover {
  background-color: #333;
}

ul li ul#dropdowncontent {
  display: inline-block;
  position: absolute;
  text-align: center;
  min-width: 100%;
  font-size: 70%;
  z-index: 999;
  width: 90px;
}

/* Additional */

#dropdowncontent li {
    display: block;
}
<ul>
  <li><a href="#">Products</a>
    <ul id="dropdowncontent">
      <li><a href="#">Motherboards</a></li>
      <li><a href="#">Processors</a></li>
      <li><a href="#">Hard Drives</a></li>
      <li><a href="#">Graphics Cards</a></li>
    </ul>
  </li>
  <li><a href="#">Stores</a></li>
  <li><a href="#">Contact</a></li>
  <li><a href="#">FAQs</a></li>
</ul>

Answer №2

Make sure to adjust the width of your dropdown list by setting width:100%

#dropdowncontent li {
    display: block;
}

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

Can the arrangement of icons/buttons on the JW Player control bar be customized?

I am trying to customize the skin of my JWplayer like this: This is my current progress: I have been researching how to rearrange the order of icon / button on the controlbar. According to the jwplayer documentation, the buttons on the controlbar are div ...

IItemTransform and minified files that are already in use

Summary: IItemTransform is not triggered when a minified file already exists in the same directory as the original (non-minified) file. Issue Description The problem arises due to CSS relative image references, impacting both JavaScript and CSS files when ...

I'm not sure how to use the global .container CSS style in Next.js for multiple pages

I'm currently diving into the world of React and Next.js, tackling a project with Next.js. I've set up a global CSS file with a .container style definition which I imported in my _app.js file. However, I'm unsure how to apply this global sty ...

Ways to adjust flex division to occupy the entire height of its container

https://i.sstatic.net/4YInI.png https://i.sstatic.net/hu8mG.png https://i.sstatic.net/Q51ha.png I am looking to activate an onClick event on the body where discussions and icons are located. While triggering onClick on the body itself poses no issue, I a ...

How to use CSS animations to remove an element from the DOM

My website has a structured HTML layout with product containers and individual products. I also have a JavaScript function that can remove any product from the page. <div class="products-container"> {foreach from=$cart.products item=product} & ...

Deselect the DOM element

Here is a jQuery code snippet: $(document).ready(function () { $(".story-area > h1, .story-area > p, .story-area > div > p").text(function () { return convertString($(this).text()); }); }); Additionally, there is a function de ...

Invalid template detected within the Kendo dropdown component

I am trying to create a template that will only be displayed if the data value is "Low". This is how I have set up my template column: template: '# if( data=="Low" ){#<span><i class="fa fa-square blue"></i> <span># } ' U ...

Select a color by inputting the type as "color" while leaving a space between the border and

I'm having an issue with the color input There seems to be a gap between the black border and the actual color, but I want to get rid of it #color-picker { border: 5px solid black; border-radius: 5px; width: 207px; height: 60px; padding: ...

Error copying cell width attribute when using border collapse

Currently, I am attempting to duplicate a table header by copying the tr HTML and then replicating the th widths. Unfortunately, this method is unsuccessful as the widths are displayed as (width minus W) pixels when border: 'Wpx' and border-colla ...

Using jQuery, you can easily update the value of the nth-child(n) for a cloned element

Looking to duplicate an HTML element and assign new values to its child elements? Here's my current HTML structure: <div id="avator" class="avator" style="display:none"> <label><a href="/cdn-cgi/l/email-protection" class="__cf_email ...

The dropdown menu extends beyond the boundaries of the page

I am attempting to incorporate a dropdown menu in the upper right corner. Here is what I currently have: https://i.sstatic.net/soHgc.png The dropdown menu that appears extends beyond the page boundaries. This is the code I am using: <link rel="st ...

Contrasting the distinctions between a pair of CSS class declarations

After using my customized CSS class named myclass alongside Bootstrap's built-in class container, I have a question about how to declare a div element with both classes. Should I go with: <div class="myclass container">some code</div> o ...

What are the steps to restrict the scope of a <style> declaration in HTML?

Currently, I am in the process of creating a user interface for a webmail. Whenever I receive an email, I extract its content and place it within a <div> element for display purposes. However, one challenge that I am facing is that each email contain ...

Effortlessly implement CSS styling in a scoped shadow element with Vue3

I am facing an issue with applying styles to an anchor element in my code. Below is a snippet of the code: <template> <custom-button> #shadow-root (open) <a href="#"></a> <other-custom></other-c ...

The image displaying "Loading" fails to appear

My webpage includes a div styled with the following CSS: .modal { display: none; position: fixed; z-index: 1000; top: 0; left: 0; height: 100%; width: 100%; background: rgba( 255, 255, 255, .35 ...

When it comes to creating a CSS drop-down menu, don't forget to incorporate an additional <ul

I've created a drop-down menu using CSS with 5 visible list items. When hovering over one of the list items, it highlights and triggers a drop-down effect to display another list underneath. Essentially, you have an initial set of options, and upon ho ...

Getting the height of a group of classes using the style attribute

How can I retrieve the current height of 813px in jQuery from the "style" section? An example of F12 CSS is shown here: My attempt at achieving this is as follows: function HandleAccordionControlSizeChanges(isOpened) { var currentHeight = 0; if ...

The mobile version is experiencing issues with the functionality of the responsive sidebar

Having an issue with the sidebar on this page. In the responsive version, particularly on smartphones, I can't get it to go underneath the content. The sidebar stays connected to the left but doesn't wrap around. Here is the CodePen link. If t ...

Customizing Semantic UI Navigation Menu Styles

For the first time, I am incorporating semantic-ui into my React application. The specific package I am working with can be found here: To display my header, I am using the following code: <Menu className='header' > <Containe ...

Ways to transform a 4-column CSS table into a 2-column layout for mobile and tablet viewing

For my latest project, I am faced with the challenge of creating a responsive table that transforms from 5 columns to 2 columns on smaller screens. While using complex jQuery is an option, I prefer a more semantic approach. Additionally, I need to incorpor ...