Create a navigation bar using CSS that is designed from an unordered list without any specific

Is it possible to create a multilevel menu using only CSS for the menu structure mentioned below? Websites like cssmenumaker.com showcase examples of menus with 2-3 level submenus by adding classes like has-submenu.

Can we achieve this without adding any classes to the sub-menu and solely manage it through CSS?

For example:

.nav ul {}
.nav ul ul {}
.nav ul li{}....


<ul class="nav">
    <li><a href="#">Home</a>

    </li>
    <li><a href="#">About Us</a>

    </li>
    <li><a href="#">News</a>

    </li>
    <li><a href="#">Gallery</a>

        <ul>
            <li><a href="#">Image Gallery</a>

            </li>
            <li><a href="#">Video Gallery</a>

            </li>
        </ul>
    </li>
    <li><a href="#">Contact</a>

    </li>
</ul>

A demonstration on jsfiddle based on cssmenu.com can be found at: http://jsfiddle.net/zo61z9sw/

Visit for more information.

Alternatively, is it necessary to use jQuery or any other framework to handle multilevel menus?

Answer №1

This method is effective because the submenu ul is contained within the parent li.

  • Initially set the submenu ul to display: none;
  • When hovering over the parent li, change the child ul to display: block;. This is achieved using li:hover > ul with the greater than sign ensuring only direct child ul elements are affected.

In this scenario, the use of .has-sub is unnecessary as checking all li elements for child ul will suffice.

CSS:

/*CSS for menu without subclasses*/
#testmenu {
    border-radius: 5px;
    box-shadow: 0 0 2px 1px rgba(0, 0, 0, 0.15);
    background: linear-gradient(to bottom, #f2edea 0%, #c0bebf 100%);
    font-weight: 600;
    height: 52px;
    width: auto;
    margin-bottom: 100px;
}
 #testmenu ul, #testmenu li, #testmenu span, #testmenu a {
    border: 0;
    margin: 0;
    padding: 0;
    position: relative;
}
#testmenu:after, #testmenu ul:after {
    content:'';
    display: block;
    clear: both;
}

/* Remaining CSS style rules removed for brevity */

I have omitted some vendor prefixes in order to condense the code. There may be room for optimization by removing or consolidating certain styles.

JS Fiddle: http://jsfiddle.net/x9fmzc82/

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 process for cancelling a pending request using jQuery in JavaScript Selenium?

My website has a high volume of users clicking on one button. Waiting for long-pending ajax requests to get responses, sometimes over a minute, seems nonsensical. I understand how to wait for the response, but how can I cancel it? How do I cancel all pend ...

Troubleshooting issues with browser scrollbars in a Silverlight application (HTML)

I am currently working on a simple silverlight application and I need to incorporate web browser scroll bars into it (scroll bars should not be inside my silverlight app). This is the HTML code I have: <style type="text/css"> html, body { heigh ...

Tips on adding space between a material icon and its corresponding description

I have a design that needs some adjustments. The material icon is too close to the description, and I attempted to create padding by adding the following CSS: .location-secondary-info span:first-child { padding-right: 5px; } However, this change did no ...

CSS - Desiring a border with three layers

Here is the CSS code I am using: border: 2px solid #00ff60; outline: 1px solid #000; outline-offset: 0px; This is how it looks: https://i.sstatic.net/SuvU1.png Is there a way to modify it so that it includes an inner black border, similar to the one at ...

Guide on using jQuery to incrementally cycle through an array using a button

I am facing an issue with iterating through an array of objects using a button. The iteration is skipping 2 on each click instead of moving to the very next record. Can someone help me troubleshoot this problem? Or suggest a better approach to iterate thro ...

How can I populate a <select> tag with options dynamically using C# when the page loads?

I am using jQuery ajax to populate cascaded dropdown elements from a database. The issue I'm facing is that I can't seem to add the default "Select Program" option at the top of my first dropdown dynamically. Even though I tried using the prepend ...

The reference to the jQuery object is void

let quiz = { settings : { questionTypeDropDown : $j('#questionTypeDdown') }, data : { questionType : 'test' }, initialize: function() { quiz.bindUIActions(); }, bindUIActions : fun ...

Adding extensive HTML content using jQuery

Utilizing a REST API for my user interface and receiving data in JSON format has led me to append this data in HTML, resulting in script files that are too large and difficult to read. Seeking a more efficient method for importing JSON data into HTML, I w ...

This error is thrown when trying to access the property 'message' of an undefined value

I'm currently working on adding an AJAX contact form to my website. However, I've run into a problem where when I try to submit the form, I encounter the following error in Google Chrome: "Uncaught TypeError: Cannot read property 'message&a ...

Embedded Twitter posts are not showing up correctly when using vue

My backend is Django and frontend is Vue. In one of my models, I have a rich text editor. When saving the content, it converts tweet links to embedded HTML tweets. While displaying this content in a div with v-html binding in Vue, everything appears as exp ...

PHP implementation for a static header layout

I am interested in learning how to update content without refreshing the header. I have created a simple example below. Header.php <html> <head> </head> <body> <ul> <li><a href="index.php" ...

Troubleshooting Problem with React Slider: Inability to Display Images and Previous Button

I'm currently grappling with a React issue after following a tutorial on YouTube. I've been working in CodeSandbox and have encountered a problem where the images and previous button are not displaying as expected. Despite my efforts to debug, th ...

Using jQuery UI to style div elements with dynamic titles

Is it possible to style a standard div container using the same styling as jQuery dialogs? I am looking to create a div panel with text content but with the title bar styling similar to that of jQuery UI dialog boxes. Appreciate your assistance in advance ...

PHP and WordPress are experiencing issues with character encoding

On certain pages of my website, the character encoding appears to be clean, while on others it is not good. For instance, you might see something like this: Flu¨e, instead of this: Flüe. I am currently using Wordpress with my own theme. My PHP documen ...

The error "relative error: invalid property value" occurs when setting the width and height of a parent element

I'm having trouble getting my h2 text to appear above my images when hovered over. I keep receiving an error message in Chrome Developer Tools saying that "display: relative" is not a valid property value, but I can't figure out why. I've se ...

Is there a way for me to determine the total number of seconds since the chatbot was first opened?

Here is a snippet of my HTML code: <div class="chatbot chatbot--closed "> <div class="chatbot__header"> <p><strong>Got a question?</strong> <span class="u-text-highlight">Ask Harry</span></p> <s ...

Is there a way to display the product name in the input field when it is selected using the keyboard?

One of our team members has created an angular js script for autocomplete search functionality. Typing keywords in the search bar will return a list of candidates. However, when using arrow keys to navigate through the candidates, the product id is displ ...

Tips for retaining filled data after an incomplete form submission

<html> <head><script type="text/javascript"> function pa(){ var fname = document.getElementById('fname').value; var lname = document.getElementById('lname').value; var email = document. ...

Experiencing difficulties with incorporating cURL data into MySQL using Simple Dom Parser

Hey there! I'm currently trying to figure out the best way to save my scraped data to a MySQL database. It seems like nothing is getting inserted into the database, and I suspect it might be due to the format of the data I'm passing. Should I con ...

Create a layout using jquery-tokeninput

Currently, my project incorporates jQuery-tokenInput and everything is functioning properly. However, I now have the desire to customize the design of the results that are displayed. For example, I want to adjust the width or make any other design changes. ...