Exclusive Orchard multi-level dropdown feature triggered only by clicking

My current project involves working with Orchard on a Bootstrap-based theme. The CSS code for the menu navigation is functioning properly when hovering over the second level menu.

.dropdown-menu .dropdown-menu {
  left: 100% !important;
  top: 0;
}

.dropdown .dropdown:hover > .dropdown-menu,
.dropdown .dropdown .dropdown:hover > .dropdown-menu,
.dropdown .dropdown .dropdown .dropdown:hover > .dropdown-menu {
  display: block;
}

.dropdown-menu i {
  line-height: 1.42857143;
}

I now want to modify the drop-down menu so that all levels open only on click, as this feature currently does not work on touch-based devices. When I change ':hover' to ':focus', everything closes when the second-level 'nav-bar' is clicked. I am not very experienced in HTML/CSS coding, so I am uncertain about how this operates. I assume 'dropdown' is the name of the 'ul' class and 'dropdown-menu' signifies the 'div' where the menu is located, but since all the code is generated automatically, I may be mistaken. Additionally, I apologize for any language barriers. Thank you.

Answer №1

In this specific example, it is evident that :focus does not function as expected due to the limitations of CSS3. Unlike :hover, which allows for bubbling, :focus only targets one element and lacks memory.

To address this issue, a workaround involving radio buttons with the :checked selector and adjacent combinator (A + B) can be implemented.

For reference, check out: http://codepen.io/HerrSerker/pen/obWBVG

The approach involves hiding the radio buttons and leveraging labels for functionality:

nav ul label {
  display: block;
}
nav ul > li > label > input:checked + ul {
  display: block;
}
nav ul.lvl-2 {
  display: none;
}
<nav>
  <ul class="lvl-1">
    <li>
      <label for="id-lvl1-01">
        <input type="radio" name="lvl-1" id="id-lvl1-01" />1
        <ul class="lvl-2">
          <li>
            <label for="id-lvl2-01-01">
              <input type="radio" name="lvl-1-1" id="id-lvl2-01-01" />01</label>
          </li>
          <li>
            <label for="id-lvl2-01-02">
              <input type="radio" name="lvl-1-1" id="id-lvl2-01-02" />02</label>
          </li>
        </ul>
      </label>
    </li>
    <li>
      <label for="id-lvl1-02">
        <input type="radio" name="lvl-1" id="id-lvl1-02" />2
        <ul class="lvl-2">
          <li>
            <label for="id-lvl2-02-01">
              <input type="radio" name="lvl-1-2" id="id-lvl2-02-01" />01</label>
          </li>
          <li>
            <label for="id-lvl2-02-02">
              <input type="radio" name="lvl-1-2" id="id-lvl2-02-02" />02</label>
          </li>
        </ul>
      </label>
    </li>
  </ul>
</nav>

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

Eliminate any empty spaces between the HTML tags

Is there a way to remove spaces between characters inside HTML tags? For example <option value="0" title="Advertising / Promotional Charges">Advertising / Promotional Charges</option> I tried using this code .replace(/\>\s+&bs ...

How to integrate half-star ratings in Ionic

Recently, I have been struggling with implementing half stars in the rating module of my Ionic mobile application. When the average rating is not a whole number, I opted to round it off and display an Ionic icon star. Below are snippets of the code I curre ...

Initialization of webix combo options values in HTML code

Currently, I am working with the Webix UI framework and I am trying to achieve something similar to: <div view="combo" options="fruit"></div> This is what I have in my JavaScript file: $scope.fruit =[{id:1, value: "Apple"}, {id:2, value: "Ba ...

What is the best way to prevent my form from saving empty values to the database?

I am currently working on a project for managing library resources using HTML5, PHP, and SQL. I have implemented form validation using PHP for fields such as email, password, name, surname, and phone number. The validation seems to work visually, but it st ...

Create a React MUI component that allows menu items to become sticky when selected

Is there a way to make the mui MenuItem stay sticky to Select while scrolling down? To see the issue in action, check out this codesandbox example https://codesandbox.io/s/quirky-knuth-5hr2dg?file=/Demo.tsx Simply click on the select and start scrolling ...

Cannot adjust Span content with JQuery

I am currently utilizing Bootstrap and JQuery for my project. HTML <div> <ul> <li><strong> Status : </strong><span id="monitorStatusSpan">1111</span></li> </ul> </div&g ...

Tips for adjusting the maximum height of the column panel within the DataGrid element

I'm trying to adjust the max-height of a column panel that opens when clicking the "Columns" button in the Toolbar component used in the DataGrid. I am working with an older version of @material-ui/data-grid: "^4.0.0-alpha.8". https://i.stack.imgur.c ...

Issue with Async pipe when utilizing autocomplete functionality

HTML Code <mat-form-field> <input type="text" matInput class="formControl" [formControl]="name" [matAutocomplete]="auto" > <mat-autocomplete #auto="matAutocomplete"> <mat-option *ngFor="let option of city | async" [valu ...

Determine the position of an object within an array using jQuery or JavaScript

Here is an example of HTML code: <ul class="paginering"> <li><a href="javascript:void(0)">Previous</a></li> <li class="active"><a onclick="changeDisplay(this)" href="javascript:void(0)">1</a></li> & ...

What is the process for including id parameters within the URL of an HTML page?

As I work on building a website with ReactJS and webpack, I encounter the need to access URL parameters. One specific challenge I face is creating a universal blog post view page that can be dynamically loaded based on the blog id parameter in the URL. Rat ...

Is it a wise choice to implement frames in AngularJS?

I am in the process of developing an AngularJS application with a three-panel main view, resembling a mail system with a navigation bar on the left. The right pane is divided into two sections; selecting an item from the navigation bar populates the top-ri ...

Tips for organizing components in jQuery Mobile

Displaying a survey creation form: <!-- HTML structure --> <div data-role="page" data-theme="b"> <div data-role="header"> <h1> Create Survey </h1> </div> <div id="main" data ...

Transfer data from a selection list in an HTML document to a Google Apps Script function and then dynamically update another selection

I am using a Google Web App to fetch text data from a spreadsheet based on dropdown selections. I want the options in the "Grade" dropdown list to be dynamic, changing according to the selection made in the "Role" dropdown menu. My approach involves using ...

How can CSS be used to prevent line breaks between elements in a web page?

div#banner>img { float: left; background-color: #4b634b; height: 265px; width: 80%; } ul { float: left; background-color: #769976; width: 162px; } <body> <div id="wrapper"> <header> <nav> <ul ...

Using the 'get' command to read a text file locally in JavaScript

I am struggling to use the get command in JavaScript to open a text file and populate a div tag in my HTML. The text file is located in the same folder as my html file, but I can't seem to make any progress with this task. When I click on the button I ...

Discovering the Javascript Code Executing on a <span> element with the Help of Chrome Inspector or Firebug

I have encountered a situation where a website is dynamically generating information into <span></span> tags using jQuery. The span has a class of "Price" and an id corresponding to a Product Code, with the data being pulled from a JSON data sh ...

What steps are involved in setting up a dropdown side navbar?

Is there a way to automatically activate the next dropdown in the side navbar when I click on the 'Next' button? And similarly, can we open the previous dropdown in the side navbar when clicking the 'Previous' button? ...

align a pair of HTML elements

One of the features on my website is a text area where users can input text. <textarea id="realAreaWhereUserTypes"></textarea> In addition to the text area, there is also a hidden input field like this: <input id="hiddenUserInput" /> ...

Is there a way to deactivate a specific JQuery function with a click of a button, while still having it enabled by default?

I am developing a video player that automatically redirects to another page once the current video finishes playing. I am looking to implement a feature that allows users to disable this automatic redirection by clicking on a 'Turn Auto Play Off Butt ...

Having trouble getting the jquery tabify plugin to work properly

I've put in a lot of effort and double-checked everything, but for some reason this tabify function just won't work. I have gone through my code 100 times but still can't seem to pinpoint the error. Here is the code I am working with: <! ...