Css :not for children elements

I have a code already working, but in a way not … too elegant.
I need not to apply a style to many elements, children of a div.
My present code is this:

html

<div [container]>
 <p><a class="someclass">sometext</a><p>
 <p><a class="someclass">sometext2</a><p>
 <p><a class="someclass">sometext3</a><p>
</div>

css

a:not(.someclass)
{
some css rules
}

How could I get the same result modifying not all the several a within that div [container], but modifying the div [container]?

Without a use case its hard to see what the problem is and I’m not quite sure what you are asking.:slight_smile:

Do you mean you don’t want to add those classes to the anchors?

Obviously you could target all a elements in that section quite easily.

.container a{etc...}

However if you wanted to exclude some anchors then they would need classes.

Perhaps if you could supply a more filled out demo the problem will become obvious :slight_smile:

You are right, excuse me.
What I want is not to apply to all anchor element in that div, a rule that should be applied to all other anchor of a webpage.
In other word a rule should be applied to all anchor element expect those in a section (within a given, particular, div).
For the real code you can see any page of my website https://www.culturanuova.net/ (in footer section and in normal.css [130 - 142 rows])

But, maybe the best thing would be not to settle a general rule and then create exceptions, but define a rule only for same divs (as is said here).

You can do that like this assuming that you have a class on the parent to use.

a:not(.token a) {background:red}

That will give a background to all anchors except the ones inside the .token div.

Generally its best not to style elements globally but use a class instead. There are a few things you can style globally but usually that would be margins and padding or simple reset styles.

Undoing styles is generally considered bad practice so keep tight control on your scope.

There are some new things in css such as @layer that will help but are rather complex for most general use cases.

1 Like

Indeed: I agree totally.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.