Triggering a targeted action upon the user's click on any HTML element

Currently, I am in the process of developing navigation buttons that trigger a dropdown sub-menu when clicked by utilizing checkbox inputs. Upon clicking the label, the input is checked, causing the menu to drop down, and when the label is clicked again, the menu collapses back up.

However, I am now faced with a specific requirement. I am attempting to design the dropdown menu in such a way that it collapses when any HTML element is clicked, eliminating the need for the user to click on a specific element to close the menu.

Below is the HTML & CSS code tailored for the dropdown menu:

<ul class="down-bar" style="list-style:hidden">
    <div class="dropdown">
        <input type="checkbox" value="drop" id="drop-1" class="dropper">
        <li><label class="down-nav" id="down-nav-1" for="drop-1" style="text-decoration:none">Click <b class="caret"> &#9660;</b></label></li>
        <div class="dropdown-menu">
            <a href="#">Link</a>
        </div>
    </div>

CSS:

.dropdown {
  display: inline-block;
  float: left;
  padding-top: 0em;
  transition: 0.1s ease-in;
  margin-top: 1.5%;
  white-space: nowrap;
}
.dropdown-menu {
  float: left;
  display: none;
  top: 0%;
  margin-top: 2.2%;
  width: 8%;
  min-width: 50px;
  padding-left: 18px;
  background-color: rgba(26, 26, 26, 2);
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 99;
  list-style-type: none;
  transition: 0.1s ease-in;
  border-color: black;
  border-radius: 3px;
}
.dropdown-menu a {
  color: white;
  font-size: 0.9em;
  padding: 0px 2px 0px 0px;
  text-decoration: none;
  display: block;
  list-style-type: none;
  white-space: nowrap;
  left: 0%;
  margin-left: 0%;
}
.dropdown-menu a:hover {
  color: #C90205;
}
.dropper {
  display: none;
  visibility: hidden;
}
.dropper:checked ~ .dropdown-menu {
  display: block;
  z-index: 99;
}
.dropper:checked ~ li #down-nav-1 {
  border: solid 3px gray;
  background-color: gray;
  margin-top: 1.5%;
  margin-left: 3%;
  border-radius: 10px;
}

In my attempts, I experimented with different approaches, including using a full-width anchor tag. However, this caused the main menu to disappear. I considered utilizing the :active tag to revert the menu's display back to None. You can view the full code (remove comment tags for full-width example).

Ultimately, I am seeking a solution that doesn't result in other elements disappearing. Do any errors exist in my code? Is the initial example a viable solution, and if so, in what way? How can I ensure the menu collapses when the user clicks any HTML element? Is it feasible to achieve this without resorting to Javascript?

Answer №1

If you are comfortable with utilizing a JavaScript solution, you can refer to the code snippet below:

$(document).click(function(e) {
  if (! ($('#down-nav-1').is(e.toElement) || $('#drop-1').is(e.toElement))) {
    $('.dropper').prop('checked', false)
  }
});
.dropdown {
  display: inline-block;
  float: left;
  padding-top: 0em;
  transition: 0.1s ease-in;
  margin-top: 1.5%;
  white-space: nowrap;
}
.dropdown-menu {
  float: left;
  display: none;
  top: 0%;
  margin-top: 2.2%;
  width: 8%;
  min-width: 50px;
  padding-left: 18px;
  background-color: rgba(26, 26, 26, 2);
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 99;
  list-style-type: none;
  transition: 0.1s ease-in;
  border-color: black;
  border-radius: 3px;
}
.dropdown-menu a {
  color: white;
  font-size: 0.9em;
  padding: 0px 2px 0px 0px;
  text-decoration: none;
  display: block;
  list-style-type: none;
  white-space: nowrap;
  left: 0%;
  margin-left: 0%;
}
.dropdown-menu a:hover {
  color: #C90205;
}
.dropper {
  display: none;
  visibility: hidden;
}
.dropper:checked ~ .dropdown-menu {
  display: block;
  z-index: 99;
}
.dropper:checked ~ li #down-nav-1 {
  border: solid 3px gray;
  background-color: gray;
  margin-top: 1.5%;
  margin-left: 3%;
  border-radius: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="down-bar" style="list-style:hidden">
    <div class="dropdown">
        <input type="checkbox" value="drop" id="drop-1" class="dropper">
        <li><label class="down-nav" id="down-nav-1" for="drop-1" style="text-decoration:none">Click <b class="caret"> &#9660;</b></label></li>
        <div class="dropdown-menu">
            <a href="#">Link</a>
        </div>
    </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

Issue with CSS 3 gradient compatibility in Internet Explorer 8

My CSS3 gradient button is working perfectly on all browsers except for IE8. To make it work on IE8, I am utilizing CSS3 Pie. You can see the search button in action by visiting: Upon inspecting my console, I noticed an error on this line: PIE.htc, lin ...

Align a span vertically within a div

I need help aligning the content "ABC Company" vertically within a div using Bootstrap 4's alignment helper classes. Despite my efforts, I have not been successful. Here is the code I am using: <div class="container" style="width: 740px;"> < ...

What is the best way to create an element that is clickable but transparent in appearance?

Is there a way to achieve an inset box shadow on elements like an iframe without blocking clicks on the element itself? The common strategy of overlaying a div over the iframe achieves the desired visual effect but unfortunately hinders interaction with th ...

Interacting with a JavaScript button using C# WebBrowser

Can someone please assist me with a problem I'm facing? I have a webpage coded in HTML and Javascript. Whenever I use webBrowser1.Document.GetElementById("hmenu").InvokeMember("click");, the menu pops up on my screen. However, I am unsure how to cli ...

Improving Consistency: Achieving a Uniform a:hover Effect for Links Listed in Lists

One issue I'm having is with creating a list of links and styling them with a:hover. The problem arises when the list has a defined width wider than the links themselves, causing the LIs to change color before the As on hover. It creates a messy appea ...

Altering the playback of a flash object using HTML or JavaScript

Can the playback speed of a flash object be adjusted without recompiling the object, like through HTML attributes or JavaScript? Appreciate any help in advance ...

Display two examples from the material-ui documentation

My goal is to merge two demos found in the material-ui documentation. Clipped under the app bar - Describes a page layout Fixed header - Illustrates a table with a fixed header. I am looking for a solution where the table occupies the entire available p ...

hover causing the table cell to act erratically

My table consists of three cells with widths of 30%, 40%, and 30% respectively. The table itself occupies 25% of its container, which can range from 1024px to 400px. The first cell contains a button labeled GALLERY. Upon hovering over the cell, the text c ...

Three dots implemented in the heading of a card

In my Bootstrap 4 project, I am aiming to design a card header with three distinct parts. The left part will showcase the author. The middle section will present a preview of the content. The right part will display the date. Specifically, I want the mid ...

Trouble with CSS and JS tabs not activating when clicked?

I am experiencing issues with a navigation (organized in tabs) that is not functioning on this page, but it works correctly on this page using the same method for inclusion. When clicking "Norway" on the top left, the navigation opens but switching to the ...

The static files are being received by Django but are not functioning properly

I've been working on a project using django 1.7, and below is my settings.py configuration: STATIC_URL = '/static/' STATICFILES_DIRS = ( os.path.join(BASE_DIR, "assets"), ) STATIC_ROOT = "absolute/path/to/static" In the 'assets&apo ...

Use jQuery to automatically update a variable every 5 seconds

For the first time, I am using django to build a webpage and facing a challenge in fetching the value of a variable from a .py file every 5 seconds. Here is the HTML code snippet: <!DOCTYPE html> <html> <head> <title&g ...

The CSS linear gradients rainbow wheel is missing a few sections, only displaying part of its 12

I've almost got everything working, but I'm facing a challenge in displaying all 12 sections of the rainbow wheel. I know it's probably something very simple (Occam's razor). I've been following this amazing tutorial on CSS linear ...

CSS vertical alignment property

I'm facing a rather simple problem that has left me scratching my head. The vertical align property is performing correctly, but as soon as I introduce a tag after the text meant to be centered along the image, any text that follows the tag appe ...

Swapping out a button

I tried searching for a solution to my problem but I couldn't find it because of my limited English proficiency. Therefore, I apologize if my question has already been answered. https://i.sstatic.net/XhQBM.jpg I would like to place my button in the ...

Adjusting the height of the navigation bar in BootStrap

I'm having trouble resizing the height of the navbar-default in this Bootstrap sample. It seems to be too big for my needs. Are there any Bootstrap classes I can use to adjust the height? For example: <form class="navbar-form navbar-left" role="s ...

Prevent span/button clicks by graying them out and disabling the ability to click using HTML and JQuery

I am facing a challenge with my two spans that reveal specific information when clicked. I want to make one span appear as a disabled button (greyed out) when the other span is clicked. I tried using $("#toggle-odd").attr("disabled", tr ...

Having trouble debugging a website remotely? Dealing with a JSON problem specifically in IE8

Currently, the website is functioning flawlessly on various machines except for those used by the client in Africa. Unfortunately, I have no way of accessing his specific machine due to geographical constraints. The client has limited IT knowledge, and we ...

How to eliminate excess white space on the right side in Bootstrap 4

Could someone please clarify why I am experiencing a white space on the right side when using Bootstrap 4? This is the primary code block for the page. <main id="intro" role="intro" class="inner text-center"> <h2>Lorem Ipsum</h2> ...

Ways to determine if an object has a provided attribute?

As I work on creating a small website using django, I encountered an issue: In my site, I want to view posts in detail, but not every post includes an image attribute - leading to errors when trying to display images that do not exist. The solution lies ...