Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialTravis Duffer
7,074 PointsWhat is the preferred notation for pseudo elements?
I've noticed a few times while watching CSS videos that the notation switches between double colons and single colons. For instance, sometimes the notation ".main-nav:after:" is used, and sometimes the notation ".main-nav::after:" is used. Which is preferred? Is there a difference?
Also, as a bonus question, I believe I have noticed the pseudo-selector "first-child" or "last-child" is sometimes written with a colon instead of a dash, as ".main-nav:first:child:" Is this an alternative syntax or am I getting multiple concepts confused here?
4 Answers
James Finn
7,055 PointsHi, in the CSS 3 spec, single colons means pseudo class, double colons means pseudo element.
I can't say it better than the master, Chris Coyier of Css-Tricks:
"Every browser that supports the double colon (::) CSS3 syntax also supports just the (:) syntax, but IE 8 only supports the single-colon, so for now, it's recommended to just use the single-colon for best browser support. :: is the newer format indented to distinguish pseudo content from pseudo selectors. If you don't need IE 8 support, feel free to use the double-colon." (Source)
Regarding your final question, your syntax (above) is incorrect because pseudo classes or elements do not contain colons, only dashes. Your syntax should read: .main-nav:first-child{}
Dustin Matlock
33,856 PointsHere's a compatibility table on ::selection
.
Dustin Matlock
33,856 PointsIt's essentially the same but the level 3 CSS spec adds an extra colon to differentiate between pseudo-elements and pseudo-classes.
You can use either one but older browsers won't recognize ::
. It might also be helpful to check out the grammar section on CSS selectors.
Travis Duffer
7,074 PointsOh my gosh I just messed my syntax all up. I wish I could remember which video I thought I saw the "first:child" notation in. I'm pretty sure I saw it but I can't remember where. Perhaps just a typo in the video.
Dustin Matlock
33,856 PointsMost likely it was Basic Structural Pseudo-Classes.
James Finn
7,055 PointsIt is possible to be confused by the fact that you can combine pseudo selectors. For example, a:visited:hover
could be used to declare user interaction styles upon any hover of a visited link.
More info about this here
James Barnett
39,199 PointsJames Barnett
39,199 Points>
Your syntax should read:.main-nav:first-child:
No trailing colon though.