Change the CSS hover menu to function as a click menu instead of a hover

I am working on a CSS/HTML menu that includes the following CSS code:

#nav {
    background-color:#F36F25;
    margin:0 0 5px 0;
    width: 100%;
    height:35px;
    left:0;
    z-index:1;
    border-top:2px solid #FFFFFF;
    border-bottom:2px solid #FFFFFF;
}
#nav>li {
    float:left;
    list-style:none;
}
#nav li:hover ul {
    position:absolute;
    display:block;
    z-index:9999;
}
#nav li a {
    display: inline-block;
    padding: 8px;
    margin:0;
    background: #F36F25;
    text-decoration: none;
    color: #FFFFFF;
    border:1px solid #F36F25;
}
#nav li:hover > a, #nav li a.active {
    color:#F36F25;
    background: #FFFFFF;
    border:1px solid #F36F25;
    cursor:pointer;
}
#nav li ul {
    position:absolute;
    display: none;
    list-style:none;
    margin:0;
}
#nav li ul li {
    margin-top:0;
    margin-right:0;
    margin-bottom:0;
    margin-left:-40px;
}
#nav li ul li a {
    background-color: #F36F25;
    color:#FFFFFF;
    border:1px solid #F36F25;
    width:145px;
}
#nav li ul li a:hover {
    background-color: #FFFFFF;
    color:#F36F25;
    border:1px solid #f36f25;
}
.clearfix:after {
   content: " "; /* Older browser do not support empty content */
   visibility: hidden;
   display: block;
   height: 0;
   clear: both;
}
.clearfix:after {
  content: "";
  display: table;
  clear: both;
}

Is there a way to keep the onhover events intact but have the sub-menus appear on click instead of when the user hovers over the parent link?

I am open to using jQuery or JavaScript if necessary, but I would prefer a solution that primarily utilizes CSS.

Answer №1

Instead of using :hover, you may want to consider utilizing :focus in the code snippet below:

You can also achieve this through JavaScript with a JSFiddle demo

#nav li:focus ul {
    position:absolute;
    display:block;
    z-index:9999;
}

Answer №2

Imagine you are looking to switch up your display methods with a simple click on an element. Let's say this element is identified by a selector. In such a scenario, you can easily set up this event using jQuery:

$(selector).click(function() {
    if ($(this).hasClass("clicked")) {
        $(this).removeClass("clicked");
    } else {
        $(this).addClass("clicked");
    }
});

Additionally, make sure to define some CSS for the class .clicked. For instance, let's assume your selector is #foo. Here is what your CSS rules should look like:

#foo {
    /*Add styles here*/
}

#foo.clicked {
    /*Add different styles when clicked*/
}

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

Editing HTML on an ASPX website can be a breeze with

Hello there, I've been tasked with editing a website for a client, but the site is located on a local web server and was designed using aspx. As I review all the files, I am encountering difficulty finding the HTML code associated with runat=server w ...

Incorrect form of SphereGeometry detected in three.js

I'm having trouble rendering a simple sphere in three.js because it's appearing distorted (looking more like a vertical stick). Below is my HTML code: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

What is the best way to progressively load sections of an HTML table?

I'm facing an issue with a table that is rebuilt asynchronously every time a new option is chosen from a dropdown. The HTML generation works fine, but sending it back across the wire breaks when the size is too large. One idea I had was to query sect ...

Tips for presenting dummy content in an Angular application with HTML

I have created a mock service file and I would like to display it in my HTML, but I'm not quite sure how to make it display correctly. Any help or suggestions would be greatly appreciated. <div class="container2"> <div class=" ...

The submenu dropdown on the navigation bar appears once you hover over the parent bootstrap element

Hey there! I'm currently facing an issue where the submenu dropdown is displaying when its grandparent is hovered, rather than just its parent. Here's the code snippet: <nav class="navbar navbar-default"> <div class="container"> ...

Guidelines on creating actionable functions using JavaScript

Having trouble connecting the form to the database and looking for a solution. This is the HTML form that I've been working on, attempting both POST and GET methods. <form id="register" action="script/register.js" method="post"> <label for= ...

When styled with an id, the DIV element does not inherit styles from a CSS class

I seem to be facing an issue with the code below, possibly due to using both class and id in the same div. Despite knowing they are meant to work together. For more information: Can I use DIV class and ID together in CSS? .PB { position: relative; ...

Preventing the selection of 'None selected' upon dropdown change

When using the NameTextBox Dropdown, all names are available for selection. Upon changing a name and clicking on search, the associated details are retrieved. However, by default, 'None Selected' is displayed. Is there a way to prevent 'None ...

Merging columns in Bootstrap 4 grid system

I have been working on creating a Bootstrap grid with the following structure: .facevalue { background: #ffffff61; font-family: Dubai; font-size: 18px; font-weight: 400; line-height: 30px; padding: 0 30px; border: 1px solid #e9e9e9; ma ...

Ensure that the child div with a background color extends all the way to the bottom of its parent div

Having trouble getting two divs inside another one to stretch to the bottom without cutting off content? The goal is for both divs to adjust in height based on whichever one is longer, filling the outer div completely. When the left div (menu) is longer t ...

How to truncate text in a cell of a Vuetify data table using CSS styling

Is there a way to truncate text in a cell using CSS without it wrapping around or overflowing? The ideal CSS code should include: .truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } However, the current implementation caus ...

Revamp Button content in HTML and Angular 2

My goal is to dynamically change the button text once it is selected. Initially, the YES button is highlighted and slightly changes upon being clicked. I want the button to switch to NO when clicked, and if hovered over, a disable symbol should be displaye ...

Positioning an HTML div

Currently, I am diving into the world of HTML and have included a relative div in my code. Although there are two additional divs positioned before it, they do not overlap; unfortunately, the first two still occupy space above the relative one. My desire ...

Display all elements on a webpage using Javascript without the need for scrolling

I'm looking for a way to ensure all items on a webpage load immediately, without having to scroll down multiple times before running a script to find a specific item. Is there a method to have all items load at once without the need to manually scroll ...

Dynamic Image Grid Created Using JavaScript Glitches

My dilemma involves using JQuery to create an HTML grid of images styled with Flex Boxes. The setup works perfectly on desktop, but when viewing it on mobile devices, the images begin to act strangely - jumping, overlapping, and even repeating themselves i ...

Tips for ensuring responsive images do not cause blocks to jump down

How can we address the issue of blocks jumping after an image loads while still maintaining responsiveness? Codesandbox. Key points to consider: The image should resize when the window is resized. The maximum width of the image is its own width. We do n ...

How to access bespoke attributes in Scene Builder using JavaFX?

I am on the hunt for a way to designate customizable properties for my custom controls. Jens Deters has already created some fantastic custom controls utilizing fontawesomefx for JavaFX. Once you import the jar files into Scene Builder, you can easily inc ...

The Bootstrap Navbar is displaying incorrectly and not adjusting properly in various viewports

To create a page header with a unique design, I am looking to incorporate a banner image with 4 cross lines along with the logo. Image Link The goal is to align text on both the dark blue and light blue bars with the logo on the left side of the image. ...

What is the best way to trigger the Save as ..dialog by clicking on an image?

Below is the code snippet provided. When the link is clicked, a save as dialog should pop up. <a href="http://www.experts-exchange.com/xp/images/newNavLogo.png" target="_new"> <img src="http://www.experts-exchange.com/xp/images/newNavLogo.png" a ...

The Cyrillic text displays uniquely across three different web browsers

On our company's website, I implemented a search function. You can input the minimum and maximum cc of the glass you're looking for, and it will filter the database to display relevant results. The issue I'm facing is that when viewing the r ...