How to write a CSS Selector selecting elements NOT having a certain attribute?
I have 2 <div>
nodes as follows:
First:
<div class="weEq5" style="will-change; width;">
<button class="_35EW6">
Second:
<div class="weEq5">
<button class="_35EW6">
I need to select the <div>
(with the similar class) and each of them which have a similar descending <button>
but without the style
attribute.
XPath seems working fine as:
//div[@class and not (@style)]/button
I am looking for an equivalent CssSelector.
Trials:
div[class :not(style)]>button (doesn't works).
I have been through the following discussion but they seem to be discarding the class attribute as :not([class])
as in:
I was looking in similar lines ending with :not(attribute)
.
To select element that does not have specific class with JavaScript, we can use the document. querySelector method with the :not pseudoclass. const li = document. querySelector("li:not(.
In CSS, to exclude a particular class, we can use the pseudo-class :not selector also known as negation pseudo-class or not selector. This selector is used to set the style to every element that is not the specified by given selector. Since it is used to prevent a specific items from list of selected items.
The [attribute|="value"] selector is used to select elements with the specified attribute, whose value can be exactly the specified value, or the specified value followed by a hyphen (-). Note: The value has to be a whole word, either alone, like class="top", or followed by a hyphen( - ), like class="top-text".
I think more accurate CSS Selector is:
div[class]:not([style])>button
because the button
element is a child of div
element.
Hope it helps you!
That's the code you're looking for:
div:not([style]) button{
background-color: red;
}
Now let's break it down. We have have four selectors in this example:
.weEq5
.The combination of div:not([style])
means that we want all divs that do not have a style attribute. After which we have a space and a button means that we want all the buttons that are inside the above selector.
Adding a >
before the button div:not([style]) > button
will only select button elements which are direct children of the selected div. It will exclude from selection buttons that are deeper inside the div.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With