Menu made with CSS that disappears when clicked

I designed a menu with the following structure:

<div class="nav">
    <div class="drnav">
        <ul class="ulMenu">
            <li>
                <div class="menuHeader">My Home</div>
                <div class="menu-content">
                    <ul>
                        <li><a href="">item1</a></li>
                        <li><a href="">item3</a></li>
                    </ul>
                </div>
            </li>
            <li>
                <div class="menuHeader">My Stuff</div>
                <div class="menu-content">
                    <ul>
                        <li><a href="">item4</a></li>
                        <li><a href="">item6</a></li>
                    </ul>
                </div>
            </li>
        </ul>
    </div>
</div>

In my CSS, hovering over a menuHeader element displays the corresponding menu-content element (with display: inline). However, I am seeking a solution where clicking on a link within the menu content makes the menu disappear without using JavaScript. Some solutions suggest using pointer-events, but this is restricted to IE 11 and above, while I aim to support at least IE 10 or even 9. Any advice on achieving this compatibility?

Answer №1

While technically feasible, implementing a pure CSS solution for menu functionality may be more trouble than it's worth:

.ulMenu .menu-content {
  display: none;
}
.ulMenu > li:hover .menu-content {
  display: inline-block;
}
.ulMenu > li .menu-content:target {
  display: none;
}
<div class="nav">
    <div class="drnav">
        <ul class="ulMenu">
            <li>
                <div class="menuHeader">My Home</div>
                <div class="menu-content" id="menuContent_1">
                    <ul>
                        <li><a href="#menuContent_1">item1</a></li>
                        <li><a href="#menuContent_1">item3</a></li>
                    </ul>
                </div>
            </li>
            <li>
                <div class="menuHeader">My Stuff</div>
                <div class="menu-content" id="menuContent_2">
                    <ul>
                        <li><a href="#menuContent_2">item4</a></li>
                        <li><a href="#menuContent_2">item5</a></li>
                    </ul>
                </div>
            </li>
        </ul>
    </div>
</div>

In addition, once you close a menu, the only way to reopen it is by opening another one and hovering over the initial menu.


Important note:

I want to emphasize that using a :hover-based menu in CSS may not be ideal with the increasing use of touch devices in today's web traffic. Only less than 1% of users have JavaScript disabled, so opting for a JavaScript-based menu could provide a better user experience overall.

If you could explain your preference for a pure CSS solution, I would appreciate it. In my experience, the only practical use for pure CSS menus was on pages where JavaScript was restricted, like on payment gateways. Do you have other specific reasons for choosing CSS-only solutions?

While I am proficient in CSS, I personally prefer using JavaScript for DOM manipulations rather than trying to replicate them in CSS. Each tool has its strengths, and JavaScript is better suited for dynamic interactions. Use the right tool for the job.


Here is the input/label-based solution I mentioned earlier in the comments. By utilizing the :focus state, I can simulate hiding/showing menu contents without JavaScript, although it still has some limitations. It's the closest alternative I can think of for achieving similar functionality purely with CSS:

.menuHeader input:focus + label,
.menuHeader label {
  display: none;
}
.menuHeader:hover label
{
  display: inline-block;
}
input.hidden {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}
<ul class="ulMenu">
  <li>
    <div class="menuHeader">
      <div>My Home</div>
      <input id="menuContent_1" class="hidden" type="text" />
      <label class="menu-content" for="menuContent_1">
        <ul>
          <li><a href="#menuContent_1">item1</a></li>
          <li><a href="#menuContent_1">item2</a></li>
        </ul>
      </label>
    </div>
  </li>
  <li>
    <div class="menuHeader">
      <div>My Stuff</div>
      <input id="menuContent_2" class="hidden" type="text" />
      <label class="menu-content" for="menuContent_2">
        <ul>
          <li><a href="#menuContent_2">item4</a></li>
          <li><a href="#menuContent_2">item5</a></li>
        </ul>
      </label>
    </div>
  </li>
</ul>

Answer №2

  1. Modified the HTML tags for improved clarity rather than semantics. I prefer using Nested lists with <dl>, <dt>, and <dd> elements to enhance visualization of list levels.
  2. Utilized hidden radio inputs to effectively maintain a persistent 'state' (such as 'on' or 'off') purely through CSS.
  3. To target elements, I matched pairs of <label> and <input type="radio"> elements.
  4. Employed the visibility property due to its ability to maintain child elements visible even if the parent element has visibility: hidden set, provided the child has visibility: visible explicitly defined.

I'm uncertain about the practicality of removing a menu's parent and whether the original poster desired the parent to reappear or not. Consequently, when menu items are clicked, the headings aren't completely gone but rather invisible. To restore them, simply click on the space above the lists.

SNIPPET

body {
  background: #222;
}
li {
  text-decoration: none;
  display: inline-block;
  cursor: pointer;
  padding: 3px;
  margin-bottom: 12px;
}
.rad {
  display: none;
}
.rad + label,
dd {
  visibility: hidden;
}
dl:hover dd,
.rad:checked + label {
  cursor: pointer;
  visibility: visible;
  color: #fc2;
}
dd label:hover {
  background: #930;
  border: .5px solid cyan;
}
<nav class="mainNav">
  <div class="drNav">
    <ul class="mainMenu">
      <li>
        <dl class="menuContent">
          <input id='rad0' class='rad' name='radA' type='radio' checked>
          <label for='rad0'>
            <dt class="menuHeader">HOME___</dt>
          </label>
          <dd>
            <label for='rad1'>
              <input id='rad1' class='rad' name='radA' type='radio'>Item1
            </label>

          </dd>
          <dd>
            <label for='rad2'>
              <input id='rad2' class='rad' name='radA' type='radio'>Item2
            </label>

          </dd>
        </dl>
      </li>
      <li>
        <dl class="menuContent">
          <input id='rad3' class='rad' name='radB' type='radio' checked>
          <label for='rad3'>
            <dt class="menuHeader">CONTENT</dt>
          </label>
          <dd>
            <label for='rad4'>
              <input id='rad4' class='rad' name='radB' type='radio'>Item3
            </label>

          </dd>
          <dd>
            <label for='rad5'>
              <input id='rad5' class='rad' name='radB' type='radio'>Item4
            </label>

          </dd>
        </dl>
      </li>
    </ul>
  </div>
</nav>

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

Issue with ng-cloak not resolving flicker on button in Chromium; strangely, ng-cloak CSS doesn't apply as expected

Here is a snippet of my HTML code: <button type="button" class="btn btn-primary ng-cloak" ng-click="ctrl.add(ctrl.userProfile.username)" ng-hide="ctrl.userProfile.added">Add</button> The code above is a part of the larger HTML document shown ...

What are the characteristics of a well-crafted HTML inline CSS form?

I am looking to create bubbles on a webpage to display content. I decided to create a CSS class called .bubble and added positioning values as inline styles. However, this resulted in long lines of code. My experience with various programming languages has ...

Sass issue: extending selector groups is not permitted

Every time I try to compile my SCSS file into CSS, I encounter an error. The specific error message states: "selector groups may not be extended" pertaining to the line @extend .btn, .btn-link;. Just a heads up: I have Bootstrap imported in my primary scs ...

Is there a way to align just one side of a stacked container and container-fluid in Bootstrap 5?

I am currently working on a design project using Bootstrap 5 where the navbar is contained and the hero header section below extends beyond the container's boundaries only on the right side. Below is an image depicting the design I am aiming for: ht ...

Enhance your form submission with the power of jQuery and jQuery mobile

I am attempting to create a collapsible sidebar on my website that includes a search form with a search field and submit button. In order to accomplish this, I have included the following scripts: https://code.jquery.com/jquery-1.11.3.min.js and https: ...

Positioning Tumblr blockquotes beneath each other, rather than within

Just diving into the world of HTML/CSS and I've been working hard on creating my own Tumblr theme (almost finished!). One issue I'm having is styling the replies, which are in blockquote form. I want them to display one after another in a line li ...

Arranging shapes for optimal placement on a web page

I recently created a vibrant red SVG shape that resembles this: https://i.sstatic.net/E3P3O.jpg My goal is to incorporate these shapes into a design that looks like the following: https://i.sstatic.net/8Yf8L.jpg I attempted to embed the SVG file in an ...

Django is experiencing difficulties showcasing static images and loading CSS files

Working with PyCharm 2019.2.5, Python 3.7, and Django 2.2.5, I am in the process of developing a website by importing a template and creating apps for it through Django. However, I am facing issues with the static files not loading properly. Despite runnin ...

Initiate CSS animation upon modification of component properties

I am looking for a way to create a fade in/out effect on a component whenever its prop changes. Unlike basic examples that toggle styles based on boolean conditions, I want the element's visibility state to be updated directly through prop changes. F ...

Is it possible to create a fading effect on an image using a border-radius of 50%?

Struggling with fading out an image that's circular in shape due to the border-radius: 50%. <section id="main"> <h1>Non-Important-Text</h1> <h4> it's all fun and games until you can't find a damn sol ...

Converting Apache POI Word documents to clean HTML stripping out styles and superfluous tags

I am currently working on converting Word documents to clean HTML. I have been using Apache POI, but it seems to create messy output similar to MS Word's own HTML saving method. What I really need is a solution like the one offered by . For instance, ...

Mixing together an array of colors, experimenting with adding a touch of transparency

Here is my first question, diving right in. I recently created a canvas in HTML and followed a tutorial to generate random floating circles that interact with the mouse position......if you want to check it out, click here. The issue I'm facing now ...

Unusual ways that backgrounds and borders behave while incorporating CSS transitions for changes in height, positions, and transformations

I was under the impression that I had come up with a brilliant idea... I wanted to create a button with a hover effect where the background would do a wipe transition (top to bottom, left to right) using soft color transitions. My plan was to achieve this ...

Create a unique look by applying styling to a nested class without affecting the outer class with the same name

I have two divs with the same class, .body, but one is nested inside another div with the class .box. Here's an example: <div class="box"> <div class="body"> </div> </div> <div class="body"> </div> I'm ...

Launch a new pop-up window that will disappear once the user clicks outside of the window

In my Java web application, I'm attempting to create a pop-up window that appears over the parent window when a user clicks on a link. The pop-up window will display values fetched from a database using Hibernate, and the user can select a value. Once ...

Images obscure dropdown menu

The issue I am experiencing with my blog is that the dropdown menu is appearing behind the image slider on the main page. You can see it here: Can anyone offer guidance on how to prevent this from happening so that the dropdown menu is not obscured? Just ...

Different tab designs with a variety of color schemes

I am looking to implement various color combinations for different tab selections using the background-color property. For instance: About: red .nav-tabs, dark red for .active, Services: yellow .nav-tabs, dark yellow for .active, .... .... and so fort ...

Create a CSS header with a fading background that extends horizontally to the left and right

I'm looking to create a header with a unique color transition effect - a left fade from light blue to blue, a center that is solid blue, and a right fade from blue to light blue. The header should span the full width of the page. Can anyone suggest th ...

How can I attach :after and :before pseudo-elements to a submit button?

I'm attempting to enhance my submit button with :after/:before elements, but I'm having trouble getting it to work properly. Specifically, I want to add a swipe (Sweep To Right) transition effect on the button from left to right. Similar to - th ...

Is there a way to achieve a similar outcome on my site?

Recently, I noticed a mouse-hover effect on two websites and found it quite appealing. https://i.sstatic.net/Ly0gP.png https://i.sstatic.net/oDe1i.png This is the specific effect that caught my attention. Can anyone provide me with insight on how to impl ...