The not() function only applies to immediate children and does not impact any nested

There is a very odd behavior with the "not()" css selector that I am experiencing.

Here is my simplified code snippet:

<div id="mapDiv" class="mapDiv mapDiv1">
    pippo
    <div class="gm-style">pluto</div>
</div>

<div id="mapDiv2" class="mapDiv mapDiv2">
    pippo
    <div class="gm-style">pluto</div>
</div>

And here is my CSS code:

.mapDiv1, .mapDiv2 {
    width: 300px;
    height: 100px;
    border: solid 1px red;
}

.mapDiv div {
    width: 200px;
    height: 50px;
    border: solid 1px blue;
}

:not(.mapDiv1) div {
    color: green;
}

You can also find a jsFiddle here.

I expected the text in the second box to be styled with green color due to the not() selector, however, it is being applied to both boxes. Can someone please explain why?

Answer №1

In my understanding, the :not() pseudo-class functions as a negation tool in CSS.

Essentially, it allows you to first select a group of elements and then exclude specific elements from that selection using the negation pseudo-class.

It is important to note that this should be used with a selector for proper functionality.

For example, if you were to update your CSS code to:

div:not(.specialDiv)
{
    background-color: blue;
}

This code snippet will target all divs except those with the 'specialDiv' class.

Furthermore, if you modify the code to:

div:not(.specialDiv) p
{
    color: red;
}

This updated code will style all paragraphs nested within divs, excluding those contained within divs with the 'specialDiv' class.

For further information, please refer to this resource

Answer №2

If you want to alter the appearance of your code, try this:

div:not(.mapDiv1)
{
 color:green;
}

I made changes to your example in this Fiddle

Answer №4

give this a shot

div:not(.mapDiv1) {
   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

Adjust the Height of a Div Following an Ajax Request

ISSUE: Currently, I have a dynamic form that utilizes JQuery steps to transform it into a wizard. At one of the steps, selecting an option from a drop-down menu triggers an AJAX call which adds additional form fields dynamically. The challenge lies in adj ...

Attempting to conceal image formatting when an image is not available - ACF images customization

When designing my Content Page Template, I added borders, backgrounds, and box shadows to style the images. However, if the optional image field is left empty, it still displays a small box with the css styling. I have been trying to figure out how to hide ...

The Video Element Is Not Expanding to Fill the Entire Parent Div

My goal is to design a responsive layout where a video element spans the full width of its parent div with the class .hero. However, despite my best efforts, the video doesn't seem to be expanding as expected. Here's the relevant CSS code I' ...

Conceal the column title for order items in the WooCommerce admin panel

I've been attempting to hide certain columns within the administrative section of Woocommerce, but have had no success with the code I'm using. I'm currently stuck and my question is: Is there a way for me to conceal specific column names i ...

Grails 3 - Resource Management Extension

Can anyone recommend a great Grails plugin for the latest version of Grails 3 (specifically working with 3.0.4)? I'm looking for something like this one () that allows me to create resource bundles for JS, CSS, and other dependencies. I think having ...

Is it possible to adjust the scroll top position using inline style in angularJS/CSS?

I am working on storing the scrollTop value of a vertical menu in my controller so that I can set it up each time I return to the page for persistent scrolling. Does anyone know how I can achieve this? This is the code snippet I am currently using: < ...

Place a written message on an picture

<head> <style> .relative{position:relative; width:600px;} .absolute-text{position:absolute; bottom:1; font-size:12px; font-family:"vedana"; background:rgba(251,251,251,0.5); padding:10px 20px; width:10%; text-align:center;} </style> < ...

Is there a way to ensure that the tab character " " remains consistent in size within a textarea at all times?

My current challenge involves inserting tabs of the same size into a textarea. The example shows that the only variation in each line of the testString is the placement of the \t. However, when displayed in the textarea, the tab sizes differ dependi ...

All you need to know about the properties of CSS ul

When I specify .a ul{} .a ul li{} as well as .b ul{} .b ul li{} will the properties of ul in .b be completely isolated from the properties of ul in .a? Or will they have some commonalities? I am looking to ensure that the styles for ul remain entirely ...

Start the CSS3 animation in reverse right away

I am trying to achieve a "flashing" effect by adding the .shown class to my #overlay, causing the opacity to fade in for 2 seconds and then immediately reverse, fading out for another 2 seconds. After experimenting with removing the class, I found that it ...

Display a pop-up alert following the completion of form validation using JavaScript

I am currently learning JavaScript and working on an enrollment form that requires validating a cellphone number with 11 characters. The issue I'm facing is that every time I hit submit, the page refreshes instead of displaying the validation message. ...

I'm looking to generate a semicircle progress bar using jQuery, any suggestions on how

Hi there! I'm looking to create a unique half circle design similar to the one showcased in this fiddle. Additionally, I want the progress bar to be displayed in a vibrant green color. I've recently started learning about Jquery and would apprec ...

CSS animations made easy - Using a single keyframes declaration to switch animation direction

Is it possible to animate a div to the right when a button is clicked, then animate back to the left with just one keyframes declaration by swapping classes? While attempting this in CodePen, I encountered an issue where the div snaps back after the first ...

Content overflowing beyond the boundaries of the parent DIV in Internet Explorer 6

Take a look at the code snippet below: <div style="width: 50px; border: 1px solid green;"> <div style="position: absolute;"> add whatever text you'd like </div> </div> The display in modern browsers (such as I ...

Exploring how to replicate background-size: cover with a center position using HTML5's <video> tag

Looking to replicate the effect of background-size: cover on a <video> element. I was able to achieve this with the code below: <video id="videobackground" autoplay="true" loop="true"> <source src="BackgroundFinal2.mp4" type="video/mp4" ...

Implementing an automatic link generation feature for files within a directory using JavaScript

I could really use some assistance with this. I created a YouTube example, which can be viewed in this PLNKR LINK: http://plnkr.co/edit/44EQKSjP3Gl566wczKL6?p=preview In my folder named embed, I have files titled p9zdCra9gCE and QrMOu4GU3uU, as shown belo ...

Tips for shifting a stuck div 200px upward once the bottom of the page is reached while scrolling:

I've set up a page layout with two columns, one fixed and the other scrollable. The left column is fixed (containing Google ads) and stays in place as I scroll down the page. The right column displays posts and scrolls along with the rest of the con ...

Make sure to align images and comments seamlessly on your website just like in the comment section of YouTube

Looking for help aligning an image and two spans in one row <img src="../images/profile/profile.png" class="img" /> <span class="name">First Name</span> <span class="comment"> This is simply dumm ...

React - the perfect solution for managing dynamic inline styles

When it comes to handling inline styles, there are two scenarios to consider. Which approach is the most effective for 'toggling' CSS properties based on the component state - or is there a better alternative? 1. The first method involves creati ...

Interactive canvas artwork featuring particles

Just came across this interesting demo at . Any ideas on how to draw PNG images on a canvas along with other objects? I know it's not a pressing issue, but I'm curious to learn more. ...