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

Input field in a tableview

I have a ListView that features a dropdown, 4 textboxes and buttons. My goal is to only show the fourth textbox (enclosed in a span) when the dropdown value in each row of the ListView is set to 2. For instance, if there are 5 records being displayed in t ...

Create a dynamic image positioned alongside text specifically designed for mobile devices

One of my challenges involves implementing a responsive image in Bootstrap (3.3.7): <div class="col-sm-2 text-center"> <img class="img-responsive center-block " src="img/1.png" style="max-width: <p>I would like to include some gua ...

Prompting the website to 'refresh' and return to the beginning of the page

My website's functionality is closely tied to the user's position on the page. Different features are triggered once specific distances are reached. I am seeking a way for users to automatically return to the top of the page upon page reload. Cu ...

What is the best way to combine PHP and HTML within a JavaScript script?

I'm facing a challenge with integrating dynamically added elements and a populated drop down on my form. Is there a way to combine the two seamlessly? Below is the code snippet I'm using for dynamically adding and removing elements: $(docu ...

Organize table information using rowspan functionality

View of Current Table I am looking to implement a side column in my table using rowspan to group dates within each pay period. The goal is for a supervisor to be able to create a new pay period, which will assign a unique integer in the database and then ...

Maintaining the active style of Asp.NET 4.0 Menu control even after the mouse is released

Utilizing a standard asp.net menu in an asp.net 4.0 web application, this setup is for a standard web application and not any version of MVC applications currently available. The issue at hand is relatively straightforward. CSS styles are applied to the d ...

Unleashing the power of React: Integrating raw HTML <a href> tags with JavaScript

I am currently working on a small web app that mimics browsing WikiPedia by fetching raw HTML content of WikiPedia articles through its API. I then display this HTML in my app using "dangerouslySetInnerHTML". I am faced with the challenge of enabling a us ...

Unable to post form data into database due to Javascript undefined data situation

I've been working on an HTML form that can interact with a PHP API to retrieve client data and then update user stats in a MySQL database. Currently, I am converting the data into a JSON object and using JSON.stringify to create a string for sending ...

Expand the display on mobile devices

What is the solution for adjusting a table on mobile view? I am aiming to achieve: https://i.sstatic.net/YkKAW.png However, it currently looks like this: https://i.sstatic.net/GW5mX.png Here are my code snippets: <script src="https://cdnjs. ...

How can I retrieve values from HTML5 data attributes using Selenium in Python?

How can I extract the value "165796011" from data-gbfilter-checkbox? I attempted to do so using the following code: element= driver.find_element_by_css_selector('#widgetFilters > div:nth-child(1) > div.a-row.a-expander-container.a-expander-inl ...

Is there a way to achieve a straightforward three-column stacked layout in Bootstrap 5 that spans 100% of the viewport height?

Today I decided to try out Bootstrap 5 for the first time. However, I am facing a challenge in creating a 3 stacked column layout with box-1, box-2, and box-3 on top of each other, taking up 100vh. I have shared a redacted version of the code below, wher ...

Saving HTML data into the WordPress database with the help of Ajax

After developing a WordPress plugin, I encountered an issue while working with div elements. My goal is to retrieve the content of a specific div using the following code snippet: var archive = j( "div.page2" ).html(); console.log(archive); Everything wo ...

When using CKEditor, pressing the Enter key results in the insertion of <br /> tags

Whenever I use ckeditor, I find that pressing enter results in two <br /> tags being inserted. While I want to keep the line break tags, having them appear twice is not ideal. In my config.js file, I have set the configuration for the enter key as f ...

The outerHeight of Elements measured in pixels

Is there a way to increase the outerHeight() function by adding extra pixels? Let's say we have a variable storing the outerHeight of .pg-sect: var $section = $('.pg-sect').outerHeight(); Now, if I want to add an additional 70px to the he ...

What is the best way to attach an image to the image section coming from the backend in vue.js?

I have developed a frontend page that displays a list of books, with the data coming from the backend. The backend response includes image files, and I am struggling to bind these images to the image section of my template. I have the image paths from the ...

Prevent unauthorized entry to css and javascript files

Is there a way to prevent direct access to a file? I want the file to be used on my website, but I want to block it from being accessed directly. For example, if you try to open this link: https://example.com/style.css, you will see an error message. Howev ...

What's the reason this picture isn't displaying in its full size?

I am having trouble adjusting the size of this image to fit the full width, but there seems to be some remaining space on the right side. Category: CSS/HTML /* Header */ .header { background: url('https://picsum.photos/1920/1080') center ...

What is the best way to navigate back to the main view once a dialog has been opened

I am working on a web application with two HTML pages. On the second page, I have implemented a dialog using CSS only, without any JavaScript. The first page contains a single button: <button onClick="redirectToDetails()">Go to details</button&g ...

v-for loop to populate dropdown menu with identical values

Recently, I started working on a points counter app using Vue.js and encountered some issues with the v-for loop and dropdown menu functionality. Within my Vue.js application, I have an array of players as shown below: var app = new Vue({ el: '#l ...

Explaining the precise measurements of font size in CSS

I recently started diving into the world of CSS and picked up a copy of Eric Meyer's 3rd edition of CSS: The Definitive Guide. While reading through his section on font sizes (page 107), I came across an interesting concept - that the font size deter ...