Click to hide CSS dropdown menu

Just starting out with CSS and HTML here. I'm working on implementing a dropdown menu using the code below, but it closes when you move the mouse away from it. What I would like is for it to close only when clicked outside the dropdown menu. Can anyone provide a CSS fix to achieve this? You can find my code on JSFiddle at the following link Fiddle link. Your help is much appreciated. Here's how the HTML looks:

<div id="main">
<div class="wrapper">
    <div class="content">
        <content>
            <div>
                <ul> 
                     <a href="#"><li>Lorem ipsum dolor</li></a>
                     <a href="#"><li>Consectetur adipisicing</li></a>
                     <a href="#"><li>Reprehenderit</li></a>
                     <a href="#"><li>Commodo consequat</li></a>

                </ul>
            </div>
        </content>
    </div>
    <div class="parent">Drop Down Parent 1</div>            
</div>

And here is the corresponding CSS:

#main {
margin: 30px 0 50px 0;
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
}
#main .wrapper {
display: inline-block;
width: 180px;
margin: 0 10px 0 0;
height: 20px;
position: relative;
}
#main .parent {
height: 100%;
width: 100%;
display: block;
cursor: pointer;
line-height: 30px;
height: 30px;
border-radius: 5px;
background: #F9F9F9;
border: 1px solid #AAA;
border-bottom: 1px solid #777;
color: #282D31;
font-weight: bold;
z-index: 2;
position: relative;
-webkit-transition: border-radius .1s linear, background .1s linear, z-index 0s linear;
-webkit-transition-delay: .8s;
text-align: center;
}
#main .parent:hover, #main .content:hover ~ .parent {
background: #fff;
-webkit-transition-delay: 0s, 0s, 0s;
}
#main .content:hover ~ .parent {
border-bottom-left-radius: 0;
border-bottom-right-radius: 0;
z-index: 2;
}
#main .content {
position: absolute;
top: 0;
display: active;
z-index: 2;
height: 0;
width: 180px;
padding-top: 30px;
-webkit-transition: height .5s ease;
-webkit-transition-delay: .4s;
border: 1px solid #777;
border-radius: 5px;
box-shadow: 0 1px 2px rgba(0, 0, 0, .4);
}
#main .wrapper:active .content {
height: 123px;
z-index: 3;
-webkit-transition-delay: 0s;
}
#main .content div {
background: #fff;
margin: 0;
padding: 0;
overflow: hidden;
height: 100%;
border-bottom-left-radius: 5px;
border-bottom-right-radius: 5px;
}
#main .content:hover {
height: 123px;
z-index: 3;
-webkit-transition-delay: 0s;
}

I want the dropdown menu behavior to be as follows: 1) Click to show the dropdown. 2) Keep displaying when hovered over, even outside the dropdown. 3) Close when clicked somewhere else besides the dropdown.

Right now, I'm using hover selectors which is why it behaves that way. But I really want to change it to the above behavior and I'm not sure how.

<div id="main">
<div class="wrapper">
    <div class="content">
        <content>

        </content>
    </div>
    <div class="parent">Drop Down Parent 1</div>

Answer №1

It appears there is an error in your HTML code. You have placed the <li> element inside the <a> tag, which is causing issues. This is a common mistake that needs to be corrected.

To fix this issue, update your HTML as follows:

<a><li>...</li></a>

Change it to:

<li><a>...</a></li>

By making this adjustment, you will resolve the problem and the click function should work properly. Remember, an <a> tag cannot contain an <li> element within it for the click event to function correctly.

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

The navigation bar's dropdown list is malfunctioning and displaying an error message

Currently, I am working on a project that involves using Bootstrap and jQuery. However, when I tried to implement the navigation code, I encountered an error in the console: bootstrap.min.js:6 Uncaught TypeError: i.createPopper is not a function at xt. ...

How to implement bi-directional scroll animation in JavaScript for top and bottom movements

In my JS project, I experimented with creating animations using the bounding client feature. By scrolling down to reveal certain text or content, I was able to adjust its opacity to 1 by applying the CSS class ".active". However, once the user scrolls back ...

Bootstrap 5: The limitations of utility usage

Recently, I decided to upgrade my project from Bootstrap 4.5 to Bootstrap 5. The process involved overwriting the old bootstrap files with npm install bootstrap@next. However, after making this transition, I encountered a problem where I could no longer us ...

How to eliminate .php and .html extensions from your URLs using htaccess

I'm facing an issue where Google has indexed some pages using the wrong URLs. The URL they have indexed is: http://www.example.com/user/emp.php while I want it to redirect to: http://www.example.com/user/emp Here is my .htaccess file with the nec ...

Unable to successfully send multiple values from input fields using forms in PHP

I am currently facing an issue with my HTML form that contains a field consisting of multiple selection boxes as shown below: <div class="form-group"> <select multiple name="Fee[]"> ...

What are some techniques to ensure that my "display: grid" layout adjusts properly on mobile devices and tablets by implementing media queries?

Responsive Grid Design I am looking to transform my grid desktop view into a responsive layout for mobile and tablets using media queries in CSS. While it looks great on desktop, it does not adjust well on smaller screens. .grid-container { display: gr ...

JQuery problem with toggling the menu icon

I have a toggle control that switches between a menu icon and an X when clicked, displaying an overlay nav as well. The issue is: Clicking on any link in the overlay nav does not switch/toggle the X back to the default menu icon. In the code below, I inc ...

Fill the second dropdown menu options based on the selection made in the first dropdown menu

I need assistance with dynamically populating my second drop-down menu based on the selection made in the first drop-down. Here are the steps I've taken so far: form.php - Utilizing javascript, I have set up a function to call getgeneral.php. The se ...

Tips on attaching a class to elements in a loop when a click event occurs

In my HTML, I am displaying information boxes from an array of objects that are selectable. To achieve this, I bind a class on the click event. However, since I am retrieving the elements through a v-for loop, when I select one box, the class gets bound to ...

Is the Paypal button causing issues with the navigation bar?

I recently encountered an issue while trying to integrate the Paypal smart button into my first project. Whenever the Debit or Credit card button is clicked, the text above it overlaps with the navbar. I attempted to add extra space to the background image ...

What is the best location for storing css files in Laravel?

I've come to realize that I can easily make changes to the app.scss file in my Laravel projects and see those changes reflected by running npm run dev or npm run watch. However, I'm faced with a dilemma when dealing with multiple .css files. Whe ...

What steps can I take to ensure that this list remains fixed at the bottom of the page?

Looking to have this content positioned at the very bottom of my webpage without any empty space below it. I'm having trouble applying the usual CSS to achieve this. .footer li{ display:inline; } <footer> <div cla ...

Expanding the rowspan within a table column has the effect of reducing its overall width

I have created a table with two rows, where the first row has three columns and the second row has two columns. The middle column in the first row has been set to rowspan="2". However, the issue is that it appears smaller than its intended width. .kolon ...

Is the username you want available?

I am facing a challenge in my registration form validation process where I need to verify the username input using javascript and flask in python, but the implementation is unclear to me. The requirement is to utilize $.get in the javascript segment along ...

Tips on eliminating the excess whitespace remaining at the bottom of the webpage when using JavaScript parallax scrolling

I'm currently working on a project that involves using parallax effects for images and divs. However, I've noticed that there is some space left at the end of the parallax effect for unknown reasons. Could someone please assist me in removing thi ...

What is the method for incorporating a currency symbol into a jQuery mask?

I have successfully implemented the mask on the <td> element of the table, but now I would like to include the currency symbol before the value as shown in the example below. if ($(this).attr('title')==='Value') { $(+ ...

Using a width of 100% with the display inline-block property does not function as intended

Is there a way to have both a checkbox and text input in one line, with the text input being responsive? I attempted to use display: inline-block, which worked for keeping them in the same line, but I'm having trouble making the text input fill the re ...

Preventing automatic interpretation of double curly braces within a Script tag in Handlebars

In my development project, I am constructing a visual representation of a database table along with various web application functionalities. The technologies being utilized include node.js, express, mysql, bootstrap, and handlebars. Within my pages.js fil ...

Personalize the color scheme of your HTML template

I recently downloaded a template from html5up.net, specifically the one located here. My intention was to add my own touch by changing the blue color to orange. I managed to do so by tweaking the main.css file and it worked like a charm. However, the res ...

Enhancing website aesthetics through CSS styling

My goal is to create websites with a fresh, updated design. One example of a site I admire is Manta Media Inc I assume that CSS plays a big role in achieving this look. What keywords should I use when searching on Google to learn how to build a web inte ...