A drop-down menu that can be toggled using only HTML and CSS

Recently, I came across two helpful tip pages that explain how to use :hover to display a drop-down menu and the checkbox to toggle it open or closed.

After implementing it successfully, I encountered an issue with CSS styling. While styling the :hover state was easy, I struggled with styling the opened drop-down menu caused by toggling as there is no direct parent CSS selector for this. Ideally, I prefer not to rely on JavaScript for this solution.

I have included the code I managed to get working on JSfiddle below. Please note that the position of the checkbox is currently not a concern. My code

The particular section at the bottom of the CSS file is where the problem lies.

nav ul li #chktut:checked:parent {
        background: #4b545f;
        background: linear-gradient(top, #4f5964 0%, #5f6975 40%);
        background: -moz-linear-gradient(top, #4f5964 0%, #5f6975 40%);
        background: -webkit-linear-gradient(top, #4f5964 0%,#5f6975 40%);
    }

Answer №1

If you want to make the "tutorials" section clickable, you can achieve this by connecting a label to the chktut checkbox:

<li><input type="checkbox" id="chktut" /><label for="chktut">Tutorials</label>

To ensure the checkbox is hidden from view, simply set its display property to none:

#chktut{
   display: none;
}

Answer №2

Creating this was a breeze. It's designed to be easily stylable without the need for javascript, and it adjusts beautifully on different screen sizes :D

<!doctype html>
<html>
    <head>
        <title>Sample Page</title>
        <style>
            .dropdown_link {
                background-color: #000;
                color: #eee;
                display: block;
                padding: 5px;
            }

            .fullWidth {
                width: 100%;
            }

            .hover:hover {
                background-color: #408FFF;
            }

            .menu_container {
                border: 0;
                margin: 0 0 10px 0;
                padding: 0;
            }

            .menu_dropdown_container:hover ul {
                display: table;
            }

            .menu_dropdown_list {
                display: none;
                padding: 0;
                position: absolute;
            }

            .menu_item {
                display: table-cell;
                background-color: #eee;
                text-align: center;
            }

            .menu_link {
                background-color: #6FA8F7;
                color: #eee;
                display: block;
                padding: 10px 0;
                width: 100%;
            }

            .menu_list {
                display: table;
                margin: 0;
                padding: 0;
            }

            .no_decoration {
                text-decoration: none;
            }

            .no_bullets {
                list-style: none;
            }

            @Media screen and (max-width:480px) {
                .menu_item, .dropdown_item, .menu_dropdown_list {
                    display: block;
                    width: 100%;
                }

                .menu_dropdown_list {
                    position: static;
                }
            }
        </style>
    </head>
    <body>
        <div class="menuContainer fullWidth">
            <ul class="fullWidth menu_list no_bullets">
                <li class="menu_item">Your logo goes here</li>
                <li class="menu_item"><a class="menu_link hover no_decoration" href="#">Home</a></li>
                <li class="menu_item menu_dropdown_container">
                    <a href="#" class="menu_link hover no_decoration">Products</a>
                    <ul class="menu_dropdown_list no_bullets">
                        <li class="dropdown_item"><a class="dropdown_link no_decoration" href="#">For your home</a></li>
                        <li class="dropdown_item"><a class="dropdown_link no_decoration" href="#">For your enterprise</a></li>
                        <li class="dropdown_item"><a class="dropdown_link no_decoration" href="#">For babies</a></li>
                        <li class="dropdown_item"><a class="dropdown_link no_decoration" href="#">For Flying cats trying to conquer the world</a></li>
                    </ul>
                </li>
                <li class="menu_item"><a href="#" class="menu_link hover no_decoration">Company</a></li>
                <li class="menu_item"><a href="#" class="menu_link hover no_decoration">Blog</a></li>
            </ul>
        </div>
    </body>
</html>

Answer №3

After experimenting on JS fiddle, I have successfully implemented a functional solution. The styling and mechanism are complete, and I hope that it can be beneficial to others. This feature is particularly advantageous for mobile users, allowing them to navigate through all pages efficiently without the need to load JavaScript or navigate multiple pages.

The code is available entirely on JS Fiddle, and there are no restrictions to its usage. Feel free to utilize it as needed.

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

developing educational material that can be unlocked

I am currently working on developing a web application for a small company that specializes in providing English courses for individuals looking to improve their language skills. I am facing a challenge in creating a form that, when completed correctly, gr ...

Technique for structuring and managing CSS resources in Angular.js

I am exploring ways to load CSS files in a modular manner within my Angular application. My goal is to have each module with its own dedicated CSS file for easy management - allowing me to add, remove, or move modules effortlessly. After some research, I ...

Troubleshooting: Style Conflict Between Select2 and JqueryValidator

I've integrated this particular library along with the jquery.validate plugin, but I'm facing a style problem: ideally, the error message should appear underneath the select list. Here is my JavaScript code : errorElement: 'p', ...

Issues have been identified with the performance of Ionic Material List when used in conjunction with ng

In my project involving the Ionic floating menu button, everything is working fine. However, I have encountered an issue where when I click on the + button, I do not want to be able to click on the click me button while the menu is open (Req1.png)https://i ...

What is the best way to give this CSS button a "highlight" effect when it is clicked using either CSS3 or JavaScript?

In the HTML page, I have two buttons implemented with the following code: <!-- Button for WIFI projects: --> <a title="WIFI" href="javascript: void(0)" id="showWifi_${item.index}" class="showWifi"> <div class="news_box news_box_01 hvr-u ...

Having issues with nth-child? It seems like nth-of-type isn't working either

I am trying to change the color of all even titles from h2 to orange using nth-child. However, I seem to have made a mistake somewhere and can't figure out what it is... *{ font-size: 1em; } h2{ font-size: 1.5em } ...

Is there a way to dynamically update the target of a form using JavaScript?

My goal is to change the target of a form in order to open a new window. However, all redirections of this form use the same window except for one specific button. This button calls a custom JavaScript function that needs to change the default "_self" targ ...

What is the process for setting up a bootstrap grid with a single column at the top and two columns

Can anyone help me figure out how to rearrange a bootstrap grid from three columns on desktop to one column above and two below on mobile? I've attempted multiple methods, but haven't had any luck... https://i.stack.imgur.com/oY2VU.png ...

JavaScript - A simple way to retrieve the dimensions of an image consistently

I'm currently working on a piece of Jquery code that is designed to display an image fitting within the screen's dimensions. These images are generated dynamically as needed. However, I am facing an issue where the height and width of the image a ...

Just finished my first webpage - can't seem to get the footer to stay put!

After spending 3-4 months learning HTML and CSS, I am now working on my first web page. However, I'm facing an issue where the footer is stuck to the slideshow and I can't seem to figure out how to position it at the bottom of the page. Any tips ...

Sending various values to a JavaScript function

I am working with a function that looks like this: //Function Call with Single Parameter responses(baseURL); //Function Definition function responses(baseURL) { $.ajax({ url: baseURL, type: "get", cache: false, header ...

I'm having trouble getting the height of my div element to render in percentage (%) in the CSS. Can anyone assist me with this issue?

When I try to set the "height" CSS property in percentage (%) for a div element, the HTML doesn't render anything. However, if I set the "height" CSS property using length values such as px or cm, it works perfectly fine. Can anyone provide assistance ...

Set a placeholder for the editable div if it does not contain any text

Currently, I am attempting to create an editable div with a placeholder. My goal is for the placeholder to be automatically displayed when there is no text in the div. To achieve this, I am using the contentEditable='true'; attribute on the div ...

The livestream table appears to be experiencing delays in updating as expected

I am currently in the process of developing a webpage for livestreamers on my website. However, I have encountered some challenges along the way. I have created a table and made several adjustments to it, yielding positive results. Despite this progress, t ...

iOS7 webkit introduces a new feature called span word break

After upgrading my phone to iOS7 today, I came across a strange issue. (blog.niwyclin.org) This is just a test post on my website Everything looks good on the desktop browser. When I used Responsivator to check, it appeared perfect, like this (i.minus.c ...

Support for SVG Sprites is limited in Internet Explorer and Safari, causing compatibility issues with SVG functionality

Utilizing SVG Sprites to incorporate svg icons on my webpage has worked smoothly in Chrome and Firefox, but I am encountering an issue where none of the svg icons are displaying in IE and Safari. <div class="toggle-icons"> <span class="toggle-row ...

Bulma inquired, "What is the best way to position a button at the center of a

<div class="container"> <button class="btn">Click me</button> </div> <button class="is-centered"> seems to be malfunctioning. ...

How to turn off pinch to zoom feature in Mozilla Firefox on a Microsoft tablet or touch screen display

My goal is to enable zooming and panning for an SVG across all devices and browsers. I've successfully implemented this using panzoom.js. However, I'm encountering an issue specifically with Mozilla Firefox on tablet devices and Windows touch sc ...

What is the best way to include an href link in my SharePoint button?

I styled some buttons with CSS, but now I want to add links to all of them. I've attempted using JavaScript to change the <button> tag to <input>, and also tried adding an onclick event in CSS like <button onclick="href">, but none o ...

Is it possible to link content to various div elements simultaneously?

Trying to explain my query visually, as I'm struggling with how to convey it in words. Feel free to ask for any clarifications! Clarification: In slide one, image 01 and image 02 are initially visible. When someone clicks on the images, the text and ...