Skip to content

Instantly share code, notes, and snippets.

@maheshbabu
Forked from nathansmith/this-is-madness.css
Created October 6, 2015 10:36
Show Gist options
  • Save maheshbabu/90bd2d60d5c6e10568e0 to your computer and use it in GitHub Desktop.
Save maheshbabu/90bd2d60d5c6e10568e0 to your computer and use it in GitHub Desktop.
Examples of CSS selector strength.
* {}
tag {}
[attribute] {}
[attribute="value"] {}
.class {}
tag[attribute] {}
tag[attribute="value"] {}
tag.class[attribute] {]}
tag.class[attribute="value"] {}
#id {}
tag#id {}
tag#id[attribute] {}
tag#id[attribute="value"] {}
#id.class {}
tag#id.class {}
tag#id.class[attribute] {}
tag#id.class[attribute="value"] {}
/*
And of course, !important always
wins, no matter what the selector.
*/
tag {
key: value !important;
}
/*
And, the whole game begins anew, as soon as
there's a parent with a descendant selector.
*/
tag {}
body tag {}
tag#id.class[attribute="value"] {}
body tag#id.class[attribute="value"] {}
/*
Here's a cool trick you can do, since .foo can also
be written as attribute selector of [class="foo"].
*/
.foo[class~="foo"] {}
/*
Or, hey... Stacked attribute selectors!
*/
input[type="text"][disabled] {}
/*
Really, everthing except for tag/id
can be stacked, if multiples exist.
*/
.class-1.class-2[attribute-1="value"][attribute-2="value"] {}
tag#id.class-1.class-2[attribute-1="value"][attribute-2="value"] {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment