Is there any functional difference between the CSS 2.1 :after
and the CSS 3 ::after
pseudo-selectors (other than ::after
not being supported in older browsers)? Is there any practical reason to use the newer specification?
Teoharov Specifically, you can use :after and ::after interchangeably with identical behaviour with the exception of IE8 which, as the question notes, requires the single colon.
::after (:after) In CSS, ::after creates a pseudo-element that is the last child of the selected element. It is often used to add cosmetic content to an element with the content property. It is inline by default.
Definition and UsageThe ::before selector inserts something before the content of each selected element(s). Use the content property to specify the content to insert. Use the ::after selector to insert something after the content.
The CSS ::before selector inserts content before a selected element. CSS :after inserts content after a specified element. These selectors are commonly used to add text before or after a paragraph or a link.
It's pseudo-class vs pseudo-element distinction.
Except for ::first-line
, ::first-letter
, ::before
and ::after
(which have been around a little while and can be used with single colons if you require IE8 support), pseudo-elements require double colons.
Pseudo-classes select actual elements themselves, you can use :first-child
or :nth-of-type(n)
for selecting the first or specific <p>
's in a div, for example.
(And also states of actual elements like :hover
and :focus
.)
Pseudo-elements target a sub-part of an element like ::first-line
or ::first-letter
, things that aren't elements in their own right.
Actually, better description here: http://bricss.net/post/10768584657/know-your-lingo-pseudo-class-vs-pseudo-element
Also here: http://www.evotech.net/blog/2007/05/after-v-after-what-is-double-colon-notation/
CSS Selectors like ::after
are some virtual elements not available as a explicit element in DOM tree. They are called "Pseudo-Elements" and are used to insert some content before/after an element (eg: ::before
, ::after
) or, select some part of an element (eg: ::first-letter
). Currently there is only 5 standard pseudo elements: after, before, first-letter, first-line, selection
.
On the other hand, there are other types of selectors called "Pseudo-Classes" which are used to define a special state of an element (like as :hover
, :focus
, :nth-child(n)
). These will select whole of an existing element in DOM. Pseudo classes are a long list with more than 30 items.
Initially (in CSS2 and CSS1), The single-colon syntax was used for both pseudo-classes and pseudo-elements. But, in CSS3 the ::
syntax replaced the :
notation for pseudo-elements to better distinguish of them.
For backward compatibility, the old single-colon syntax is acceptable for pseudo-elements like as :after
(browsers still all support the old syntax with one semicolon). Only IE-8 doesn’t support the new syntax (use single-colon if you want to support IE8).
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