A full-width CSS menu featuring dropdowns beneath each entry for easy navigation

Check out the JSFiddle here

I've created a full-width CSS menu that spans the entire screen, but I'm struggling to figure out how to make the corresponding subnav items appear below their parent items.

Is it even possible to achieve this design? I haven't been able to find any resources online that show exactly what I want. However, I'm convinced that it must be achievable, right?

The number of top menu items should be flexible so that, regardless of how many there are, the menu will adapt accordingly.

Click on the link above to view the current state of my code in the JSFiddle.

HTML:

<div class="nav">
      <ul id="css3menu1" class="topmenu">
        <li class="link"><a href="/content/home.php">Home</a>
        </li>
        <li class="link"><a href="/content/products.php">Products</a>
          <ul>
            <li class="sublink"><a href="/content/products.php">Sub Products</a></li>
            <li class="sublink"><a href="/content/switchboards_distribution_panels.php">Switchboards</a></li>
          </ul>
        </li>
        <li class="link"><a href="/content/solutions.php">Solutions</a>
        </li>
        <li class="link"><a href="/content/case_studies.php">Case Studies</a>
        </li>
        <li class="link"><a href="/content/downloads.php">Downloads</a>
        </li>
        <li class="link"><a href="/content/careers.php">Careers</a>
        </li>
        <li class="link"><a href="/content/contact.php">Contact</a>
        </li>
      </ul>
    </div>

CSS:

ul#css3menu1,ul#css3menu1 ul{
    margin:0;
    list-style:none;
    padding:0;
    z-index:5000;
}
ul#css3menu1 ul{
    display:none;
    position:absolute;
    left:0;
    top:100%;
    padding:10px 10px 10px 10px;
    margin:0px 0 0 -13px;
  background-color:#fff;
}
ul#css3menu1 li:hover>ul{
    display:inline-block;
}
ul#css3menu1 li:hover li{
}
ul#css3menu1 li{
    display:table-cell;
    /*white-space:nowrap;*/
    font-size:0;
    color:#000;
}
ul#css3menu1 li:hover{
    z-index:1;
}
ul#css3menu1{
    font-size:0;
    z-index:999;
    position:relative;
    display:table;
  table-layout:fixed;
  width:100%;
    zoom:1;
    padding:0;
    *display:inline;
}
* html ul#css3menu1 li a{
    display:block;
}
ul#css3menu1>li{
    margin:0;
}
ul#css3menu1 .link a:active, ul#css3menu1 .link a:focus{
    outline-style:none;
}
ul#css3menu1 .link a{
    display:block;
    vertical-align:middle;
    text-align:center;
    text-decoration:none;
    font-size:14px;
    color:#777777;
    cursor:pointer;
    padding:23px 9px 24px;
    font-weight:bold;
    transition:all ease-in-out 0.4s;
    border-right:1px solid #eef1f1 !important;
  letter-spacing:0px;
}
ul#css3menu1 .link a:last-of-type{
    border-right:0;
}
ul#css3menu1 .link a.selected{
    background-color:#E4E8E8;
}
ul#css3menu1 .link a:hover{
    background-color:#E4E8E8;
    transition:all ease-in-out 0.4s;
}
ul#css3menu1 ul li{
    float:none;
    display:inherit;
    margin:10px 0 0;
}
ul#css3menu1 ul ul{
    margin-left:-10px;
}
ul#css3menu1 ul a{
    text-align:left;
    padding:4px;
    font:14px Tahoma;
    color:#FFF;
    text-decoration:none;
}
ul#css3menu1 li:hover>a,ul#css3menu1 li a.pressed{
    text-decoration:none;
}
ul#css3menu1 span{
    display:block;
    overflow:visible;
    background-position:right center;
    background-repeat:no-repeat;
    padding-right:0px;
}
ul#css3menu1 ul li:hover>a,ul#css3menu1 ul li a.pressed{
    color:#666;
    text-decoration:none;
}
ul#css3menu1 li.topfirst>a{
    border-width:0;
}
ul#css3menu1 li.toplast>a{
    }

Answer №1

Your code isn't the issue here, it's all about how CSS references an origin point for position: absolute.

The values like top/left are based on the nearest parent element with a specified position value. If there isn't a positioned element in the chain, then the origin is based on the <body> element.

|||| Element with position: absolute
||| - parent
|| - parent position: relative (this X/Y is used to position the element)
|

To resolve this, make sure to give your navigation containers positioning. Here's an example:

(note: In this example, I'm using flexbox to align the menu for simplicity. You can also use ul & li. Also, ensure to put the hover effect on the container so that the submenu remains open when you move the cursor over.)

nav {
    display:flex;
    justify-content: space-between;
}

.navItem {
    flex-basis: 1;
    background: #333333;
    color: #ffffff;
    margin: 0 1px;
    cursor:pointer;
    position: relative; // important for positioning
}

.navItem span {
    display:block;
    margin: 10px;
}

.subNav {
    position: absolute;
    top: 100%; // aligned at the bottom of the container
    left: 0;
    min-width: 100%;
    background: #555555;
    color: #ffffff;
    display:none;
}

.navItem:hover .subNav {
    display:block;
}
<nav>
    <div class="navItem">
        <span>Main Nav</span>
    </div>
    <div class="navItem">
        <span>Main Nav</span>
        <div class="subNav">
            <span class="subMenuItem">Sub 1</span>
            <span class="subMenuItem">Sub 2</span>
            <span class="subMenuItem">Sub 3</span>
            <span class="subMenuItem">Sub 4</span>
        </div>
    </div>
    <div class="navItem">
        <span>Main Nav</span>
    </div>
    <div class="navItem">
        <span>Main Nav</span>
        <div class="subNav">
                <span class="subMenuItem">Sub 1</span>
                <span class="subMenuItem">Sub 2</span>
                <span class="subMenuItem">Sub 3</span>
                <span class="subMenuItem">Sub 4</span>
            </div>
    </div>
    <div class="navItem">
        <span>Main Nav</span>
    </div>
</nav>

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

What is the best way to extract the text inside a div element based on the input value in a

I attempted to extract the innerText of a div along with the input values within it. For instance: <div> My name is Shubham, I work for <input type="text"/> for the last 5 years.</div> My objective is to retrieve all the text ...

The AJAX POST request is not receiving the JSON data as expected

After creating an HTML form with the following structure: <form id="loginForm" name="loginForm"> <div class="form-group"> <input type="username" class="form-control" id="username" name="username" placeholder="Your username..." > ...

Personalizing MaterialUI's autocomplete functionality

Trying to implement the material-UI Autocomplete component in my react app and looking for a way to display a div when hovering over an option from the list, similar to what is shown in this reference image. View example If anyone has any tips or suggest ...

Tips for allowing divs to be dragged and resized within table cells and rows

UPDATE I believe that utilizing Jquery is the most appropriate solution for resolving my issue with the demo. Your assistance in making it work would be greatly appreciated. Thank you. My goal is to develop a scheduler application. The essential functio ...

Discovering the art of incorporating various color palettes within a single stylesheet using HTML

I'm curious about incorporating multiple color schemes into a single stylesheet that can be activated by clicking, similar to the functionality found in platforms like WordPress and Magento. These platforms offer themes with various color options avai ...

Leveraging Angular and HTML to efficiently transfer the selected dropdown value to a TypeScript method upon the user's button click

I am experiencing difficulty accessing the .value and .id properties in {{selectItem}} in order to send them back to the typescript for an HTTP post at a later time. Although there are no specific errors, I have encountered exceptions and have tried search ...

Converting an NPM package into a gulp workflow

I'm currently generating html files from Nunjucks using my gulp file. Now, I need to convert these html files into text files. I came across a useful NPM package called html-to-text that can help with this task. However, I'm facing some challenge ...

When clicked, the onClick feature will reduce the number each time instead of initiating the timer

Currently, I am working on a meditation application using React. As a starting point, I implemented a 25-minute countdown feature. The challenge I am facing is that the timer starts counting down each time the button is clicked, rather than triggering it ...

There is a random section that keeps crashing on the website

I have encountered a bug on my one-page Bootstrap template website. One of the sections is causing issues after multiple page refreshes. Despite checking the console for errors, I am unable to identify the source of the problem. Here is the HTML code for ...

Adding multiple images to email HTML with Python made easy

Looking for a solution to embed 15 .jpg images from a single folder into the body of an email without them shrinking when stacked vertically. My initial approach was to create one long image with all photos but it reduced in size as more pictures were adde ...

Tips for detecting the existence of a different class within a div/ul through jquery or javascript?

This is how I have my unordered list, or "ul" element, styled. As you can observe, there are two classes defined for the ul <ul class="nav-second-level collapse"> </ul> There might be an additional class added to the same ul like so: <u ...

Activating list anchor upon click

I have a list that looks like this: <!-- List --> <ul class="nav nav-sm nav-tabs nav-vertical mb-4 steps-sampling"> <li class="nav-item"> <a class="nav-link active" id="link1" href="{{ ...

The collapse button in Bootstrap 3.3 and jQuery 1.1 appears to be visible but unresponsive when clicked

This code isn't functioning properly as it displays the three horizontal bar button, but nothing happens when clicked. bootstrap-3.3 jquery-1.1 Toggle navigation CopyHere <div class="collap ...

What is the best way to vertically align my image for perfect centering?

On one of my pages at , there is a section titled "FREE PICK UP AND RECYCLING OF LARGE QUANTITIES OF ELECTRONICS" with a picture of a truck. The image is perfectly centered vertically between the pictures on its left and right. Another page I have at fea ...

Accessing the Bootstrap tab form from an external source

I currently have Bootstrap tabs on a page and they are functioning correctly. However, I am looking to open these tabs from an external page. Is this feasible? <div role="tabpanel"> <ul class="nav nav-tabs" role="tablist"> ...

Instructions for implementing tooltips on a pie chart slice when hovering with the mouse pointer, using the canvas

var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); var cw = canvas.width; var ch = canvas.height; ctx.lineWidth = 2; ctx.font = '14px verdana'; var PI2 = Math.PI * 2; var myColor = ["Gr ...

Establish a vertical column that matches the height of its parent element

Is there a way to create a column that matches the height of its parent element? I have a navbar followed by a container with three columns. The challenge is to make the last right column the same height as its parent and also scrollable. In this scenario, ...

"Troubleshooting WordPress website: issues with CSS and images not

I recently developed a custom WordPress theme and everything was working perfectly on my local machine. However, when I moved the theme to the server, the CSS and images are not displaying properly. Strangely enough, when I checked the 'view source&ap ...

The default setting for my bootstrap modal is displayed, not hidden

Is there a way to make my bootstrap model hidden by default and only visible when I click on a link? I'm new to web development and would appreciate any help. tst.html <!DOCTYPE html> <html lang="english"> <head> < ...

The function "getElementsByClassName" in Javascript is malfunctioning

I have redesigned my website by implementing an interactive feature where users can click on a tree image to remove it and replace it with a number using JavaScript. Initially, I targeted the specific tree with the following code: document.getElementById( ...