What is the best way to define a style without affecting classes and their descendants?

I am facing an issue with styling a div on my webpage. I want to write some less code to style divs with a specific class and all their children.

Below is the simplified HTML:

<div class='people'>
    <div>
        <!--This text should be green-->
        Bob
    </div>
    <div>
        <!--This text should be green-->
        Geoff
    </div>
    <div class="ex-members">
        <div>
            <!--This text should remain red--> 
            Mary
        </div>
        <div>
            <!--This text should remain red--> 
            Fred
        </div>
    </div>
</div>

This is the less code I have been trying to implement:

body {
   color:red;
}
.people {
   &:not(.ex-members) {
      color:green;
   }
}

However, it seems that this code is setting all names to green instead of excluding the ones with the 'ex-members' class.

Note that I could solve this by just overwriting the green color and setting 'ex-members' back to black, but I am looking for a solution without using this method if possible.

Answer №1

To highlight specific elements, you can employ the direct descendant selector >

.people {
   > :not(.ex-members){
      color: green;
   }
}

This code will produce...

body {
  color: red;
}

.people> :not(.ex-members) {
  color: green;
}
<div class='people'>
  <div>
    <!--This text should display in green-->
    Bob
  </div>
  <div>
    <!--This text should display in green-->
    Geoff
  </div>
  <div class="ex-members">
    <div>
      <!--This text should remain in red-->
      Mary
    </div>
    <div>
      <!--This text should remain in red-->
      Fred
    </div>
  </div>
</div>

Answer №2

The former-members section is nested within the population division, while your additional style is targeting components with a population category but without an ex-members classification. To resolve this issue, it would be best to eliminate the &:not part.

.ex-members {
  color:green;
}

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

I'm having trouble getting my paragraph to center with both Bootstrap 4's margin:auto and text

I've been trying to center a paragraph in my project but it's not working out. I attempted using margin:0px auto; and the text-center features, but they didn't have the desired effect. <div class="row text-center"> <p> You c ...

Set the height of the div element to be the same as its parent element (100

Within my main div, there are two child divs. I want the first child div to have the same height as the parent div, without specifying a height in CSS. For reference, take a look at this example: http://jsfiddle.net/x8dhnh4L/ The pink div should expand to ...

When the window is resized, the div shifts position

I recently added a div to my website with the following code: <div id="div"></div> #div { height: 400px; width: 300px; position: absolute; right: 59%; text-align: left; } Howe ...

Animate the text before it fades away

Could someone help me figure out how to make my text animations work correctly? I want to show and hide specific text with animation when buttons are pressed, but I'm having trouble implementing the hiding animation. Any guidance would be appreciated. ...

Optimizing CSS table styles for larger cell width

In my CSS file, I have defined the following style: .ES{ max-width:20px; word-wrap:break-word; } However, when I apply this to my table... <table border="1" cellpadding="2" class="ES" > <tr> <td><input name="rbeso" type="radio" ...

Revamping the purpose of a function found within an HTML <script> tag

index.html: <script id="change"> function methods(){ return 1; } </script> js.js: ... button.addEventListener("click", ()=>{ document.querySelector("#change").innerHTML = ` function ...

An issue in HTML where only one div is displayed at a time when hovering over another div

I have a section in HTML that is divided into two parts. In one section, I am listing some points and in the other, I am displaying their corresponding sentence on hover. Here is how I implemented it: .showme { display: none; } .single-fun-fact:hover~. ...

Recording Node pathways that begin with the hash symbol #

How can I configure my Node/Express routing to intercept a route like /#/about and send it to /about Is there a way for node to handle routes that start with "/#"? It appears to only cut off at the hash mark. ...

How to Use Jquery to Select a Checkbox within a Table

Within my project, I have a table setup like this: <table id="filter-table" class="table table-striped"> <thead> <tr> <td>Accessory</td> <td>Typology</td> <td>Check</td> < ...

The HTML select element - Styling the selected item using the option font

Currently, I am working on adding a font dropdown list to a web page. Each option in the dropdown list is styled with the corresponding font it represents like this:- <option value="Arial, Helvetica, sans-serif" style="font-family: Arial,Helvetica,sans ...

Get rid of the blue outline border when embedding Google Maps

Does anyone know how to remove the blue focus border that appears when clicking on an embedded Google Map? I've tried various CSS styles without success: *:focus { outline: none !important; border: none !important; } iframe { outline: no ...

Using CSS to display several images across the entire screen width

I need to display multiple images in a row that fill the screen width: <img src="1.jpg" style="max-width:25%"> <img src="2.jpg" style="max-width:25%"> <img src="3.jpg" style="max-width:25%"> <img src="4.jpg" style="max-width:25%"> ...

Can you tell me if the "dom model" concept belongs to the realm of HTML or JavaScript?

Is the ability to use "document.X" in JavaScript to visit an HTML page and all its tags defined by the HTML protocol or the ECMAScript protocol? Or is it simply a choice made in the implementation of JavaScript, resulting in slight differences in every bro ...

A single feature designed to handle various elements

I currently have this HTML code repeated multiple times on my webpage: <form class="new_comment"> <input accept="image/png,image/gif,image/jpeg" id="file" class="file_comment" key=comment_id type="file" name="comment[media]"> <spa ...

Seeking to implement a feature where Javascript adds input from a form into an HTML table in real-time?

Attempting to achieve this goal involves creating an array that holds an object with instance variables related to form input. After that, the next step is to add whatever was submitted from the form to the table. However, upon calling the function to acco ...

What is the best way to position a Button at the bottom of a div using jQuery?

Within my HTML, I have divided into two col-md-6 sections. On the left side, there is a picture and on the right side, there is text. I am considering using jQuery .height() for this layout. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7 ...

Issue with Javascript code - function does not provide a return value

I have a JavaScript function that checks for a value in a text box. If the text box is not blank, it outputs a statement. The text box takes a numeric value, and I want to include that numeric value in the outputted HTML. Here is the HTML code: <br> ...

instructions on incorporating a sidebar into a .NET web page utilizing a master page that lacks a sidebar

I am facing a dilemma regarding adding a sidebar to one specific page of my .net project. The master template does not include a sidebar, so I'm wondering how I can achieve this on just that one page? Here is the code snippet for the Master Template. ...

Center 3 elements horizontally using Flexbox, ensuring the center element is aligned properly

Is it possible to center 3 elements using flexbox on a page? The middle element should be centered on the page, while the left and right elements can vary in width. Please refer to the image below: https://i.sstatic.net/IVdz8.png I am not certain if this ...

Symbol adjacent to button with multiple lines

My objective is to design a button with multi-line text and an icon positioned centrally next to it for both lines. Refer to the screenshot below for clarification: https://i.sstatic.net/ODPI2.png Despite my efforts, I am encountering difficulty in cente ...