Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use single or double colon notation for pseudo-elements?

Since IE7 and IE8 don't support the double-colon notation for pseudo-elements (e.g. ::after or ::first-letter), and since modern browsers support the single-colon notation (e.g. :after) for backwards compatibility, should I use solely the single-colon notation and when IE8's market share drops to a negligible level go back and find/replace in my code base? Or should I include both:

.foo:after, .foo::after { /*styles*/ } 

Using double alone seems silly if I care about IE8 users (the poor dears).

like image 325
jinglesthula Avatar asked Apr 16 '12 21:04

jinglesthula


People also ask

How many colons we use in case of pseudo-classes?

It only accepts a single colon for both pseudo-classes and pseudo-elements. Because of this, all modern browsers are required to accept the single colon syntax for the current 4 pseudo-elements mentioned (but not for any future pseudo-elements introduced).

What is the difference between single colon and double colon?

The double colon replaced the single-colon notation for pseudo-elements in CSS3. This was an attempt from W3C to distinguish between pseudo-classes and pseudo-elements. The single-colon syntax was used for both pseudo-classes and pseudo-elements in CSS2 and CSS1.

Which feature uses double colon?

It means pseudo element selector. It means the element to the right doesn't exist in the normal DOM, but can be selected.

Is it :: before or before?

Note the double-colon ::before versus the single-colon :before . Which one is correct? Technically, the correct answer is ::before . But that doesn't mean you should automatically use it.


2 Answers

Do not use both combined with a comma. A CSS 2.1 compliant (not CSS3 capable) user agent will ignore the whole rule:

When a user agent cannot parse the selector (i.e., it is not valid CSS 2.1), it must ignore the selector and the following declaration block (if any) as well.

CSS 2.1 gives a special meaning to the comma (,) in selectors. However, since it is not known if the comma may acquire other meanings in future updates of CSS, the whole statement should be ignored if there is an error anywhere in the selector, even though the rest of the selector may look reasonable in CSS 2.1.

http://www.w3.org/TR/CSS2/syndata.html#rule-sets

You could however use

.foo:after { /*styles*/ } .foo::after { /*styles*/ } 

On the other hand this is more verbose than necessary; for now, you can stick with the one-colon notation.

like image 62
user123444555621 Avatar answered Sep 18 '22 17:09

user123444555621


From CSS3 Selectors REC:

This :: notation is introduced by the current document in order to establish a discrimination between pseudo-classes and pseudo-elements.
For compatibility with existing style sheets, user agents must also accept the previous one-colon notation for pseudo-elements introduced in CSS levels 1 and 2 (namely, :first-line, :first-letter, :before and :after).
This compatibility is not allowed for the new pseudo-elements introduced in this specification.

It seems you're safe using (only) one-colon notation for pseudo-elements that already existed in CSS2.1 as UAs must be backward compatible.

like image 28
FelipeAls Avatar answered Sep 18 '22 17:09

FelipeAls