There seems to be some spacing issues with the HTML menu items

Trying to customize my menu with two levels, I decided to remove the background color from the top level items by modifying the ".nav li" CSS class. However, this caused an issue with the display of sub-menu items, creating a 5px gap when hovering over the upper menu items. Adjusting the margin property in the ".nav li a" class resulted in the sub-menu items disappearing upon hover if set to less than 5.

.nav {
  width: 100%;
  margin-left: -30px;
  list-style-type: none;
  text-align: center;
  position: fixed;
  top: 0;
  z-index: 1000;
  display: inline-block;
}

.nav li {
  display: inline-block;
  position: relative;
  float: none;
  font-family: Arial, Helvetica, sans-serif;
  font-size: 14px;
}

.nav li:hover {
  background-color: #D1CA88;
  font-weight: 500;
}

.nav li a {
  display: inline-block;
  margin: 5px 0;
  text-decoration: none;
  width: 150px;
  line-height: 15px;
  color: #000000;
}

.nav li a:hover {
  background-color: #D1CA88;
  font-weight: 500;
  color: blue;
  font-size: 18px;
}
<ul class="nav">
  <li><a href="#">Contact Us</a>
    <ul>
      <li><a href="/contact_us.html">Staff</a></li>
      <li><a href="/contact_us.html#council">Council</a></li>
      <li><a href="/registration/input1.php">Registration Form</a></li>
      <li><a href="/map_and_directions.html">Map and Directions</a></li>
    </ul>
  </li>
</ul>

Is there a way to eliminate the background color for top-level items without gaps showing in the sub-menu items' background or making the sub-menu items disappear on hover?

Answer №1

Could you clarify if this is what you had in mind? I may need more information to fully grasp the objective.

.nav {
    width: 100%;
    /* Required for menu to float and center. Using explicit PX values doesn't yield desired results */
    margin-left: -30px;
    /* Adjusting for variance between page width and menu */
    list-style-type: none;
    /* Ensuring no bullets or other list styles */
    text-align: center;
    /* Necessary for menu alignment */
    position: fixed;
    /* Keeping menu bar fixed on top without scrolling */
    /* ALSO ENSURES MENUS REMAIN ON TOP OF SLIDESHOW*/
    top: 0;
    /* Placing menu bar at the top */
    z-index: 1000;
    /* Assigning high z-index to keep menu bar above all other elements, including slideshow */
    display: inline-block;
  }
  
  .nav li {
    display: inline-block;
    /* Allowing text to wrap */
    position: relative;
    /* Enabling drop-down functionality */
    float: none;
    /* Ensuring proper floating of menu items with window resizing */
    /*background-color: #BDB76B; */
    /* Setting menu background color */
    font-family: Arial, Helvetica, sans-serif;
    font-size: 14px;
  }
  
.target:hover {
    /* Highlight current hovered item when parent menu is hovered over */
    background-color: #D1CA88;
    font-weight: 500;
  }
  
  .nav li a {
    display: inline-block;
    /* Allowing text wrapping within menu items */
    margin: 5px 0;
    /* Spacing between menu elements (Top, Right, Bottom, Left) */
    text-decoration: none;
    width: 150px;
    /* Standard width of menu items */
    line-height: 15px;
    /* Text height of menu items */
    color: #000000;
    /* Font color of list items (000=black, FFF=white)*/
  }
  
  .nav li a:hover {
    /* Highlighting hovered item of parent menu */
    background-color: #D1CA88;
    font-weight: 500;
    color: blue;
    /* font-size: 18px; */
  }
  <ul class="nav">
    <li><a href="#">Contact Us</a>
      <ul>
        <li class="target"><a href="/contact_us.html">Staff</a></li>
        <li class="target"><a href="/contact_us.html#council">Council</a></li>
        <li class="target"><a href="/registration/input1.php">Registration Form</a></li>
        <li class="target"><a href="/map_and_directions.html">Map and Directions</a></li>
      </ul>
    </li>
  </ul>

Answer №2

Provide the precise selector by utilizing a particular CSS selector symbol. For instance: .nav > li (utilize greater than sign to target the exact next element).

To learn more about this topic, please refer to this article

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

Tips for implementing a linear gradient on a bar graph using Highcharts

I'm trying to create a bar chart using highcharts, and I want to apply a linear gradient for the bar fill color. However, I am not sure how to set this up. Can anyone provide some guidance or ideas? Here is an example of what I'm hoping to achie ...

Tips for dynamically resizing a div element as a user scrolls, allowing it to expand and contract based on

I was working on a project and everything seemed easy until I hit a roadblock. What I am trying to achieve is expanding a div to 100% when scrolling down to the bottom of the div, then shrink it back to 90% at the bottom and do the reverse when scrolling ...

Is there a way to clear out all items from the cart using PHP?

I have a shopping cart with items that I want to remove all at once. I tried creating a submit button with the post method called 'remove_all', but it's not working and showing an error message saying "The requested URL was not found on this ...

Creating horizontal cards with Angular MaterialWould you like to learn how to design

I need help converting these vertical cards into horizontal cards. Currently, I am utilizing the Angular Material Cards component. While I can successfully create cards in a vertical layout, my goal is to arrange them horizontally. Below is the code sni ...

Transform object into JSON format

Is there a way to transform an object into JSON and display it on an HTML page? let userInfo = { firstName: "O", lastName: "K", email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2b44476b445b05484446">[ema ...

Implementing validation for multiple email addresses in JavaScript: A step-by-step guide

Currently, I am utilizing Javascript to perform validation on my webpage. Specifically, I have successfully implemented email validation according to standard email format rules. However, I am now seeking assistance in enhancing the validation to allow for ...

Switch the CSS class with an HTML tag

Can someone help me transform this code snippet with Python 3 and Beautiful Soup? <span class="ld-nowrap"> 20th century’s </span> I need to convert it to: <em> 20th century’s </em> Any suggestions on how to achieve t ...

Learn how to dynamically assign a new ID and name to a cloned div element using jQuery based on the original element's ID

Within a DIV, there are two Input Textboxes present. I am attempting to duplicate the above div, meaning I want two additional textboxes cloned from the original ones. However, I am encountering challenges when it comes to assigning new IDs and Names to ...

Why isn't my div displaying in JavaScript?

Currently working on a project for the Odin Project front-end web development. My task involves creating a grid using javascript/jQuery. I attempted to use the createElement method but have encountered an issue in making the div visible within the html. Am ...

The Zingchart final element continually switches colors, creating a mismatch with the legend

I'm facing an issue with my zingchart where the last element's color doesn't match with the legend and keeps changing unlike the others. Any suggestions on how to fix this? The rest of my code works fine. I am fetching data from a MySQL data ...

Carousel with Horizontal Scrolling (Pause)

I stumbled upon this incredible "Pause Scroll (Horizontal) Parallax" created by Khaled on Codepen. One thing that has been puzzling me is how to make it run multiple times. It appears that when you try to apply it more than once, the duplicates ...

Adjust the height of the Twitter Widget to be proportionate to the height of its parent div dynamically

I have integrated the Twitter widget into my library management system, but I am facing an issue with its fixed height. Is there a way to make the height dynamic through CSS? Any suggestions on how this can be achieved will be greatly appreciated. Below ...

Determine total using PHP object

Can anyone assist me with adjusting this code? $aBlockVideos = (array) Advancedvideo_Service_Video::instance()->query("","v.total_comment DESC",1,5); I'm trying to figure out how to display the total number of comments for the current week. Any s ...

Is there an issue with the JavaScript functionality of the max/min button?

Help needed with maximize/minimize button functionality as my JavaScript code is not working. Any suggestions for a solution would be greatly appreciated. Thank you in advance. Below is the code snippet: <html> <head> <script> $(functio ...

Creating PNG images in a scripting language for web applications

Imagine giving your website user the ability to specify a radius size, let's say 5px, and in return receiving a PNG file with a circle of that radius. This question can be divided into two sections: In what language and using which technologies can ...

The Order of Transitions Reversed in CSS

I have a unique situation that I'm hoping to get some help with. I've been trying to create an effect where I move text away and lift up a gray box, and then when my mouse moves away from the gray box, the box goes down first before the text move ...

Position divs to be perfectly aligned and centered

I'm in the process of creating a webpage and facing challenges with aligning my divs properly, specifically the animated company logos. I want them to be positioned side by side rather than on top of each other, with space in between to fit the layou ...

Discover the size of an image using JavaScript by accessing innerHTML

I'm currently working on a unique AJAX function that retrieves image URLs without using node append for insertion. Instead, I opted to utilize innerHTML, but this has posed challenges in accurately obtaining the file dimensions. My current approach i ...

Is it possible to make the `<body>` element respect the offset (i.e. the user's current scroll position) when the 'custombox' modal is triggered?

I'm currently facing an issue with the scroll bars in the browser while using custombox.js in combination with BS4. Let me provide some context: I am working with a Custombox modal, which is similar to a BS4 modal. When a modal is opened in BS4, a c ...

Ways to include multiple tags ahead of one tag in BeautifulSoup

Looking for a solution: I have a single tag I want to insert three a tags before it with different text. Here is what I have tried: headTag = soup.find_all('h1', text='Attendance List') aTag = soup.new_tag('a') aTag['c ...