CSS Selector and Selector API Quick Reference

CSS selector

Direct Selector
SelectorMeaning
tagNameAll the tags
[tag].classNameAll the tags with a specific class
[tag]#idThe tag with a specific ID
*Any element
Context Selector
SelectorMeaning
E FSelect element F that is a descendent of E
E > FSelect element F that is a direct child of E
E + FSelect element F that is an immediate sibling after E
E ~ FSelect element F that is a sibling after E
Pseudo Element Selector
SelectorMeaning
::first-letterThe first letter in text
::first-lineThe first line of text
::beforeThe space immediately before the element (used with content property)
::afterThe space immediately after the element (used with content property)
Link and Dynamic Pseudo Class Selector
SelectorMeaning
:linkThe hyperlink when it first appears (used with a hyperlink tag)
:vistedThe hyperlink that has been clicked (used with a hyperlink tag)
:targetA targeted anchor link (used with an anchor tag)
:hoverThe element that is under the cursor
:activeThe element that is being clicked
:focusThe element that has the focus
:enabled and :disabledThe element that is enabled or disabled
:checkedThe element that is checked (used with a radio button or a checkbox)
Structural Pseudo Class Selector
SelectorMeaning
:rootThe top level element of the document
:emptyThe element that has no child
:only-childThe element that has no siblings
:first-childThe first child of some other element
:last-childThe last child of some other element
:nth-child(an + b)

The (an + b)-th child within its parent element. The index of the first child is 1. The use of a and b is:

  • If a > 0, b > 0, all the children are divided into groups of size a, and it selects the b-th child within each group. Eg., :nth-child(n) selects all the children, :nth-child(n + 1) selects the second, the third, ..., the last children, :nth-child(3n) selects every third child;
  • If a > 0, b < 0, all the children are divided into groups of size a, and it selects the |b|-th last child within each group. Eg., :nth-child(10n - 1) is the same as :nth-child(10n + 9);
  • If a = 0, b != 0, it selects the b-th child (b > 0) or |b|-th last child (b < 0);
  • If a < 0, only n-th child that satisfies condition an + b ≥ 0 will be selected; Eg., :nth-child(-n + 2) selects only the first child (n = 1) and the second child (n = 2);
  • odd or even can be used. odd is the same as :nth-child(2n + 1), and even is the same as :nth-child(2n).
:nth-last-child(an + b)The (an + b)-th last child within its parent element
:only-of-typeThe element that has no sibling of the same type
:first-of-typeThe first of its selector type within its parent element
:last-of-typeThe last of its selector type within its parent element
:nth-of-type(<an + b)The (an + b)-th of its selector type within its parent element
:nth-last-of-type(an + b)The (an + b)-th of its last selector type within its parent element
Other Pseudo Class Selector
SelectorMeaning
:lang(l)The element that has a specific language code l defined
:not(s)Not confirming to selector s
Attribute Selector
SelectorMeaning
[attr]The element that has a specific attribute
[attr = "value"]The element that has a specific attribute with an exact value
[attr ~= "value"]The element that has a specific attribute with an exact value in a space-seprated value list
[attr |= "value"]The element that has a specific attribute with an exact value in a hyphen-seprated value list
[attr ^= "value"]The element that has a specific attribute with an exact pattern at the beginning of the value
[attr $= "value"]The element that has a specific attribute with an exact pattern at the end of the value
[attr *= "value"]The element that has a specific attribute with an exact pattern anywhere in the value

Access of DOM element

Selector API

References

  1. Selectors Level 3
  2. Jason C. Teague, CSS3: Visual QuickStart Guide, 5th edition
  3. Selectors API Level 1