Creating a CSS dropdown menu with absolute positioning and setting the left position to auto

Just laying out my usual HTML menu structure:

<div id="page">

    <h1>Home</h1>
    <ul id="nav">
      <li><a href="http://example.com/">Home</a>
        <ul>
            <li><a href="http://example.com/">Nested Item</a></li>
            <li><a href="http://example.com/">Nested Item</a></li>
        </ul>
      </li>
      <li><a href="http://example.com/">About Us</a>
        <ul>
            <li><a href="http://example.com/">Nested Item</a></li>
            <li><a href="http://example.com/">Nested Item</a></li>
        </ul>
      </li>
      <li><a href="http://example.com/">Our Products</a>
        <ul>
            <li><a href="http://example.com/">Nested Item</a></li>
            <li><a href="http://example.com/">Nested Item</a></li>
        </ul>
      </li>
      <li><a href="http://example.com/">Contact Us</a>
        <ul>
            <li><a href="http://example.com/">Nested Item</a></li>
            <li><a href="http://example.com/">Nested Item</a></li>
        </ul>
      </li>
    </ul>

    <h2>Stuff</h2>
    <p>
    Random stuff.</p>
    <p>
    Some random text.</p>

</div>

Take a peek at the menu css:

#nav, #nav ul{
    padding: 0;
    margin: 0;
    list-style: none;
}

#nav a{
    display: block;
    border:1px solid cyan;
}

#nav li {
    border:1px solid;
    float: left;
}

#nav li ul {
/* Hides sub-items while keeping them available to screen readers - important note! Never use display: none; as it removes sub-items entirely. */
    position:absolute;
    width: 10em;
    left: -999em;
}

 #nav li:hover ul {
    left: auto;
}

I'm curious about that last CSS rule:

 #nav li:hover ul {
        left: auto;
    }

How exactly does "left:auto" normalize the negative position of "left:-999em" and create a seamless drop down effect?

Here's an example.

Answer №1

The initial use of "left:-999em" effectively hides the 'ul' element by pushing it out of view on the page. By changing it to "left:auto," the 'ul' is simply brought back to its default position, making it visible once again.

To learn more about this concept, visit: http://www.w3schools.com/cssref/pr_pos_left.asp

This technique works because setting a "position:absolute" allows the 'ul' to overlay other content, and as you hover over the 'li' elements, the "left" value is adjusted back to its original state.

For further understanding, check out http://www.w3schools.com/cssref/pr_class_position.asp

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

Adjusting Dropdown Direction Based on Section Location – A Step-by-Step Guide

Question: Is there a way to adjust the bootstrap dropdown so that it can appear either above or below depending on where the section is positioned? Check out my code here Here is the CSS I am using: #cover-inspiration{ width: 100%; height: 100vh ...

Looking for a Horizontal Layout?

@foreach (var item in APModel) { <div class="row row-cols-1 row-cols-md-2 g-4"> <div class="col"> <div class="card" style="width: 18rem;"> <img src="h ...

Unable to incorporate additional functions while using directives

http://example.com Is there a way to incorporate an addChild(tree) function into this code in order to expand the data array? I have made changes to the template as shown below: '<p>{{ family.name }}{{test }}</p>'+ '<ul>& ...

Utilizing jQuery/Ajax to send an ID parameter to a PHP script

This code is where the user interacts. <html> <head> <title></title> <script src="jquery-1.9.1.js"></script> <script src="jquery.form.js"></script> </head> <body> <?php include 'con ...

Troubleshooting Flexbox in Internet Explorer 11: Issue with 100% width not functioning properly within nested flex containers with a 'column'

Is there a way to ensure that an image within nested flex containers has a width of 100% in IE11, even when the container has flex-direction: column? I've tried setting the width to 100% with a max-width of calc(100% - 0.1px), but unfortunately it doe ...

Why does my element appear to be a different color than expected?

I've developed a sleek wind map (check out for reference). Using a weighted interpolation every 10ms, I generate uVector and vVector data to calculate wind speed. Based on this speed, each point is assigned a specific color as outlined below. if (we ...

Selectize Fails to Populate Options Using Ajax

ExpressJS is the platform I am using to develop a management dashboard for a community project. Right now, my main concern is integrating a modal that allows users to add new games to a database. Although I am able to fetch data remotely, I am facing diffi ...

Issues with a responsive website not displaying correctly in the Firefox browser

I have created a carousel that is functioning correctly. However, I am encountering an issue with making it fully responsive. It appears to work properly in webkit browsers, but there are some issues in Firefox. Despite the images resizing properly, the ...

Issue with the footer not remaining at the bottom of the page

I'm in the process of updating my website and have come across an issue with the footer placement on one specific page. Typically, the footer stays at the bottom of all pages except for this particular page where it appears floated to the left instead ...

Tips for rearranging the icon placement of the parent menu item on Bootstrap Navigation Bar for mobile display

I need assistance with adjusting the position of the bootstrap navigation menu bar parent menu icon, in mobile view. Currently, the menu header displays a right-arrow-down icon at the end of the header name as shown below. https://i.sstatic.net/kv4T2.png ...

How can I include an HTML tag within a select input value in a React.js project?

I am facing an issue while trying to map values from the input field of a select tag. It seems to be returning [Object object] for some reason. When I do not include another tag in the return statement of the map function, the output works fine. However, I ...

Using HTML and CSS, change the background color to red based on the value of each cell in a forEach loop

Need help with my HTML/CSS code. I have a table where I need to change the background color of any row that has a cell value of "Not Approved" while using a foreach loop in Google Apps Script to send an email with two tables. What is t ...

Should we make it a routine to include shared sass variables in each vue component for better practice?

Within my Vue application, there are numerous components that rely on shared variables like colors. I'm considering having each component import a global "variables.scss" file. Would this approach have any adverse effects? ...

Tips for setting up an automated system to check feeds

While I have experience parsing and setting up databases in PHP, I am now faced with the challenge of constantly monitoring an XML feed for a specific string within one of the tags. Each time this string is detected, I need to add the entire XML item (incl ...

Having trouble importing JS into HTML file with NodeJS

I have created a web server using NodeJS that serves the file index.html. However, when I attempt to add a JavaScript file to my HTML document, the browser displays an error message stating The resource "http://localhost:3000/includes/main.js" was blocked ...

Creating UL tabs using just HTML and CSSHere's a step-by-step guide

Looking for help to accomplish this task. I already have the styling, but I want something to happen when I click on the tabs. Specifically, I want the div with the tab class names to show and hide upon clicking the tabs. Currently, nothing happens when I ...

Determining with jQuery if the right element has been successfully "dropped"

Imagine having 10 different draggable boxes and 2 droppable areas. You need to correctly match 5 boxes with droppable area 1 and the other 5 with droppable area 2. If a box intended for droppable area 1 is mistakenly dropped into area 2, it should not wor ...

How do I repeatedly invoke a function in JQuery that accepts two different arguments each time?

I have a collection of folders, each containing various images. The number of pictures in each folder ranges from 8 to 200. Folders are numbered from 1 to 20 and the images within them are also labeled numerically. My goal is to aggregate all these images ...

Troubleshooting problem with displaying circular tag image below an image using Jquery

I'm looking to enhance my HTML DOM by adding circular tag images with dimensions of 25x25 pixels to all image tags. While my current method is functional, I've noticed that the position sometimes shifts and the tag often ends up displayed below t ...

Techniques for animating background image using jQuery

Hello there! I'm currently experimenting with animating a background image using jQuery. Despite following a tutorial, I haven't been successful in getting my test page to work as intended. The blue Facebook icon displays, but it doesn't ani ...