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

Can you explain the distinction between ' &:hover' and ' & :hover' pseudoclass selectors?

While styling a Material-UI Button, I encountered an unexpected behavior when adding hover effects. When using & :hover, only the inner span of the button was affected. However, when using &:hover, the desired style was achieved. What is the diff ...

Show divs in identical position when clicking

There are 4 section divs that need to be displayed in the center of the page when clicked, but the last one appears further down. This is likely due to the flex box nature. What is the best way to ensure all sections appear at the exact same location? Ano ...

Using CSS to show only the central portion of an image

How can I display only the center part of an image with dimensions height=300px and width=520px using CSS or jQuery? I tried searching on Google, but didn't find a solution. Is it possible to show only a 200x130 section from the center of an image us ...

"I recently started using Protractor and as per company policy, I am unable to utilize XPath. I am facing difficulties with the code below and would greatly appreciate any insights or assistance provided

I am looking for guidance on how to write automation test scripts for a small AngularJs application using the cucumber-protractor-typescript framework. As someone new to Angular applications, I am particularly concerned about creating reliable locators. B ...

Preventing click events with pointer-events property in CSS while still allowing scrolling

Is there a way to use pointer-events: none to disable click events on a specific container while still allowing scroll functionality and an overlay? You can view my fiddle here: https://jsfiddle.net/1eu6d3sq/1/ Currently, when I apply pointer-events: non ...

Is your WordPress one-page scroll JQuery script failing to function properly?

Currently attempting to develop a single page scroll feature. Within WordPress, my navigation contains anchor tags structured as follows: <a href="#contact">Contact</a> <a href="#about">About</a> The corresponding divs for the li ...

Having trouble with your website's container not wrapping properly?

I've run into an issue where my container is not wrapping a border around the other elements, only around the header. I checked with validators for both CSS and HTML but there are no errors being shown. Does anyone have an idea what might be causing t ...

How to Extract Information from a Table Enclosed in a Div Using HTML Parsing?

I'm new to HTML parsing and scraping, looking for some guidance. I want to specify the URL of a page (http://www.epgpweb.com/guild/us/Caelestrasz/Crimson/) to grab data from. Specifically, I'm interested in extracting the table with class=listing ...

Despite using twitter bootstrap, the elements on my webpage are still overlapping one another

While implementing Twitter Bootstrap on my website, I encountered a problem where elements start overlapping if the window size is reduced. This issue does not look appealing, and I expected Twitter Bootstrap to ensure that my website is fully responsive a ...

Converting a Powershell array into an HTML format

I have three arrays that I need to display on an HTML page: foreach($item in $array1){ // write array data to HTML } foreach($element in $array2){ // write array data to HTML } foreach($value in $array3){ // write array data to HTML } What is the ...

The CSS and Javascript files are failing to load

My web page is not displaying my CSS and JavaScript properly when I access it through a folder in the browser. Both files are located within multiple subfolders. I have attempted to rename the files to troubleshoot the issue. <head> <link rel=" ...

The text is not meticulously arranged within the designated span

My text-align (center) property doesn't seem to be working correctly with the following code snippet: <span class="info">&nbsp;i&nbsp;<span id="uitleg_itje">Bla bla. </span></span> The CSS for the info class is as fo ...

Is there a way to adjust the margin for a child DIV element using CSS?

Currently, I am working on my website located at www.buildinghunter.com. On the homepage, there are four div elements positioned in the bottom left corner of the page. I am attempting to align two specific divs (id=text-9 and id=text-10) with the other tw ...

Troubleshooting: My jQuery script for fading in content upon document ready is not

I'm currently working on a website where I want everything to fade in when the user enters the site. Take a look at my code: var celyLogin = $(".container"); $(document).ready(function () { /*! Fades in page on load */ $("container") ...

Transforming a multi-row table into a list of dictionaries using Beautifulsoup

I'm facing an issue with converting an HTML table into a list of dictionaries. The table has limited attributes, and I need to ensure accurate parsing. Here's the relevant section: <div class="table-container"> <table> ...

Maintain the basic structure while ensuring divs are properly aligned

Behold, my div structure! ONE // at the top level TWO THREE // two divs side by side in the middle level FOUR // residing at the bottom <div id="ONE"> <div></div> <div><img src="logo.png"></div> </div> < ...

It vanishes as soon as you move your cursor away during the animation

I created a button component with text animation, but I'm encountering an issue. When I hover over the button, the animation works smoothly. However, if I quickly move my cursor away or unhover in the middle of the animation, the text disappears unex ...

Address the underlying issues with the background

I am working on an app and I want to include a fixed background image. I am using HTML5, CSS3, and JavaScript for this project. I have applied the CSS code to set the background image as fixed: body{ background:#1F1F1F url(../assets/bg.png) fixed;} Ev ...

What could be causing my fixed header to disappear in Safari 5?

I am experiencing a strange issue with my fixed header on Safari 5 for iPad. The header disappears once it scrolls down to a certain point, even though it works fine in IE, Firefox, Chrome, and the latest version of Safari. Has anyone else encountered this ...

How can one use Selenium to verify the existence of an element on a webpage?

Currently, I am developing a program in Selenium with Python and facing an issue. During the test execution, there is a possibility that a button may or may not appear on the webpage based on an unknown parameter. The relevant HTML tag for this button is ...