:not() selector not behaving the same between Safari and Chrome/Firefox
Safari recently shipped the level 4 version of :not()
, which allows complex selectors for arguments, putting it on par with jQuery's hitherto non-standard implementation. See the release notes. The current incarnation of :not()
only allows a single simple selector for an argument, so a complex selector like p div
will not work in today's browsers by design.
A complex selector is an expression consisting of one or more compound selectors separated by combinators such as descendant, >
, ~
and +
. A compound selector is a sequence of one or more simple selectors. div
is a compound selector consisting of one simple selector, and p div
is a complex selector consisting of two compound selectors (each of which consists of one simple selector), separated by a descendant combinator.
It is currently not known when this will land in the other browsers, though it's unlikely the new specification of :not()
will change at this point — the current level 4 definition is a no-brainer and if the original WebKit strain is daring enough to implement it, then it's really only a matter of time before it makes its way into the other strains (including Blink).
After almost five grueling years of waiting since the FPWD, we might actually finally get to see a CR of selectors-4 real soon. Consider me pumped.
Two classes in :not() doesn't seem to work in Safari, but it works in Firefox and Chrome
Are you sure .column:not(.custom.no-edit)
worked in chrome and FF but not safari ?
That is a CSS level 4 syntax. However Safari is known to support it.
Let's take a look at the spec.
CSS level 3
The The negation pseudo-class, :not(X), is a functional notation taking a simple selector (excluding the negation pseudo-class itself) as an argument. It represents an element that is not represented by its argument.
A simple selector is either a type selector, universal selector, attribute selector, class selector, ID selector, or pseudo-class.
Something that is not mentioned in the spec, :not() does not
accept compound Selectors
A compound selector is a sequence of simple selectors that are not separated by a combinator, and represents a set of simultaneous conditions on a single element.
Basically chaining classes and types etc.., Example:
.class1.class2.class3
#myId.MyClass
div.MyClass#yourId
CSS level 4
The The negation pseudo-class, :not(), is a functional pseudo-class taking a selector list as an argument. It represents an element that is not represented by its argument.
In CSS level 4 it will become free for all.
Is div:not(.a):not(.b)
equal to div:not(.a.b)
div { display: inline-block; width: 80px; height: 80px; border: 4px solid black;}
div:not(.a.b) { background-color: green;}
<h3>div { ... }: select all divs</h3><h3>.a.b { ... }: select all elements with both classes .a and.b </h3>
<h3>If we combine the two using :not() The first two boxes should be green</h3>
<div class="a"></div><div class="b"></div><div class="a b"></div>
Advanced element selector with element prefix does not work in Chrome
:not()
only takes a "simple" selector according to the spec.
You can use the classic CSS approach of giving two rules, with the second overriding the first if applicable:
div[class*="element-"] + div[class*="element-"] {
margin-left: 1%;
}
div[class*="element-"] + div[class*="element-"][class*="element-offset-"]) {
margin-left: 0;
}
CSS not working in Safari, but in Chrome and other browsers it does
You need to remove background-attachment : fixed
not supported on the safari , check it here Can I use , last parameter of background css key is an attachment
CSS3 :not negation pseudo-class not fully supported by Firefox?
In CSS the comma (,
) separates selectors. It's not a selector itself so it can't be used inside a selector. So depending of if you want to apply the rule to
- paragraphs that are not
.class1
and paragraphs that are not.class2
, - paragraphs that have neither
.class1
norclass2
or paragraphs that don't have.class1
and.class2
it's
p:not(.class1), p:not(.class2) {
}
or
p:not(.class1):not(.class2) {
}
or
p:not(.class1.class2) {
}
BTW, IMHO it's better to avoid :not
if possible and in this case, for example, have a general rule that applies to all p
s (with the properties you want to set in the :not
rule) and one that applies to ones with the class and overrides the properties of the first rule if necessary.
Is it ok to use the CSS3 selector :not()
The one that is safari only says
Selectors Level 4 allows the
:not()
pseudo-class to accept a list of
selectors, which the element must not match any of. Selectors Level 3
only allowed:not()
to accept a single simple selector. Thus,
:not(a):not(.b):not([c])
can instead be written as:not(a, .b, [c])
Which means this form :not(a, .b, [c])
can be used oppose to this form :not(a):not(.b):not([c])
but only within safari
http://caniuse.com/#feat=css-sel3 tells us
Advanced element selection using selectors including:
[foo^="bar"]
,
[foo$="bar"]
, [foo*="bar"]
,:root
,:nth-child()
,:nth-last-child()
,
nth-of-type
,nth-last-of-type()
,:last-child
,:first-of-type
,
:last-of-type
,:only-child
,:only-of-type
,:empty
,:target
,:enabled
,
:disabled
,:checked
,:not()
,~
(general sibling)
Which means we can use all of those selectors including :not()
in any browser, well all major and updated browsers.
hope that clears it up.
Does :not() negation accept descendant selectors?
In Selectors Level 3, the answer would be NO. The :not()
notation accepts only simple selectors.
6.6.7. The negation
pseudo-classThe negation pseudo-class,
:not(X)
, is a functional notation taking
a simple selector (excluding the negation pseudo-class itself) as an
argument. It represents an element that is not represented by its
argument.
What is a simple selector?
From selector syntax:
A simple selector is either a type selector, universal selector, attribute selector, class selector, ID selector, or pseudo-class.
Nothing about a descendant selector.
HOWEVER, in Selectors Level 4, :not()
accepts complex selectors, which would include descendant combinators. Browser support is still quite weak for this specification.
Related Topics
How to Change the Bootstrap Primary Color
"Text-Align: Justify;" Inline-Block Elements Properly
Specify Multiple Attribute Selectors in Css
How to Make CSS Visible Only For Opera
Full-Screen Responsive Background Image
Difference Between Background and Background-Color
Outlining and Partially Filling an Svg Shape
Background Image, Linear Gradient Jagged Edged Result Needs to Be Smooth Edged
Set Google Maps Container Div Width and Height 100%
What Does the Forward Slash Mean in the CSS Font Shorthand
How to Make Internet Explorer 8 to Support Nth Child() CSS Element
Ie11 - Does a Polyfill/Script Exist For CSS Variables
How to Select All Children of an Element Except the Last Child
How to Style Default Confirm Box With Only Css
How to Use a Unitless CSS Variables and Later Add the Needed Unit