Is there a simple way to create a clickable popup menu without using JavaScript?

/*---Creating a Popup Menu with CSS---*/
.box{
position:fixed;
top: 0.1%;
left: 14px;
display: inline-block;
cursor: pointer;
}
ul{
list-style-type: none;
}
ul li{
font-family: Lato;
padding: 10px;
}
ul li a{
text-decoration: none;
color: black;
}
ul li:hover{
background-color: bisque;
}
.box1{
position: absolute;
top:3%;
left:15px;
z-index: 1;
background-color: floralwhite;
max-width: 260px;
box-sizing: border-box;
box-shadow: 2px 5px 15px black;
display: none;
}
.box2{
    position: absolute;
    top:3%;
    left:8%;
    z-index: 2;
    background-color: floralwhite;
    max-width: 260px;
    box-sizing: border-box;
    box-shadow: 2px 5px 15px black;
    display: none;
}
.box:hover .box1{
display: block;
}
#nav li:focus .box2 {
    display:block;
  }
<!--box-->
<div class="box">
        <img src="https://img.icons8.com/ios-filled/344/menu-rounded.png"
            height="35px"width="35px" id="menu">
              <!--box1-->
        <div class="box1">
            <ul>
         <li id="nav"><a href="#">item1  &#187;</a></li>
            <li><a href="#">item2</a></li>
            <li><a href="#" >item3</a></li>
            <li><a href="#">item4</a></li>
            <li><a href="#" >item5</a></li>
            <li><a href="#">item6</a></li>
            </ul>
           </div>
       </div>
     <!--box2-->
     <div class="box2">
      <ul>
        <li><a href="#">sub-item1</a></li>
        <li><a href="#">sub-item2</a></li>
        <li><a href="#">sub-item3</a></li>
        <li><a href="#">sub-item4</a></li>
      </ul>
    </div>     

I am attempting to design a popup menu that shows up when you click or hover over a small image (essentially an icon) using only HTML and CSS without using JavaScript. However, I am encountering an issue where the main menu appears but the sub-menu does not appear when focusing on a specific item from the list. Is there something I can do to fix this problem? My goal is to create a very simple menu.

Answer №1

You have the option to create a simulated behavior using checkbox inputs along with the :checked attribute.

To achieve this, you can hide the checkbox and assign your menu icon as a label for that input element. By toggling the checked state, you can utilize CSS to control the display instead of relying on hover effects.

/*---CUSTOM MENU STYLING---*/
.box {
  position: fixed;
  top: 0.1%;
  left: 14px;
  display: inline-block;
  cursor: pointer;
}

ul {
  list-style-type: none;
}

ul li {
  font-family: Lato;
  padding: 10px;
}

ul li a {
  text-decoration: none;
  color: black;
}

ul li:hover {
  background-color: bisque;
}

.box1 {
  position: absolute;
  top: 3%;
  left: 15px;
  z-index: 1;
  background-color: floralwhite;
  max-width: 260px;
  box-sizing: border-box;
  box-shadow: 2px 5px 15px black;
  display: none;
}

.box2 {
  position: absolute;
  top: 3%;
  left: 8%;
  z-index: 2;
  background-color: floralwhite;
  max-width: 260px;
  box-sizing: border-box;
  box-shadow: 2px 5px 15px black;
  display: none;
}

#nav li:focus .box2 {
  display: block;
}


/**
 * Checkbox toggle
 */

.menu-toggle {
  display: none;
}

.menu-toggle:checked ~ .box1 {
  display: block;
}
<!--box-->
<div class="box">
  <input class="menu-toggle" id="menu-toggle" type="checkbox" />
  
  <label for="menu-toggle">
    <img src="https://img.icons8.com/ios-filled/344/menu-rounded.png"
      height="35px"width="35px" id="menu">
  </label>
  
  <div class="box1">
    <ul>
      <li id="nav"><a href="#">item1  &#187;</a></li>
      <li><a href="#">item2</a></li>
      <li><a href="#">item3</a></li>
      <li><a href="#">item4</a></li>
      <li><a href="#">item5</a></li>
      <li><a href="#">item6</a></li>
    </ul>
  </div>
</div>

<div class="box2">
  <ul>
    <li><a href="#">sub-item1</a></li>
    <li><a href="#">sub-item2</a></li>
    <li><a href="#">sub-item3</a></li>
    <li><a href="#">sub-item4</a></li>
  </ul>
</div>

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

Implementing a Dynamic Navigation Feature using PHP in a Standalone File

I am currently working on creating a dynamic PHP navigation system that should highlight the active tab, but I'm facing challenges with making the active tab part function properly. While there are simpler methods available, they all involve incorpora ...

Why do I see my footer displayed twice on the page?

<div class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li>@Html.ActionLink("Home", "Index", "Home")</li> <li> @Ajax.ActionLink("Imagenes List", "Images", "Home", new { page = 0 }, new AjaxOptions() ...

Having Trouble Loading PHP File with Jquery

I've been struggling with using JQuery/Ajax to load the content of my PHP file into a div tag. Below is the code snippet from my page that attempts to load the file: <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/ ...

Next.js in combination with Tailwind CSS

While attempting to customize the styling for my Next.js 13 app, I encountered an error message that states: Syntax error: C:\Users\User\Documents\webdev\TicketingApp\ticketing-app\app\globals.css The text-default- ...

Is there a way to temporarily toggle classes with jQuery?

Incorporating ZeroClipboard, I have implemented the following code to alter the text and class of my 'copy to clipboard button' by modifying the innerHTML. Upon clicking, this triggers a smooth class transition animation. client.on( "complete", ...

Issue with the back-to-top button arises when smooth-scrolling feature is activated

This Back To Top Button code that I discovered online is quite effective on my website. // Defining a variable for the button element. const scrollToTopButton = document.getElementById('js-top'); // Creating a function to display our scroll-to- ...

Ways to make a component gradually appear and disappear in an Angular application

I have developed a task management application using Angular and I wanted to implement a fading effect for each task when it is created and before it is deleted. Despite successfully applying the fade in effect at the todo-item component level, I encounter ...

The CSS property overflow:hidden or overflow:scroll is not functioning as expected when applied to a

On my practice website, I have set up a demonstration for showcasing text data. The issue arises when the user inserts an excessive amount of characters in the text box. To address this, I would like the text to be scrollable so that all content can be d ...

Unable to retrieve the value from a textarea when using Shopify Product Options by Bold

I'm currently facing an issue trying to retrieve the value of a textarea using Shopify's Product Options by Bold. The code works fine locally, but when I transfer it over to Shopify, I am unable to get the value. Despite looking at various resour ...

Items that do not commence from the start of the list

I'm encountering an issue with my unordered list where the li elements are not rendering properly. The first element seems to have a margin, and I'm unsure why this is happening. How can I fix this problem? function App() { return ( < ...

What steps can I take to expand this on a grander level?

I have a code snippet for a personality quiz, but I'm facing an issue with increasing its size. Here's the HTML code: <h3>1. What is your favorite color?</h3> <input type="radio" name="q1" value="A" /> A. Red. <input type="r ...

Having trouble getting the Facebook Like Box to function properly

I am having trouble displaying the like box on my website. I followed the code provided at https://developers.facebook.com/docs/plugins/like-box-for-pages and even tried using the HTML5 version, but nothing appears on the page. This is the code I used: & ...

Adjusting ToggleButton hues in react-bootstrap

I am working with a group of ToggleButtons in my app and I want to customize their background colors. Currently, all the buttons have a gradient defined by <.untoggled-button> class, with an added blue overlay for unselected buttons. However, I am fa ...

React splits the page and interrupts the scroll event listener

For some reason, my webpage has been split by the React.js library. When attempting to scroll down while hovering over the top navigation menu, scrolling becomes impossible. However, scrolling works fine when done on any other part of the screen. I' ...

Equal dimensions for all cards in the TailwindCSS gallery display

I am currently working on an image gallery in React with Tailwind CSS. Here's a glimpse of how it looks: https://i.stack.imgur.com/ABdFo.png Each image component is structured like this: <div className="flex items-center"> < ...

How can I retrieve the HTML content of a ReactJS rectangle element saved in an array?

Within a loop, I am creating rectangle elements and adding them to an array: rectangles = []; render: function() { for (var i = 0 ; i < 10; i++) { rectangles.push (<Rectangle height={this.props.afm} width={this.props.afm} x={xpos} y={ypos} va ...

My efforts to resolve the issues in my code have been ongoing, but unfortunately, I have been unable to find a solution

I'm struggling to insert my contact form code into the contact tab without it interfering with the tab bar. I want the form to appear in the middle of the page when the tab is clicked. I've tried various placements for the code but none seem to ...

Enhancing User Experience on Android Devices: Automatic Website Zoom-In Feature for Responsive Design

For the first time, I am delving into creating a responsive website design. I seem to be encountering difficulties when it comes to smartphones. I have been using the following meta-tag to ensure that the website loads "zoomed in". (I found this method on ...

Dynamically adjust the image link in responsive mode to improve user experience

Is there a way to dynamically change the image links in responsive mode on WordPress? I am displaying thumbnails with a fixed size of 280*200, but I want to switch to medium-sized thumbnails (480*200) when the resolution is between 480px to 600px. Is there ...

What is the correct way to write the syntax for wp_register_style function in

I'm attempting to include a cache-buster version in my CSS file. The guides mention: <?php wp_register_style( $handle, $src, $deps, $ver, $media ); ?> However, I've scoured Google and Stack Overflow for the correct formatting of these var ...