Is there a way to create a menu using only CSS?

Is there a way to achieve this using only CSS without any JavaScript?

For reference, here's the fiddle: http://jsfiddle.net/q646Ljg6/4/

This is the HTML:

<div id="navigation" class="navigation">
    <div id="dropmenu" class="dropmenu">
        <login>
            <ul>
                <li> <a href="#" ><span id="cart-button" class="flaticon flaticon-cart" aria-hidden="true"></span></a>

                </li>
                <li> <a href="javascript:void(0);">Dropdown C</a>

                    <ul>
                        <li><a href="#" >Option 1</a>
                        </li>
                        <li><a href="#" >Option 2</a>
                        </li>
                        <li><a href="#" >Option 3</a>
                        </li>
                    </ul>
                </li>
            </ul>
        </login>
        <ul>
            <li> <a href="/">Home</a>

            </li>
            <li> <a href="javascript:void(0);" >Dropdown A</a>

                <div class="sub-menu">
                    <div class="sub-menu-inner">
                        <div>
                            <ul>
                                <li><a href="#" >Dropdown A - 1</a>
                                </li>
                                <li><a href="#" >Dropdown A - 2</a>
                                </li>
                            </ul>
                        </div>
                    </div>
                    
                </div>
                
            </li>

            <li> <a href="javascript:void(0);" >Dropdown B</a>

                <div class="sub-menu">
                    <div class="sub-menu-inner">
                        <div>
                            <ul>
                                <li> <a href="#" >Dropdown B - 1</a>

                                    <ul>
                                        <li><a href="#" >Dropdown B - 1 - 1</a>
                                        </li>
                                    </ul>
                                </li>
                                <li><a href="#" >Dropdown B - 2</a>
                                    <ul>
                                        <li><a href="#" >Dropdown B - 2 - 1</a>
                                        </li>
                                        <li><a href="#" >Dropdown B - 2 - 2</a>
                                        </li>
                                    </ul>
                                </li>
                            </ul>
                        </div>
                    </div>
                    
                </div>>
                
            </li>
            <li style="float:none;overflow:hidden;">
                <div class="dark width-max"> <span>
                        <input id="quick-search" data-load="content" data-url="/quicksearch"  class="search" type="text" placeholder="Quick Search" maxlength="50" autocomplete="off">
                        <div id="search-results" class="search-results"></div>
                    </span>

                </div>
            </li>
        </ul>
    </div>
</div>
<div>
content content content content content content content content content content content content content content content content content content content content content content content content </div>

Javascript:

var currentTop = -1;
var currentSub = -1;
var currentProfile = -1;
//var topMenu = '';
//var subMenu = '';
var lastToggled = '';


function InitNav() {
 
    $('#dropmenu > ul > li > a', 'body').mouseenter(ToggleTop);
    $('#dropmenu', 'body').mouseleave(ToggleTop);

    $body.on('click', '#dropmenu > ul > li ul > li > a', ToggleSub);
    $('#dropmenu login a', 'body').mouseenter(ToggleProfileIn)....


function ToggleTop(e) {
 ... (CSS styles omitted)
}

InitNav();
      

CSS:

.navigation {
  /* CSS styles */
}
.dropmenu {
  /* CSS styles */
}

/* Other CSS rules */

...
... (Additional CSS styles omitted)
...

Answer №1

UPDATED:

<ul class="menu">
    <li id="levela">A Level 1
        <ul id="sub1" class="wide">
            <li>A Level 2</li>
            <li>A Level 2</li>
        </ul>
    </li>
    <li id="levelb">B Level 1
        <ul id="sub2" class="wide">
            <li>B Level 2</li>
            <li>B Level 2
                <ul class="wide_sub">
                    <li>B Level 3</li>
                    <li>B Level 3</li>
                </ul>
            </li>
        </ul>
    </li>
        <li style="position: relative;">C Level 1
        <ul class="normal">
            <li>C Level 2</li>
            <li>C Level 2
                <ul class="normal_sub">
                    <li>C Level 3</li>
                    <li>C Level 3</li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

css:

*, html, body {
    margin: 0;
    padding: 0;
    line-height: 40px;
}

ul.menu {
    list-style:none;
    position: relative;
    width: 100%;
    display: block;
    height: 40px;
    display: inline-block;
        background-color: yellow;
}

ul.menu > li {
    display: block;
    float: left;
    padding: 0 10px;
    cursor: pointer;
    height: 100%;
}

ul.wide {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    margin: 0 auto;
    background-color: grey;
    width: 100%;
    height: 50px;
    list-style: none;
}

ul.wide li {
    float: left;
    padding: 5px 10px;
        position: relative;
}

ul.wide li:hover ul.wide_sub {
    display: block;
}

ul.wide_sub {
    display: none;
    position: aboslute;
    top: 100%;
    left: 0;
    list-style: none;
        background-color: green;
}

ul.wide_sub li {
    float: none;
}

ul.normal {
    display: none;
    position: absolute;
    background-color: red;
    top: 100%;
    left: 0;
    list-style: none;
}

ul.normal li {
    float: none;
    position: relative;

...

#levela:hover #sub1 {
    display: block;
}

#levelb:hover #sub2 {
    display: block;
}

http://jsfiddle.net/michaelyuen/twdjobmL/1/

Hope this information proves useful for your needs.

Answer №2

Learn HTML

<ol>
    <li>
        <a href="#">example</a>
        <ul>
            <li>
                   <a href="#">hello!</a>
            </li>
        </ul>
    </li>
</ol>

CSS:

ol li ul{display:none;}
ol li:hover ul{display:block;}

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

Retrieving row and column values from an 81-element indexed array

I am working with an indexed JavaScript array that contains exactly 81 elements. These elements will form a 9x9 grid, similar to a Sudoku puzzle. Right now, I am trying to figure out the most efficient way to extract row and column values from this array. ...

Ways to conceal and reveal a different section at the same time (like an accordion)

Looking for guidance on implementing an accordion-style functionality, where clicking a navigation link hides one section and reveals another seamlessly with automatic scrolling to the start of the section (end of the navigation). Any tips or starting poin ...

Having trouble making changes to MUI TextFields once they've been filled in with data from an

My goal is to make MUI TextFields editable even after they have been filled with data from an API. The TextFields are getting populated successfully. I attempted using an onChange function, but it only allowed one additional character to be entered befor ...

Total of values that are continuously updated

I am working on a code snippet that dynamically updates the total cost of food items and clothing items separately as the user enters the value of each item. I am looking to display the combined Total Cost (sum of clothing + food inputs) which should be up ...

Fundamentals of Angular 2

It's not just an inconvenience, but something that truly frustrates me. Could someone please clarify the following: Why does Angular load these scripts in HTML directly from node_modules https://i.sstatic.net/D8UrG.png Why am I unable to simply imp ...

deactivating image mapping on mobile media screens

I'm looking to disable my image map on mobile screens using media queries. I've attempted to include some javascript in the head of my html file, but I encountered an error: <script src="http://code.jquery.com/jquery-1.11.3.min.js"></s ...

Issue with Vue.js devtool not appearing in browser

Currently, I am integrating moment.js into a Vue component but I am facing an issue where certain changes are not being reflected in vue devtools. Here is an example of my code: export default { data() { return { moment: moment(), ...

Creating a PHP script to generate a response for an AJAX request

I'm struggling with a logical issue in my script. The goal is to format some rows in a table without repeating the header, and then outputting all items as a variable passed to AJAX. However, I'm unsure of how to tackle this problem. function a ...

Ways to extract information from a JSON array based on its length and content

Here is an example of some data: { "_id": ObjectId("528ae48e31bac2f78431d0ca"), "altitude": "110", "description": [ { "id": "2", "des": "test" } ], "id": "1", "latitude": "24.9528802429251", ...

Is it necessary to utilize stopPropagation() when the event doesn't need to bubble up, but it also doesn't cause harm to do so?

As I write jQuery event handlers, I frequently come across instances where there is no absolute need to bubble up the event, but it also doesn't have any negative effect. Is it common for developers to use stopPropagation() in these cases? ...

The CSS styles are not being applied to the header and footer sections in the PDF file created with

When I try to add a header and footer template to the PDF generated by puppeteer, I am facing an issue where the CSS I added is not being fully applied. await page.pdf({ path: filePath, format : 'A4', printBackground : true, margin : { t ...

JSON data handling in Select2 with jQuery

My Select2 plugin is not loading any data. I created a test.php file with JSON data (which will be provided externally once everything works, test.php is just for internal testing). Output of test.php [{"suggestions": ["1200 Brussel","1200 Bruxelles","12 ...

Is there a way to ensure that a "catch all other" route in the Vue Router will also capture the URL if a portion of it matches a predefined route?

After following the suggestion to implement a catch all route from this article, I realized that it does not capture URLs that partially match a defined route. routes: [ { path: "/album/:album", name: "album", component: Album, } ...

Bypass license and credit comments when compressing JavaScript with YUIcompressor

When using YUICompressor to minify JavaScript, is there a method to retain license or credit comments? Are there specific commenting characters to use, or a flag in YUICompressor for this purpose? Thank you, Grace ...

Aligning items in the center using the flex property is limited to just one element

#draggable-container { margin: 0 auto; display: flex; flex-wrap: nowrap; align-items: center; } .draggable-game-content { width: 100%; height: 40rem; } .draggable-game { position: relative; border: 1px solid black; bor ...

Looking at a web page within another web page by utilizing XMLHttpRequest

Recently, I created a web app featuring a preview window that displays the content of an HTML page. To keep up with the constant updates to the HTML page, I have set the preview window to refresh every 0.5 seconds. Although the functionality works correct ...

Limit the iframe display from an external site to specific domains with these easy steps

My service involves preparing and displaying a client's content within an iframe. However, some clients have raised concerns about the possibility of other websites copying the iframe tag and using it on their own sites. Is there a way to restrict th ...

Encountering an issue in my Next.js application with the app router where I am unable to read properties of undefined, specifically in relation to '

As I develop a basic Rest API in Next.js, my goal is to display "Hello world" in the console for a post api. export const POST = (req: Request) => { console.log('hello world'); }; The error message that appears in my terminal is as follows: ...

Similar to using `display:flex` and `justify-content: center`, you can achieve centered

I have a group of n children within a div. I want to arrange them in a single row and center them horizontally. If I were to use flexbox, I could achieve this by: display: flex; justify-content: center; Is there a way to accomplish the same layout using ...

Login Form with AJAX and PHP - Accessing MySQL Database using jQuery Mobile

Having trouble with AJAX/PHP form submission. Below is my AJAX code: <script type="text/javascript> $('#loginForm').submit(function() { checkLogin(); }); function checkLogin() { $.a ...