Show Menu
Cheatography

The 30 CSS Selectors You Must Memorize Cheat Sheet by

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

*

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­-in­fo~­="ex­ter­nal­"] {

   color: red;

}

a[data­-in­fo~­="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

   
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          CSS Selectors Cheatsheet Cheat Sheet
          Five ways to center DIV element in CSS Cheat Sheet

          More Cheat Sheets by dimitrios

          CoffeeScript Cheat Sheet
          vim.spf13 Cheat Sheet
          My JS notes Cheat Sheet