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

What steps should I take with my Android PWA manifest and service workers?

I created a web application that I want to prompt Android users to download when they visit. To build my service workers and manifest, I utilized PWA Builder which can be found at Now that I have the manifest file ready, should I simply upload it to the r ...

Updating dynamic content in IBM Worklight V6.1 with jQuery Mobile v1.4.3 requires waiting for the DOM to load

I need to dynamically update the content of a div within my HTML page structure. <!DOCTYPE HTML> <html> ... <body> <div data-role="page"> <div data-role="header"> //Image </div> <!-- Th ...

Ways to retrieve a JSON element and incorporate it into a CSS styling

Can someone assist me in converting a JSON object for use in CSS? I've attempted to do this using Vue.js, creating a map legend with v-for to generate the legend. However, if there is an alternative method that allows me to take a JSON object and ins ...

Save information in a session using html and javascript

I'm having trouble accessing a session variable in my javascript code. I attempted to retrieve it directly but ran into issues. As an alternative, I tried storing the value in a hidden HTML input element, but I am unsure of how to properly do that wit ...

Rotate each row of the table in sequence with a pause between each flip

I have a table with 3 columns and 10 rows. I would like to flip each row one by one, where each row contains data on both the front and back sides. The flipping animation should be similar to the example provided in this link, but the flipping should sta ...

Could you provide steps for verifying the functionality of the show password feature in selenium?

What is the best way to verify that the show password feature is functioning correctly? Which checkbox property should I inspect after clicking on the 'show password' checkbox to confirm that the entered text in the password field is visible? ...

Creating a bootstrap form field that spans 100% in width:

I am encountering an issue with two column divs in a row, each containing two text fields. When resizing on mobile, the width of the textbox is not expanding to 100% and looks unattractive. Below is my Bootstrap code, but strangely, the width looks fine f ...

Aligning the Twitter and Facebook buttons

Issue with Social Media Button Alignment I'm encountering a problem with aligning Twitter and Facebook buttons on my website. The alignment seems to be off and I need some help fixing it. Code Snippet html: <span id="social"> ...

Struggling with aligning rows containing three divs each in Rails and bootstrap

Looking to display 3 article divs in each row on my website using bootstrap 3 grid system: <div class="container-fluid"> <h1>NEWS</h1> <div class="row"> <% @articles.each do |article| %> <div class="col- ...

Incorporating a background-image to enhance the appearance of checkboxes

I am attempting to set up checkboxes that will display different images when checked and unchecked. Below are the checkboxes I am currently using: <input type="checkbox" name="fordCheckBox" value="Ford"> To assign background images to each checkbox ...

Customize the drawer background in Vue Material

Recently, I developed a vuejs application incorporating Google's material design components. I've been exploring options to customize the background color. <template> <div class="page-container"> <md-app> ...

Is there a way to ensure that GIFs in jQuery Mobile always start from the beginning?

My cross-platform mobile app built with jQuery Mobile is a small quiz application. After each correct or wrong answer, I display a 3-second GIF. Currently, I have set up the code to show the GIF for 3 seconds before moving on to the next page: else if ($. ...

Implementing div elements in a carousel of images

I've been working on an image slider that halts scrolling when the mouse hovers over it. However, I'd like to use div tags instead of image tags to create custom shapes within the slider using CSS. Does anyone have any advice on how to achieve th ...

Resizing the elements within an iframe: Troubleshooting problems with embedding Bandcamp players

My goal is to embed a Bandcamp music player on my WordPress blog page, but I'm facing a challenge. The maximum width of the current player is 700px and I need it to be 900px. After consulting with someone at Bandcamp, they directed me to the old versi ...

What is the best way to retrieve an element from an array within a script?

As a newbie in the realm of vue and Laravel Framework, I am eager to fetch data from an API and display it dynamically on my web page. To achieve this, I have already set up a table named 'Progress' where I seeded some initial data. Utilizing AP ...

What is the process for creating a selector that links to an element that has been dynamically generated?

Can anyone help me figure out how to create a selector that links to a dynamically created element without using an event on the dynamic element? By dynamically created element, I mean an element that is not present in the HTML at the beginning. I know t ...

Several DIVs with the same class can have varying CSS values

I am looking to modify the left-margin value of various separate DIVs using JavaScript. The challenge is: I only want to use a single className, and I want the margin to increase by 100px for each instance of the class. This way, instead of all the DIVs ...

When utilizing jQuery's .on() method, sometimes the data being passed to the handler may appear

I have three anchor tags that are dynamically added from an ajax $.get() response. Whenever one of them is clicked, I want to trigger a second request. The parameters for the second request should be set based on the global data attributes in these an ...

Is there a way to create automatic line breaks in this text?

Is there a way to automatically ensure the span spans multiple lines? I am working with HTML and CSS. Below is a modified version of my code, as some parts are in PHP: <div style='border: 1px solid; padding: 10px; margin-top: 5px;'> < ...

include a button next to the input field

As you reduce the browser window size, you may notice a different layout designed specifically for iPhone. One question that arises is how to add a button next to the search text box dynamically using JavaScript. The issue at hand is that the text box is b ...