DEV Community

6 powerful CSS selectors that will really help you write clean CSS.

Ibrahima Ndaw on December 12, 2019

Originally posted on my blog Selectors in CSS are used to pick elements and style them. They can be very powerful following which one we use. In...
Collapse
 
rafaelliendo profile image
Rafael Liendo

Probably should tag this with #beginer

Collapse
 
hyftar profile image
Simon Landry

Yeah I also thought I was about to learn something new ;/

Collapse
 
focusotter profile image
Michael Liendo

Not sure. The regex-like selectors are often times forgotten.

Collapse
 
ibrahima92 profile image
Ibrahima Ndaw

I'll add the beginner tag. Thanks

Collapse
 
rolandcsibrei profile image
Roland Csibrei

Yes, should be tagged as beginner, however it is good from time to time to refresh our knowledge and this was a perfect opportunity. Thanks a lot! Have a great day! R.

Collapse
 
ibrahima92 profile image
Ibrahima Ndaw

I'll add the beginner tag and thanks for your comment and have a great day too.

Collapse
 
yezehmat profile image
Ahmad Shahzad • Edited

What about selecting an a before a div?

<a></a>
<div></div>

Is this possible?

Collapse
 
verdedev profile image
verdedev

What you're describing is a "previous sibling" selector and it doesn't exist in CSS. Another popularly desired selector that doesn't exist is a "parent selector." While they both can be implemented in JavaScript pretty easily, the spec-writing gods of CSS, and the algorithm writers who implement those specifications have their reasons for denying us these things.

Collapse
 
alohci profile image
Nicholas Stimpson • Edited

The reasons come down to performance. We can see this from a couple of snippets from the Selectors Level 4 spec, where it discusses the Live and snapshot profiles

Selectors are used in many different contexts, with wildly varying performance characteristics. Some powerful selectors are unfortunately too slow to realistically include in the more performance-sensitive contexts.
ISSUE 1 - The categorization of things into the “live” or snapshot profiles needs implementor review. If some things currently not in the live profile can reasonably be done in CSS Selectors, we should move them.

In this particular case <a> before <div> could be done using the has() pseudo-class as a:has(+ div) or a:has(~ div), but has() is only part of the snapshot profile and therefore not for implementation in CSS.

Collapse
 
aimadghssisse profile image
Aimad Ghssisse

Very useful thank you

Collapse
 
ibrahima92 profile image
Ibrahima Ndaw

I'm glad that you find it useful, Thanks for your comment.

Collapse
 
lucian2403 profile image
Lucian2403

Very useful tips. Thank you!

Collapse
 
ibrahima92 profile image
Ibrahima Ndaw

I'm glad that you find it useful, Thanks for your comment.

Collapse
 
bipan82gill profile image
bipan82gill

Great job

Collapse
 
ibrahima92 profile image
Ibrahima Ndaw

Thanks for your comment.

Collapse
 
hnnx profile image
Nejc

Some of these are really helpful and easy to understand, thanks

Collapse
 
ibrahima92 profile image
Ibrahima Ndaw

I'm glad that you find it useful, Thanks for your comment.

Collapse
 
mostafa_hegazi profile image
Mostafa Hegazy

Yes, it's common selectors but useful, thanks

Collapse
 
ibrahima92 profile image
Ibrahima Ndaw

Thanks a lot for your comment.

Collapse
 
ji2026 profile image
jishnu chandran

good

Collapse
 
ibrahima92 profile image
Ibrahima Ndaw

Thanks

Collapse
 
kashanshah profile image
Syed Kashan Ali Shah

No matter this would help in writing clean code or not, but thanks for a quick recall to some of the unconventional selectors, which may surely help to style some typically written html 😁

Collapse
 
ibrahima92 profile image
Ibrahima Ndaw

Thanks

Collapse
 
mikaelfante profile image
Mikael Fante

Nicely put. Thanks for the tips, good work done.

Collapse
 
ibrahima92 profile image
Ibrahima Ndaw

I'm glad it could help, Thanks for your comment.

Collapse
 
anuraghazra profile image
Anurag Hazra

Btw just realized that,
*, $, ^ are all valid Regex quantifires. 🤯

Collapse
 
ibrahima92 profile image
Ibrahima Ndaw

That's it..regex is super powerful.

Collapse
 
mikehu profile image
MikeHu

Really good article. Thanks

Collapse
 
ibrahima92 profile image
Ibrahima Ndaw

Thanks for your comment.

Collapse
 
goodmuyis profile image
goodmuyis • Edited

You might want to review your html markups, they wrongly type and will confused a learner.

Powerfull css selector are the pseudo selector

Collapse
 
doug_horne profile image
Douglas Horne • Edited

Yeah, I knew these too. But, so what? He took the time to write the article, and I am sure others will find it useful. Nice job!

Collapse
 
ibrahima92 profile image
Ibrahima Ndaw

Thanks man!