Switching the navigation drop down from hover to click: A step-by-step guide

I'm currently designing a responsive navigation bar with a dropdown feature that expands on hover. However, I've encountered an issue as this hover effect is not ideal for mobile devices. Therefore, I am looking to modify my navigation bar so that it expands on click instead of hover.

I attempted to implement the checkbox method but faced difficulties in getting it to function properly. Ideally, I would like to achieve this using only HTML and CSS if feasible. Any assistance on this matter would be greatly appreciated!

Take a look at the implementation here: JSFiddle

HTML:

<ul id="navigation">
<li><a href="#">Product 1</a>
<ul>
<li><a href="#">rx.com </a></li>
<li><a href="#">Resources</a></li>
<li><a href="#">Copay Cards</a></li>
</ul>
</li>
<li><a href="#">Product 2</a>
<ul>
<li><a href="#">e.com </a></li>
<li><a href="#">Resources</a></li>
<li><a href="#">Copay Cards</a></li>
<li><a href="#">Informational Video</a></li>
</ul>
</li>
... (remaining HTML code)

CSS:

/* Main Navigation */
#nav {
    position:relative;
    width:620px;
    margin:0 auto;
    margin-top:50px;
    padding:10px;
}

ul#navigation {
    margin:0px auto;
    position:relative;
    float:left;
}
... (remaining CSS code) 

Answer №1

Insert the following code snippet at the end of your CSS file or within a style tag.

.hide{
    display: none;
}
.show{
    display: block;
    opacity: 1;
}

Here is an example of how you can use these classes in your HTML file:

<li>
  <a id="prod1" href="#">Product 1</a> 
  <ul id="prod1list" class="hide"> 
    <li><a href="#">rx.com </a></li> 
    <li><a href="#">Resources</a></li> 
    <li><a href="#">Copay Cards</a></li> 
  </ul> 
</li>
<script>
    document.getElementById('prod1').addEventListener('click', function(e){
        e.preventDefault();
        document.getElementById('prod1list').classList.toggle('show');
    });
</script>

I have tested this implementation and it appears to be functioning 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

If the selected tab matches, collapse it using jQuery

When utilizing the jQuery accordion, I am looking to collapse if the currently active tab is selected. If I click on the same tab again, nothing happens. However, if I click on another tab, the activated tab changes accordingly. Check out an example on j ...

What is the reason for the unexpected JQuery attribute in my FORM element?

Using a combination of Approval Tests and WatiN to test the integration of ASP.NET MVC2 pages has been efficient. WatiN launches IE to access the provided URL and captures the browser's HTML response in a variable. Approval Tests then facilitate compa ...

Creating an HTML table from dynamic JSON data - A comprehensive guide

I am in need of a solution to transform JSON data into an HTML table format quickly and easily. Can anyone provide guidance on effectively utilizing PartialView to recursively render tables, ensuring that most details are visible? ...

How do I start using Google Analytics: application.js, ga.js, or a beginner’s guide?

Hello there! I was wondering if anyone could provide a comprehensive guide or step-by-step tutorial on setting up Google Analytics with some examples. The information I found so far only covers signing up and obtaining a tracking code, but it doesn't ...

What is the best URL structure to use?

I am seeking feedback on the best approach for structuring a website. My website allows companies to register and have their own page at company.site.com. I am trying to decide where the administration should take place for each company. Here are the two o ...

Intersecting Hyperlink Elements Creating a Hover Effect

I've encountered a perplexing CSS display issue and I'm at a loss for alternative solutions besides simply widening the width of the parent div. Allow me to explain the layout: <div> <ul> <li> <a href="javascrip ...

The picture is unavailable for inline viewing

I am currently working with bootstrap and I have a requirement to show an image followed by some text. However, despite having enough space after the image, the text keeps getting pushed to the next line. Below is my code: <div class="splash"> < ...

Explain the functionality of the Angular EventListener that is triggered on the scroll event of

Currently, I am exploring ways to track the position of the navbar beneath the landing page. The goal is for the navbar to become sticky at the top once it reaches that position until you scroll back up. Despite trying various solutions on Stack Overflow a ...

Transforming the appearance of a clickable link when tapped

Is there a simple way in Bootstrap 5 to change the style of a clicked link to an "active" style while reverting the previously clicked link back to normal? Or is this something that would require custom CSS/JS? <ul class="nav nav-pills flex-column& ...

What is the best method for choosing HTML "ids" that are generated automatically by DevExpress controls using JavaScript DOM manipulation?

How can I create a pop-up that displays unique information for each of the hundreds of html divs with a common class but individual ids generated by DevExpress Controls? I have a large number of automatically generated html div "ids" and I'm looking ...

Ways to add space around the td element and properly align the content within it

I've recently delved into the realm of web development, and I'm encountering some challenges with HTML alignment. Despite exploring various resources and attempting to utilize align="left"/right/center attributes, I'm still struggling to ach ...

JS | How can we make an element with style=visibility:hidden become visible?

HTML: <div id="msg-text"><p><b id="msg" name="msg" style="visibility:hidden; color:#3399ff;">This is a hidden message</b></p></div> JS: $('#url').on('change keyup paste', function() { $('# ...

CSS: Problem Arising from Line Connections Between Tree Elements

I'm currently working on connecting tree nodes with lines in a drawing. I've managed to achieve it to some extent, but there are a few issues like dangling lines that I need to fix. You can find the implementation on codepen here: https://codepe ...

The layout created with Tailwind did not meet our expectations in terms of the value obtained

Choose the shape you desire. Click here to view image The current shape looks like this... Click here to view image bar : h-14 nav : h-full content : h-full footer : h-14 I am trying to create a layout similar to this using tailwind CSS, but it's no ...

Changing the properties of the admin bar when it is hovered over in

After successfully styling a button on the admin bar, I noticed a few imperfections in the implementation. The button in question is situated on the admin bar and is labeled "maintenance". Upon clicking the button, jQuery triggers the addition of the clas ...

Mistake made by the author while using the Google Structured Data Test

While reviewing my website, I encountered an error message stating: "Missing required hCard "author"" http://www.google.com/webmasters/tools/richsnippets?q=http%3A%2F%2Fwww.gamempire.it%2Fcastlestorm-ps-vita%2Frecensione%2F131419 Why is this happening? I ...

Position the caret after adding a new element in a content-editable div

I have a contenteditable div that contains various elements. My goal is to automatically create a new paragraph tag right after the element where the cursor is positioned when the user presses the enter key. Currently, I am able to insert the paragraph tag ...

What is the best way to add a numbered alert to a button using HTML5, jQuery, PHP, or JavaScript?

I have a button on my HTML page that directs users to a page containing email features. I'm looking for a way to show users the number of unread messages they have before they click on the button to view them. I want this to be a subtle alert without ...

What is the best way to conceal a bootstrap directive tooltip in Vue.js for mobile users?

Currently, I'm tackling a project with Vuejs and Laravel. There's a tooltip directive incorporated that relies on Bootstrap's functionality. Check it out below: window.Vue.directive("tooltip", function(el, binding) { // console ...

Deactivate a specific button within a row on a Primeng table by clicking on it

I'm trying to modify my primeng table so that each row has a button, and when I click on a button, only that specific button should be disabled. However, in my current code, all buttons are getting disabled with a single click. Can someone please help ...