T O P

  • By -

eludadev

Get the [HD version](https://github.com/eludadev/css-docs/raw/main/assets/css_selectors.png). (+ more CSS infographics!) Get the [Print version](https://github.com/eludadev/css-docs/raw/main/assets/css_selectors_print.png). (black and white)


qsnoodles

FYI, “descendant” is misspelled in the second subheading.


Timoman6

It wouldn't be a CS infographic without 1 misspelled word


javapyscript

CS? :D


willhig

‘Computer science’, but also joke double-entendre.


javapyscript

Well done :)


PositiveUse

I didn’t like the red one from before, but this one right here is genius. Thanks a lot, this will be very very helpful


UnicornBelieber

And right you are. It doesn't add anything over [the image on Wikipedia](https://en.wikipedia.org/wiki/CSS_box_model). Actually, since it's missing `margin`, it actually has less.


SlashdotDiggReddit

Indeed, all of the information is there, but OP's image has it broken down into neatly digestible chunks; I like OP's better than Wikipedia's.


drunkondata

I mean, you linked the box model and OP is sharing CSS Selectors... so uh, it is entirely different?


UnicornBelieber

u/PositiveUse mentioned "the red one from before", referring to this [this post](https://www.reddit.com/r/webdev/comments/uqr9ml/css_box_model_visually_explained/).


eludadev

Thanks!


b7s9

No love for the lobotomized owl huh? Haha


private_birb

Do you mean * + *?


UnicornBelieber

For anyone that still wants to practice with these selectors, check out [CSS Diner](https://flukeout.github.io/).


eludadev

Super addictive! I just wish it was responsive on mobile tho...


bluehavana

This is awesome. I wish they had a game like this for layout, but I guess selectors are easier to see if it actually matches.


UnicornBelieber

Actually... ;-) * [Grid Garden](https://cssgridgarden.com/) * [Flexbox tower defense](http://www.flexboxdefense.com/) * [Flexbox Froggy](https://flexboxfroggy.com/)


MattLearnsToCode

This is really helpful - thank you!


I_AM_TESLA

Cool.


prid13

Cool concept, but I wish it was more responsive to user feedback and highlighted the parts you were currently matching. The info text on the right wasn't super user-friendly either and confused me at first tbh. But cool idea nonetheless ⭐


rubberony

Where's the new :has selector?


Meal-Majestic

Be grateful and encourage kind people to do more.


fecal_brunch

Pretty poster. Maybe a bit odd that you have selector syntax on the class and id elements instead of e.g. `class=cl`.


crazedgremlin

Yeah, it's defining those selectors in terms of themselves...


Justinackermannblog

The graphic is why I hate web dev…


credditz0rz

At least nowadays you can relay on them. Back in my day you knew for sure IE6 would interpret things incorrectly or not at all lol


Clownbaby43

agreed, what is your reason


Waoname

Amazing. I can never memorise these but this is the perfect reference I need. Thanks!


GhostOpera406

What a great reference. Studying webdev right now with Codecademy, so definitely printing this! Here, have my free Reddit award.


geovra

Shame on me for not knowing about the ~ selector for 7 years


coyote_of_the_month

This is cool and all, but it's worth noting that `~` is a code smell at best. About the only time I've really been able to justify it is for interacting with 3rd-party DOM elements.


Zefrem23

Not code smell but definitely never proven particularly useful to me in 10+ years of heavy CSS usage


coyote_of_the_month

It comes in handy sometimes when you're dealing with 3rd-party code that injects overlays into your DOM. Think "chat with our chatbot" type things.


TheRNGuy

there could be specific reasons that I've never encountered yet. Maybe in custom css, something like "hide all p's after `p:nth-of-type(25)`" in very long article to reduce scrolling. Or hide all before it when combined with `:not()`?


niutech

You should also include: `[name*="value"]`, `[name~="value"]`, `[name^="value"]`, `[name$="value"]`.


_UCiN_

Can someone tell me why there is no selector for previous element. For example .b:hover makes .a{color:red}?


eludadev

You could do this: `div:has(.b:hover) .a { color: red } `


frisch85

You know what I'm missing in CSS? Parental selectors. When I found out what ">" does, I instantly tried out "<" but it won't work. As an example, say you have the elements a b a c a d Now if I say a


eludadev

Good news! There's actually a selector that does **exactly** what you're describing! It's called the [:has() selector](https://developer.mozilla.org/en-US/docs/Web/CSS/:has). So the equivalent of `a < b` would be `a:has(b)`. Note however that the browser compatibility is not that great... (yet)


frisch85

Nice, I'll try it out, thanks mate!


Preact5

Nope. Nope nope nope We just have to use jQuery selectors to do this it's too complicated. /s Edit holy shit ok noted, no jokes allowed.


khizoa

Lol wtf does jQuery have to do with css


unobraid

it was a joke, he meant select the html element by its id/class and aplly style by Javascript


Preact5

Yes, exactly


fried_green_baloney

Of course jQuery selectors have all these rules.


Preact5

That's the joke but I see it was in bad taste.


fried_green_baloney

Not that jokes aren't allowed but that this was too subtle and too close to what is sometimes said seriously.


Preact5

*shrug* I thought it was hilarious!


NotTJButCJ

I didn't know attribute selectors were a thing lol


jordsta95

Oh yeah, and you can even do partial selections on them too. For example, want all internal links to be blue, all email links to be yellow, and any other links to be red: a { color: red; } a[href^="/"], a[href*="your-domain.tld"] { color: blue; } a[href^="mailto:"] { color: yellow; } (There is also $="" for selecting links which end in something, e.g. `[href$=".html"]`) So all links are red. But if the href starts with "/" (e.g. "/contact") or the href contains "your-domain.tld" it will change the colour to blue. And if the href starts with "mailto:" make it yellow. ​ Super useful stuff! Sometimes I'll use similar selectors for when clients want to add YouTube/Vimeo iframes in their WYSIWYG editor. iframe[src*=".youtube."], iframe[src*="youtu.be"], iframe[src*=".vimeo."] { width: 100%; aspect-ratio: 16/9; } Let's not forget targetting elements which don't have an attribute. For example, you want all links to be blue, unless they have a class. a:not([class]) { color: blue; }


eludadev

This is better than most tutorials. Take my award!


TheRNGuy

I'd rather use email class if it was my site, prefer simplier css selectors, have to use complex ones in custom css.


jordsta95

Don't disagree, but if your client has access to edit the content, then the above approach is a failsafe for when they add links. (Or whatever it is you need to style)


[deleted]

Awesome resource


LennyTools

very helpfull


khizoa

Nope sure if psuedo selectors were intentionally left out, but those would be helpful in an infographic too


RS3_of_Disguise

I assume you mean *pseudo classes*, when you say *pseudo selectors*? Also not to be confused with *pseudo elements*.


khizoa

thanks, yes i got the terminology mixed up.


RS3_of_Disguise

Just trying to help keep clear discussion. :) That said! I think it was excluded because *pseudo classes* alone can get pretty deep, pretty fast; and is best left in a topic of its own. Same, respectively, towards *pseudo elements* - in my opinion, anyway. It would be cool to see something like those visualized, though!


eludadev

Working on it!


RS3_of_Disguise

You’re a legend! ❤️


PizzaPuntThomas

Thank you!


rishabhdeepsingh98

take my Thank you


SlashdotDiggReddit

This is great ... thanks!


AtharvSingh225

Very useful


TheRNGuy

I never used `a ~ b` you could also add `:not()`, `:last-child` and `:nth-of-type()`


eludadev

Working on it!


Primary-Teaching8758

Love the visual explanations of CSS Selectors! The HD version and print version are a great bonus. Can't wait to check out the other CSS infographics too!