Utilize CSS to selectively target specific sections of a repetitive pattern within a list

I have come across a list fragment that is generated automatically and I only have the ability to adjust the CSS for it.

<ul class="menu">
    <li class=" menuItem1 first parent"></li>
    <li class=" menuItem2 parent"></li>
    <li class=" menuItem3 parent"></li>
    <li class=" menuItem4 parent selected"> 
        <ul class="nccUlMenuSub1">
            <li class=" menuItem1 first parent"></li>
            <li class=" menuItem2"></li>
            <li class=" menuItem3 selected parent">
                <ul class="nccUlMenuSub2">
                    <li class=" menuItem1 first"></li>
                    <li class=" menuItem2"></li>
                    <li class=" menuItem3"></li>
                    <li class="menuItem4 last"></li>
                    <!-- Although this structure continues, I prefer not to display it-->
                </ul>
    ... and so on

My goal is to utilize a CSS rule of display: none to hide everything except the specific part of the list shown in the above snippet:

            <li class=" menuItem3 selected parent">
                <ul class="nccUlMenuSub2">
                    <li class=" menuItem1 first"></li>
                    <li class=" menuItem2"></li>
                    <li class=" menuItem3"></li>
                    <li class="menuItem4 last"></li>
                    <!-- Although this structure continues, I prefer not to display it-->
                </ul>

Currently, the CSS code I've developed only hides all content after the first line in my second example.

.menu .selected ul.nccUlMenuSub1 li[class*="menuItem"]:not(.selected) {
    display: none;
}

How can I adapt my CSS to include both the selected element and two levels of its children (specifically .nccUlMenuSub2, along with their immediate <li> elements)?

Answer №1

Keep only the targeted elements visible with this CSS snippet:

.menu > li:not(.selected),
.menu > li.selected > ul > li:not(.selected),
.menu > li.selected > ul > li.selected > ul:not(.nccUlMenuSub2),
.menu > li.selected > ul > li.selected > ul.nccUlMenuSub2 ul
{
    display: none;
}

This code will hide everything except for the specific ul.nccU1MenuSub2 tier and its direct child lis. Submenus beyond that level will also be hidden, along with any siblings of the ul and its parent's siblings.

JSFiddle Demo

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

Tips for storing images in a database using PHP

After writing image upload files, I encountered an error stating "Undefined index: image in C:\xampp\htdocs\Schmgt\svr\upload_image.php on line 3". Despite thoroughly reviewing my code, I am unable to find any issues. Any assistanc ...

Utilize WebGL to incorporate a mesh as a background image mask

Currently, I am faced with a challenge in WebGL that involves the following scenario: Picture a mesh positioned in front of the camera. This mesh does not have traditional shading, but its outline, or "silhouette," is utilized to display a background imag ...

Issue with the width on Mobile Safari

Encountering a strange problem with mobile Safari on iPads and iPhones where the body and some divs are getting cut off in the middle. Despite trying various solutions, I can't seem to fix it. The website works perfectly fine on desktop but not on mob ...

Website errors appear on the hosted page in <body> section without being present in the code

Hello there, I have set up a debug website using my "Olimex ESP-32 POE" to send internal data via JSON, eliminating the need for Serial Output from the Arduino IDE (the reasons behind this are not relevant). #include "Arduino.h" #include <WiF ...

Position the right div to float to the right with a dynamic width ahead of the left div in HTML to achieve

This particular challenge has proven to be quite difficult for me, and I have a feeling that I am overlooking something simple. My goal is to create a layout with a fixed-width left sidebar and a responsive variable-width content section on the right. Th ...

Transfer data from button to input field

Trying to create a simple calculator with PHP to submit to my teacher, I have the basic HTML and PHP codes in the same file. However, I am struggling to figure out how to pass the number from a button to the text input in order to create a larger number, ...

Try utilizing the adjacency selector in Less again

Is it feasible to repeat this pattern an unlimited number of times using Less with the provided CSS selectors? I've been looking into using loops, however, I haven't come across any examples that achieve a similar result. .page-section { ba ...

Leveraging Ajax XHR2 for uploading files and accessing values in PHP

I recently followed a fantastic tutorial online on how to use AJAX to upload a file in an HTML form. Here is the code snippet I used: HTML <input type="file" id="myFile" name="myFile" /> <input type="button" id="upload" value="upload" /> < ...

React - Material UI Divider not centered

Is the title says - my divider is not centered in the middle of my text. The current result displays a misalignment: However, I am aiming for my divider to be placed in the center like this (refer to red line): It's quite strange because according t ...

Why is the size of my array shrinking with every iteration of the for-loop in JavaScript?

I am struggling to change the classname of three elements that share the same classname. Unfortunately, as I loop through my array, it seems to decrease in size with each iteration, preventing me from successfully changing all three elements. Any advice or ...

Click on link after animation has finished

I'm currently facing an issue with my script not functioning correctly. The code I'm utilizing is from a resource provided by Codyhouse to implement a 3D rotating navigation menu on my webpage. Essentially, when you click the hamburger icon, it o ...

What steps can be taken to confirm that a comment posted on a specific post is genuine and related to that post?

Currently, I am immersed in the world of AJAX, PHP, and MySQL. Below is a snippet of my PHP code: if(isset($_GET['postTextComment'])){ // The text of the comment $htpc = htmlspecialchars($_GET['postTextComment']); // ID of ...

Conceal a div element using a button through JavaScript

I've been scouring various online resources for solutions, including Stack Overflow and W3Schools, but I haven't had any luck so far. Here's a simplified version of my current code: <script language="javascript"> function remove() ...

Using jQuery to show/hide linked CSS3 animations upon Mouseenter/Mouseleave events

Exploring the capabilities of animate.css and jQuery within a bootstrap environment has been quite interesting for me. However, I've encountered a major issue! I am attempting to trigger animations on mouseenter / mouseleave, which led me to create a ...

What is the reasoning behind the "open in a new tab" function triggering a GET request?

Check out this HTML tag: <a href="#" id="navBar_navBarInput_3_subNavDropdownInput_0_subNavLinkInput_0" onclick="redirectPost(4,'EntryData.aspx');">My Cool Link</a> The Javascript function "redirectPost" function redirectPost(id, ur ...

What is the best way to enable duplicate entries in ng-repeat?

When utilizing stringify, the json data gets formatted and appears in an alert message. However, when attempting to display it in an actual link, it is not showing up. Upon checking the console, an error was displayed stating that "duplicates are not allow ...

Setting the minimum width for a grid item in a React Material UI component

Is there a way to ensure that a material-ui Grid item has a minimum width of 250 pixels? I've tried using the sizing suggestions from Material UI's documentation, but haven't had any success. The 'logo' element within the Grid cont ...

How can I stop full-page auto-scroll screenshot extensions from automatically scrolling?

As the owner of a membership website, I'm faced with the challenge of preventing users from easily taking full page screenshots of the valuable text content in my members area. Many browser extensions, such as Fireshot and GoFullPage, enable auto-scro ...

The fascinating CSS Circle interaction on mouse click or hover

I am encountering a challenge with my circular element: Here is the HTML code for it: <div id="quadrant-sizer"></div> And here is the CSS styling: #quadrant-sizer { width: 40px; height: 40px; border-radius: 999px; } The issue I ...

Tips for deleting an HTML row and removing an entry from a MySQL database through the use of AJAX

Utilizing a mix of HTML, PHP, Javascript, and AJAX, I am creating an HTML table populated from a MySQL database. The goal is to implement a functionality where a row can be removed from both the table and the database using a delete button. Here's wha ...