DEV Community

What's the difference between : and :: in CSS?

Salma Alam-Naylor on September 01, 2022

I spent years Googling “the difference between : and :: in CSS” before the information stayed in my brain. Sound familiar? Then this post is for yo...
Collapse
 
abhinav1217 profile image
Abhinav Kulshreshtha

Just to add on, :before and :after, :first-letter and :first-line are also valid due to their legacy usage, since they were introduced way back before the definition was finalized. But it is highly recommended that for newer code, we should follow proper convention.

Collapse
 
ankush981 profile image
Ankush Thakur

Thanks! My brain was going in a tizzy thinking about these. Damn, is CSS a mess?!

Collapse
 
abhinav1217 profile image
Abhinav Kulshreshtha

It is not a mess if you look at the history with a border perspective. The big reason for all these messes is the fact that web was originally designed for documents. Then we sprinkled some dynamic elements on those documents. The current web is more like a platform, with payment api, device api, system api, some that were needed to improve web, some that were added by big corps purely for their own purposes and no relation to web at all. And a primary goal of web is to never break backward compatibility, so even though some things (like definition of psudo-elements and psudo-classes) were needed to change, they had to adopt it to make sure none of the past pages break.

There is an interesting post with CSS working group on their mistake list that they now realize but cannot change until time machine is invented. Do give it a read, you would enjoy it.

Collapse
 
whitep4nth3r profile image
Salma Alam-Naylor

Indeed, that’s why I didn’t mention it 😉

Collapse
 
kolja profile image
Kolja

And you can combine them 😉

Collapse
 
jessica89079831 profile image
Jessica • Edited

In CSS, ::after creates a pseudo-element that is the last child of the selected element. Right?

Collapse
 
whitep4nth3r profile image
Salma Alam-Naylor

No it’s not the last child, because it’s a fake element! 😁

Collapse
 
asafagranat profile image
Asaf Limi Agranat

Not only its not the last child - it is not a child at all, but placed in the DOM at the same node level. Meaning that the selected element and its ::after pseudo-element share the same direct parent element.

Collapse
 
raguay profile image
Richard Guay

Thanks. I keep getting them mixed up myself.

Collapse
 
jlrxt profile image
Jose Luis Ramos T.

No estás solo, me pasa lo mismo.🤣😀🤣

Collapse
 
monicafidalgo profile image
The Coding Mermaid 🧜‍♀️

Great article Salma! Simple and right on point💪😊

Collapse
 
whitep4nth3r profile image
Salma Alam-Naylor

Thank you!

Collapse
 
jlrxt profile image
Jose Luis Ramos T.

Gracias excelente articulo. Ahora repetiré en mi cabeza : define una seudo-clase y : : define un seudo-elemento. Listo ya lo tengo.

Collapse
 
whitep4nth3r profile image
Salma Alam-Naylor

De nada! 😎

Collapse
 
jonosellier profile image
jonosellier

A good rule is : is for the state of the element itself and :: is for a pseudo-element