The 30 CSS Selectors You Must Memorize Cheat Sheet

A Programming › CSS cheat sheet in English by dimitrios

https://code.tutsplus.com/tutorials/the-30-css-selectors-you-must-memorize--net-16048 Originally by Jeffrey Way, 9 Jun 2011

41,807 views 9,561 downloads 28 Sep 2016 History
4.1 avg · 2 ratings

*

Every element on the page.

#X

Target by id

.X

.error {
color: red;
}
Class selector

X Y

li a {
text-d­eco­ration: none;
}
Descendant selector.

X

a {
color: red;
}
All elements of page.

X:visited and X:link

pseudo­-cl­asses

X + Y

Adjacent selector only if the element is Y immedi­ately preceded by X.

X > Y

Only distinct children.

X ~ Y

Less strict sibling combin­atior, Y elements that follow X elements.

X[attr]

 
a[title] {
color: green;
}
X elements that have an attr attribute. For example anchor elements with title attribute.

Other attribute selectors

X[href="foo"] /* href == foo */
X[href*="foo"] /* foo somewhere in href */
X[href^="foo"] /* href begins with foo  */
X[href$="foo"] /* href ends with foo  */

X[data­-*=­"­foo­"]

 
a[data­-fi­let­ype­="im­age­"] {
color: red;
}
Custom attributes

X[foo~­="ba­r"]

 
a[data­-info="ex­ter­nal­"] {
color: red;
}
a[data­-info="im­age­"] {
border: 1px solid black;
}
and <a href="p­ath­/to­/im­age.jp­g" data-i­nfo­="ex­ternal image">
For spaced­-se­parated list of values.

X:checked

pseudo­-class

X:before

X:after

 

X:hover

 

X:not(­sel­ector)

 

X:pseu­doE­lement

x::fir­st-line
x::fir­st-­letter

X:nth-­chi­ld(n)

Integer n is not zero based, so first element is 1.
2n for every two items.

X:nth-­las­t-c­hild(n)

Other nths

X:nth-­of-­type(n)
X:nth-­las­t-o­f-t­ype(n)
X:firs­t-child
X:last­-child
X:only­-child
X:only­-of­-type / no siblings within parent container /
X:firs­t-o­f-type

Origin

Badges

Useful Cheat Sheet Great Cheat Sheet Nice Cheat Sheet Good Cheat Sheet Appreciated Cheat Sheet Downloaded from Unusual Locations × 6

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.