Select elements that do not have a specific parent class using LESS

I have created an HTML structure as outlined below:

<ul class="list list--sm">
    <li class="list__item list__item--draggable">
        List item 1
          <ul>
              <li class="list__item list__item--draggable">
                  List item 1.1
                    <ul>
                      <li class="list__item list__item--draggable">
                           List item 1.1.1
                      </li>
                    </ul>
              </li>
          </ul>
    </li>
</ul>

In this scenario, my goal is to target elements with the class list__item--draggable that do not have a parent or grandparent with the same class, specifically referring to the element displaying "List item 1". I intend to accomplish this using LESS without utilizing the > selector due to potential complexities in nested structures with additional divs and elements.

I am currently struggling to devise a solution for this dilemma. Is there a way to achieve this task?

Answer №1

With just CSS, you can achieve this:

.list__item--draggable:not(.list__item--draggable *) {}

Give it a shot:

.list__item--draggable {
  outline: 1px solid black;
}

.list__item--draggable:not(.list__item--draggable *) {
  outline: 1px solid red;
}
<ul class="list list--sm">
  <li class="list__item list__item--draggable">
    List item 1
    <ul>
      <li class="list__item list__item--draggable">
        List item 1.1
        <ul>
          <li class="list__item list__item--draggable">
            List item 1.1.1
          </li>
        </ul>
      </li>
    </ul>
  </li>
</ul>

Answer №2

If you're looking to restyle the children in a specific way, here's a straightforward CSS example that focuses on highlighting only the first text:

.list__item--draggable {
  background: yellow;
}

ul ul,
ul ul .list__item--draggable {
  background: white;
}
<ul class="list list--sm">
  <li class="list__item list__item--draggable">
    List item 1
    <ul>
      <li class="list__item list__item--draggable">
        List item 1.1
        <ul>
          <li class="list__item list__item--draggable">
            List item 1.1.1
          </li>
        </ul>
      </li>
    </ul>
  </li>
</ul>

In essence, we modify all occurrences and then revert those that are child elements.

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

Using Array.push within a promise chain can produce unexpected results

I have developed a method that is supposed to retrieve a list of devices connected to the network that the client has access to. export const connectedDevicesCore = (vpnId: string, vpnAuthToken: string) => Service.listVPNConnectionsCore ...

Secure an input field for exclusive attention. React

How can I lock the focus of my input field? I attempted using the following code: onBlur={this.click()} However, it was not successful. What is the correct way to accomplish this? ...

Populate Chart.js with a specific set of JSON data

As I delve into the world of JavaScript, I'm faced with a challenging issue that I need help resolving: I have a JSON file housing an array of data. What I aim to do is extract specific arrays from the month of August (Reports, Average, Global) and d ...

Select2.js Dropdown List with Case Sensitivity

Currently making use of select2.js version 4.0.3 Situation: Imagine the dropdown list contains the following options: JavaScript javascript Javascript javaScript So, when a user types in java, all options are displayed (case is n ...

A guide to monitoring and managing errors in the react-admin dataProvider

Rollbar has been successfully integrated into my react-admin app to track uncaught errors. However, I've noticed that errors thrown by the dataProvider are not being sent to Rollbar. It seems like errors from the dataProvider are internally handled w ...

The steps to display a partial view within another view in React Native

Attempting to show the View after calling alert("Hi") in the renderMoreView function has been challenging for me. The alert works fine, but displaying the View is where I am encountering issues. class Main extends Component { state = { moreButton: f ...

Connecting Checkboxes and Chips Together in Vue js (Bi-Directional Binding)

Hey there, I'm new to using Vue Js and I'm facing an issue with selecting a dropdown option from a form. The options are displayed as checkboxes within a div, and when a checkbox is selected, a chip should appear with the label of the checkbox. H ...

Is there a way to smoothly transition a FlatList while the keyboard is open?

At the moment, the appearance and animation of my messaging screen are as follows: However, I'm eager to enhance my messaging initial animation to lift up my flatlist like this (opens with scrolling): This is the code for the screen: // Code goes he ...

Sharing environment variables in gulpfile with other JavaScript files outside NODE_ENV

Is it possible to pass a variable other than NODE_ENV from the gulpfile.js to another javascript file? gulpfile.js // Not related to NODE_ENV! let isDevelopment = true; somejsfile.js /* I need to access "isDevelopment" from the gulpfile.js... For the ...

The functionality of cloning in jQuery may encounter an issue where the text field remains enabled if the user selects an option labeled "other

Currently, I am working on a jQuery clone with my existing code and everything is functioning well. In the first scenario, if the user selects other from the dropdown menu, the text field becomes enabled. In the second scenario, when the user clicks ...

Having trouble retrieving AJAX data [PHP]

When a form on my homepage (index.php) is submitted, it opens a randomly generated URL in a new tab. This random URL runs a script named run.php. <form action="/run.php" method="POST" target="_blank"> <input type="hidden" id="idgen" name="idg ...

Incorporating words into the core of a pie chart: Highcharts

Is there a way to display the total value in the center of a pie chart? I've attempted some methods but haven't been successful. The goal is to show the sum of all y values. Input export const data = [ { y: 15, name: "Category 1&q ...

value of object's property set to string "null" if null

I am facing an issue with my Angular app, where I am sending an object via a PUT request to my Express server. The request's content-type is multipart/form-data. The object structure is as follows: obj = { field1 : "foo", field2 : null } Upon ...

Retrieve the response status using a promise

There is a promise in my code that sometimes results in an error response (either 400 or 403, depending on the user). I am trying to handle this situation by catching the response and implementing a conditional logic to execute different functions based on ...

Redefining the onClick function in the Redux 'presentation' component to include parameters

How can I pass an object as an argument to a function in a redux "presentation component" efficiently? In my <BookList /> container component, I display a <BookListRow/> presentation component for each book. I want to add a button in each Boo ...

I am experiencing issues with my bootstrap file not working despite providing the correct file path. The styles are not being applied on my webpage

The file path is provided below <link rel="stylesheet" href="../css/bootstrap.min.css"> I have exhausted all options in an attempt to resolve the issue. The browser console indicates a 404 error (err_abborted) ...

Having trouble including a YouTube iframe code within the document ready function

I am having trouble getting the youtube iframe API code to work properly within my $(document).ready() function. When I try to add the code inside the function, the player does not load. However, when I move the code outside of the document.ready, the play ...

Tally each div individually and display the count within each div, instead of showing the total count across

I have come across various solutions that show the total number of certain special divs, such as: $('.someclass').length However, I am facing a different challenge. I want to sequentially count each div with a numerical sequence. For instance, ...

Issue with Nivo Lightbox: image not properly centered or resizing correctly

Whenever an image is clicked, it opens in the Nivo lightbox. This feature used to work flawlessly, but recently, it seems to be experiencing issues. The image should be centered and resize responsively to stay in the center. (For example, it worked correct ...

Limit User Input in Android Using JavaScript

Looking for a way to stop users from typing in an input field on Android devices. I've tried multiple solutions but none have worked so far. Potential Solutions Attempted: Preventdefault MaxLength Input maxlength does not work on Android -Ionic Is ...