Is there a way to separate the sub-navigation ul from the main navigation ul?

My Content Management System generates a main-nav menu with one sub-menu. Both menus are styled using standard ul tags with a class of “navCMSListMenuUL” and then customized in my CSS. The problem arises when hovering over a link, causing the container to expand to fit the displayed sub-menu.

I am looking to have the sub-menu extend beyond the container and be positioned at a higher z-index than any elements below the container. While there are other improvements I would like to make to this navigation, fixing the sub-menu flow takes priority. I have created a simplified example in Codepen @ http://codepen.io/patrickTheWizard/pen/ZGWKYX .

<nav class="navigation">
<ul id="menuElem" class="navCMSListMenuUL">
    <li class="navCMSListMenuLI">
        <a class="navCMSListMenuLink" href="#">Home</a>
    </li>
    <li class="navCMSListMenuLI">
        <a class="navCMSListMenuLink" href="#">About Us</a>
    </li>
    <li class="navCMSListMenuLI">
        <a class="navCMSListMenuLink" href="#">Calendar</a>
    </li>
    <li class="navCMSListMenuHighlightedLI noseparator">
        <a class="navCMSListMenuLinkHighlighted" href="#">Members</a>
        <ul class="navCMSListMenuUL">
            <li class="navCMSListMenuLI">
                <a class="navCMSListMenuLink" href="#">Become a Member</a>
            </li>
            <li class="navCMSListMenuLI">
                <a class="navCMSListMenuLink" href="#">Board Meetings</a>
            </li>
            <li class="navCMSListMenuLI">
                <a class="navCMSListMenuLink" href="#">Committees</a>
            </li>
            <li class="navCMSListMenuLI">
                <a class="navCMSListMenuLink" href="#">Current Members</a>
            </li>
            <li class="navCMSListMenuLI">
                <a class="navCMSListMenuLink" href="#">Member Benefits</a>
            </li>
            <li class="navCMSListMenuLI noseparator">
                <a class="navCMSListMenuLink" href="#">Statistics &amp; Data</a>
            </li>

        </ul>
    </li>

</ul>

/* #01-Header/Nav# */
nav.navigation {
  clear: both;
  overflow: visible;
}

ul#menuElem {
  text-align: center;
  background-color: #3c3c3c;
  border-top: 6px solid #2a8cec;
  margin-bottom: 30px;
}

ul#menuElem li {
  display: inline-block;
  margin: 16px 0 15px 0;
}

ul#menuElem a {
  color: #fff;
  text-decoration: none;
  padding: 15px 35px;
  font-weight: 600;
  text-transform: uppercase;
  border-right: 1px solid;
  margin: 15px 0;
  margin-left: -4px;
}

ul#menuElem a:hover {
  background-color: #2a8cec;
}

ul#menuElem li ul {
  display: none;
  width: 100%;
  float: left;
}

ul#menuElem li:hover ul {
  display: block;
}

ul#menuElem ul li {
  display: block;
}

Answer №1

When working with menu-like code, it is common to encounter styles that override each other due to multiple nested UL, LI, and A elements.

A good practice is to use the ">" element in your style sheets to limit the scope of your query to direct children.

I have made some edits to your code, primarily introducing the direct child rule (>). I have also positioned the sub menu absolutely relative to its parent, preventing it from expanding and affecting the main menu size.

Click here for your updated code on CodePen.

Here is the modified code:

/* #01-Header/Nav# */
nav.navigation {
  clear: both;
  /* background-color: #3c3c3c; */
  overflow: visible;
  /* padding: 15px; */
  /* border-top: 6px solid #2a8cec; */
  /* margin-bottom: 30px; */
}

ul#menuElem {
  text-align: center;
  background-color: #3c3c3c;
  border-top: 6px solid #2a8cec;
  margin-bottom: 30px;
}

ul#menuElem > li {
  display: inline-block;
  position: relative;
  margin: 16px 0 15px 0;
}

ul#menuElem > li > a {
  color: #fff;
  text-decoration: none;
  padding: 15px 35px;
  font-weight: 600;
  text-transform: uppercase;
  border-right: 1px solid;
  margin: 15px 0;
  margin-left: -4px;
}

ul#menuElem > li > a:hover {
  background-color: #2a8cec;
}

ul#menuElem > li > ul {
  display: none;
  width: 100%;
  float: left;
}

ul#menuElem > li:hover ul {
  display: block;
  top: 30px;
  right: 0px;
  position: absolute;
  background: #FFF;
  padding: 0px;
  margin: 0px;
}

ul#menuElem > li:hover > ul > li{
  margin: 0px;
  padding: 0px;
  list-style: none;
  text-align: left;
  padding: 5px;
  line-height: 30px;
  border-bottom: solid 1px #dedede;
}

ul#menuElem > li:hover > ul > li > a{
  text-decoration: none;
  color: inherit;
}

ul#menuElem > li:hover > ul > li:hover {
  background: #000;
  color: #FFF;
}

ul#menuElem > ul > 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

Design the spans to have equal heights that adjust dynamically based on the content entered

I'm facing a challenge that I've been trying to resolve using just CSS, but I've hit a roadblock. The issue at hand involves aligning multiple spans next to each other and making them equal in height. The height is not fixed and depends on ...

The CSS is functioning properly on the local server, however, it is not loading

Feeling a bit overwhelmed... I've been developing an app using nitrous.io and everything was working perfectly (CSS displayed, able to login to Steam and save entries in the database). Exciting stuff! However, when I deployed it to Heroku, things star ...

Cube area to be filled

I am struggling to fill a cube with colors as it is only getting half of the figure filled. I suspect there might be an issue with the cubeIndices, but I'm having trouble figuring out how to make it fill everything. While I know I could use a cylinder ...

"What is the best way to switch between multiple data targets and toggles in bootstrap using the HTML data toggle attribute

I am currently coding an animated hamburger icon for the Bootstrap navbar, and I need to find a way to toggle both the hamburger icon and the responsive navbar. Is there a method or code snippet that allows me to toggle these two elements independently usi ...

ng-deep is causing changes in sibling components

Facing a challenge with Angular Material Design as I discovered the need to utilize ng-deep to customize the styling of an accordion. The issue is that when I use this method, it affects another accordion in a different section which is undesirable. Is th ...

The card's shadow effect is not functioning properly because the hover feature is not working correctly

I'm having an issue with getting the cards ('.card', '.card p' and '.card p:hover' classes) to darken their shadow upon hovering over them, unfortunately nothing happens. However, the hover function on the navigation bar ...

The JQuery Full Calendar embedded within a div tag will only become visible once the calendar click event is activated

Upon entering my page, various sections (images, calendar, info, etc) are displayed within div tags. These div tags are hidden or shown based on which navigation link is clicked on the left side of the page. However, I am facing an issue where the calendar ...

Having trouble accessing listview capabilities in jquerymobile?

I am currently delving into the world of jquerymobile. I am experimenting with a sample project where I aim to showcase a listview. Below is the code that I have been working on. <!doctype html> <html> <head> <link rel="stylesheet" h ...

How to Prompt CSS Rule Evaluation to Ensure Proper Functionality (Solution)

When encountering the issue of browsers, specifically Internet Explorer in my case, failing to correctly implement complex CSS rules, is there a way to force the evaluation? For example: body[data-some-flag="3"] #someElement .someClass svg stop:first-chi ...

Tips for updating existing data with Angular JS?

I'm looking to edit a posted JSON data form record by clicking a button, but I'm having trouble populating the form with the old data for editing. <button style="margin:3px;" ng-click="put1(student)" onclick="opendiv();">Edit</button> ...

Discover an effective way to display Ajax search results above a different div element!

I am having trouble with positioning my search results on top of other divs. To see the issue, you can watch the screen recording by clicking on this link: https://drive.google.com/file/d/1kU_vl2ZAmSCok0pxnTTvY hacxqcZZ9m_/view?usp=sharing These are the s ...

Change the positioning of the image to a new column within the bootstrap grid when a

My website utilizes Bootstrap 4, featuring a <container> with two individual columns. For screen sizes within the Bootstrap 4 breakpoints of md-xl, these columns are displayed side by side with equal widths: col-md-6. On smaller screens like sm-xs, ...

The background gently fades away, directing all attention to the popup form/element

After extensive searching, I have yet to find a straightforward solution that seems to be a standard practice on the web. My goal is to create a form that appears in the center of the screen when a button is clicked using CSS or jQuery. I would like the ...

verifying the user's screen dimensions

I'm currently exploring ways to customize the appearance of my website based on the viewer's screen resolution. I am interested in potentially optimizing the layout for vertical screens on mobile devices, and horizontal screens for laptops. Addit ...

Text arranged in a vertical orientation with a group of text aligned along the baseline

I am trying to create the following layout: https://i.sstatic.net/HYNDw.png Here is what I need: The word total and the number 120 should align vertically in the center The words per month should align at the bottom with respect to the number 120 The ...

The arrow extends just a hair below the boundaries of the div, by approximately 1 pixel

I am facing an issue with my nav bar code. In Firefox and Chrome, everything works perfectly fine - there is a small arrow displayed below the links when hovered over. However, in Internet Explorer, the arrow appears slightly below the div, causing only pa ...

Updating the functionality of one-page scrolling - changing to overlap instead of sliding upwards

Hello, I am currently utilizing a JavaScript library to create a website with full page scrolling. If you want to know about the library, check out this link: For those interested, here is an example of my work on jsfiddle: http://jsfiddle.net/aLjLjxux/ ...

Posting data using the form method="post" in Vue does not seem to be working

My goal is to create a website that allows users to reserve a seat using a specific password and time slot. I've set up a form where users can input the password and their name, then submit the form to make the reservation. However, I'm facing an ...

Alert: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Hostname or service could not be resolved

While attempting to establish a connection with my database, I encountered this error message. I am utilizing a paid server for my database. function establish_database_connection() { $username = "treq"; $password = "treq"; $hostname = "89.4 ...

Trigger the scrolling of one div when another div is scrolled

Link to Jsfiddle I'm attempting to activate my current scroll while I am outside that scroll, specifically in #DivDet Here is the code snippet of what I have tried so far: $("div#DivDet").scroll(function () { // Still trying to figure out what ...