Attempting to design a CSS3 drop-down menu

I've been struggling to get this right, but I can't seem to make the sub list appear! I attempted using

nav > ul > li:hover > ul{}
, but it ended up causing issues with the code functionality. It seems like a simple problem, but I just can't figure it out.

nav > ul {
  list-style: none;
}
nav > ul > li {
  padding: 20px;
  float: left;
}
nav > ul > li {
  background-color: #fff;
}
nav > ul > ul {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  padding: 0;
}
nav > ul > ul > li {
  float: none;
  width: 200px;
}
nav > ul > li:hover {
  color: #4169E1;
  display: block;
  visibility: visible;
}
body {
  background: black;
}
<nav>
  <ul>
    <li>Home</li>
    <li>About Us</li>
    <li>Secure</li>
    <ul>
      <li>How secure are we?</li>
      <li>We are not secure enough!!</li>
    </ul>
    <li>Mad</li>
  </ul>
</nav>

Answer №1

  • Streamline your selectors (nav ul ul) for better performance

  • Set the parent li's position: relative so that the dropdowns with position: absolute are accurately positioned in relation to them. Adjust the top value accordingly

  • In this scenario, the use of visibility: visible is redundant. Instead, utilize display: none and display: block to manage visibility

  • Ensure proper nesting of lists as demonstrated below:

<ul>
    <li>Top Menu Item
        <ul>
            <li>Sub-menu Item</li>
        </ul>
    </li>
</ul>

For further information, visit: Nested lists on w3.org

CSS / HTML / Demo

* {
  margin: 0;
  padding: 0;
}
nav ul {
  list-style: none;
  padding: 0;
}
nav ul li {
  padding: 20px;
  float: left;
  background-color: #fff;
  position: relative;
}
nav ul ul {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  padding: 0;
}
nav ul ul li {
  width: 200px;
  background: #FFF;
  padding: 10px;
}
nav ul li:hover ul {
  color: #4169E1;
  display: block;
}
body {
  background: black;
}
<nav>
  <ul>
    <li>Home</li>
    <li>About Us
      <ul>
        <li>This is us!</li>
        <li>This is us!</li>
        <li>This is us!</li>
        <li>This is us!</li>
      </ul>
    </li>
    <li>Secure
      <ul>
        <li>How secure are we?</li>
        <li>We are not secure enough!!</li>
      </ul>
    </li>
    <li>Mad</li>
  </ul>
</nav>

Answer №2

Identifying Errors

   <ul>
        <li>Home</li>
        <li>About Us</li>
        <li>Secure
           <ul> **--> This should be inside li** 
                <li>How secure are we?</li>
                <li>We are not secure enough!!</li>
            </ul>
        </li>

        <li>Mad</li>
    </ul>

Additionally, in your css

Include the following snippet:

nav > ul > li:hover > ul{
  display: block;
  opacity: 1;
  visibility: visible;
}

View the fiddle http://jsfiddle.net/ruchan/4fk6y2wu/

Answer №3

Enhance your website with the power of CSS3!

Check out the Demo here

View in Fullscreen mode

<nav>
    <ul id="menu">
        <li class="category top_level"><a class="selected" href="#">Home</a></li>
        <li class="category top_level"><a href="#">About</a></li>
        <li class="category top_level"><a href="#">Secure</a>
                    <ul class="dropdown">
                <li class="item"><a href="#">How secure are we?</a></li>
                <li class="item"><a href="#">We are not secure enough!!</a></li>
            </ul>
        </li>
        <li class="category top_level last"><a href="#">Mad</a>
        </li>
    </ul>
</nav>

CSS Styles

body {
    font-family:'Montserrat', sans-serif;
    background:#000;
}
ul {
    list-style-type: none;
}
#menu a {
    text-decoration: none;
    white-space: nowrap;
    color: #222;
    background-color: #fff;
}
#menu li.top_level {
    vertical-align: top;
    zoom: 1;
    display: block;
    float: left;
    width: 16.5%;
    margin-right: 0.2%;
    position: relative;
    text-align:center;
}
#menu li.last {
    margin-right: 0px;
}
#menu .dropdown {
    float: none;
    z-index: 100;
    position: absolute;
    width: 100%;
    height: 0;
    overflow: hidden;
}
#menu .category:hover .dropdown, #menu .category:focus .dropdown {
    height:auto;
}
#menu .item a, #menu .category a, #menu .category a:visited, #menu .item a:visited {
    line-height:2em;
    display:block;
    padding:.6em;
    border-top: #ffffff 2px solid;
}
#menu .item a:hover, #menu .category a:hover, #menu .item a:focus, #menu .category a:focus {
    background:#007dac;
    -webkit-transition: background-color 940ms;
    -moz-transition: background-color 940ms;
}
#menu a.selected {
    color: #ffffff;
    background-color: #007dac;
}

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

Failure of event watcher on dynamically updated content

Any help would be greatly appreciated! :) I am currently using JavaScript to dynamically add fields to a document, but I have noticed that the event listener only works on predefined fields. For instance, in the code snippet below, the 'lozfield&apo ...

The Node.contains() function in JavaScript does not verify the presence of inner child elements

I have developed a custom DatePicker component for my React app and I am facing an issue with hiding it on outside click, similar to how I handled other components like Select Dropdown. To address this problem, I created a custom hook: export default (ref, ...

JavaScript's jQuery selector retrieves a child element from one location and adds it to a different

Why does the Jquery children selector select the img element, extract it, remove it, and then append it to another div element? Shouldn't it create a copy of the element being appended? var $anchors = $("#imageGallery a"); var $overlay = $('&l ...

Place the span element above the div container

I recently created some social media buttons with share counters, but I'm facing an issue where the counter data appears inside the buttons rather than on top of them. Has anyone encountered this problem before? Is there a way to display the numbe ...

Alignment issue detected with the text for radio buttons

Attempting to include radio buttons on my survey form has resulted in the button and text being misaligned. Here is a screenshot showcasing how it currently looks: view image here I have experimented with using display: inline; but unfortunately, no chang ...

Tips for changing the contents of a div when a particular link is clicked

Can someone please explain how to update the contents of a div using "AJAX" when a specific link is clicked? Here's what I'm trying to achieve: I would like to show the company description if the user clicks on "Description", or display the revi ...

When initiating payment through Razorpay by clicking "Pay Now" with a card or wallet, a new tab opens showing a blank page with a blocked message,

When attempting to integrate Django with Razorpay, I encountered an issue where clicking on the "Pay Now" button for payment options like card and wallet opened a new tab but resulted in a blank page. This prevented access to the success page. It's im ...

Conceal column exclusively on smaller displays using Bootstrap 4

I want to design a grid that covers the whole screen with 3 columns (left, middle, right) Left: This column should only be displayed on large views and occupy 16.6% of the screen (2/12) Middle: This column should always be visible. It should take up 75% ...

Checkbox input with alphabetical ordering

I am currently facing an issue with a form that has checkboxes, but the text is not in alphabetical order. While there are plenty of examples for lists, finding examples for checkboxes has been challenging. http://jsfiddle.net/fG9qm/ <form> < ...

Angular Email Pattern Validation: Ensuring Email Formatting is Correct

<label for="userEmail">User Email:</label><br /> <input type="email" class="userMobile" id="userEmail" name="userEmail" [(ngModel)]="selectedLocker.lockeruser ...

How can I reset a CSS position sticky element using JavaScript?

I have created a page where each section fills the entire screen and is styled using CSS position: sticky; to create a cool layered effect. Check it out here: https://codesandbox.io/s/ecstatic-khayyam-cgql1?fontsize=14&hidenavigation=1&theme=dark ...

Discovering the following div

<button class="volumen-btn" onclick="niceFunction()> <i class="fa fa-volume-up"></i> </button> <div class="volume-control-container" id="container1" style="display:none;"> <input type="range" name="blabla" class="volume-ran ...

What are the advantages and disadvantages of using the picture element in web design?

What advantages and disadvantages come with using the HTML5 picture element? Specifically, does the browser download all images or only the image that matches the media query? ...

Interacting with the DOM using jQuery's .post() method

I am attempting to utilize Jquery's onclick function and post to msg.php. Could there be an issue with my code? I would appreciate any insights on what the problem might be. Thank you. HTML <script src="https://ajax.googleapis.com/ajax/libs/jquer ...

What is the best way to merge two values with the same property in an object using a comma as a separator

Is it possible to combine the values of properties with a separator in my scenario? Here is an example of my JSON structure: { "name": "aa", "name": "bb", "name1": "cc", "name2": "dd" } I am looking to display the values of the key 'name&apo ...

Tips for saving the visibility status of a <div> in your browser bookmarks?

Currently, I am in the process of developing a single webpage (index.html). The navigation bar at the top includes links to hash references (DIV IDs). If JavaScript is enabled, clicking on these links triggers a JavaScript function with the onclick attribu ...

Elementor custom CSS for Wordpress header

I've been struggling to make my header transparent and then change to a colored background when scrolling down. I tried following the instructions in a video, but I couldn't find the OFFSET EFFECT setting. WEBSITE: Video: https://www.youtube.co ...

Bootstrap 4 did not achieve the desired responsiveness for the image and h1 element

I am having trouble with my two Div elements, one for an image and one for a heading. I have set different font sizes based on different media queries in my CSS file. While it works when shrinking the width, the heading does not break into two lines. Addit ...

Disappearance of logo div causes video to vanish

Hey there fellow coders! I'm facing a little issue. I'm attempting to replicate my webpage on another page, but every time I try to remove the logo, the video disappears as well. It's quite puzzling. I have the code in place, but it seems l ...

Looking for assistance in translating HTML code into an XpathQueryString?

While following a tutorial by raywenderlich on how to parse HTML in Xcode, I encountered an issue. I was able to parse his website successfully, but I'm struggling with the concept of Xpathquery. All I need is to extract the data from a table. Below i ...