CSS Selector Syntax

By Xah Lee. Date: . Last updated: .
match any element. (example: div span p img etc) [see HTML5 Tags Complete List]
match any element named e.
match any e2 that is a direct child (first-level nesting) of e1.
e1 e2
match any e2 that is nested (any level) inside e1.
match any e2 immediately preceded by e1. (e1 and e2 are siblings, with e1 coming first.)
expr1, expr2
match any selector expression expr1, expr2, expr3 etc.
match e only if it is nth child element. (counting start at 1. Cannot be negative.)
count from right, starting at 1.
same as e:nth-child(1).
same as e:nth-last-child(1)
match e that is nth sibling of name e. Count starts at 1.
same as e:nth-of-type(n) but counting from right.
same as e:nth-of-type(1)
same as e:first-of-type but the last.
match e that is root of document.
match e if it is the only type (tag name) among siblings.
match e if it's the only child.
match e that has no children. That is, with no inner text
match e that has a id value same as the current URL #fragment.
match a user interface e that is enabled.
match a user interface e that is disabled
match a user interface e which is checked (example: radio-button, checkbox)
match e if it does not match simple selector s.
match e that has attribute a. Example of attributes: {id, class, href}.
match e, has attribute a, value v.
match e, has attribute a, value begins with v
match e, has attribute a, value ends with v
match e, has attribute a, value contains v
match e, has attribute a, value is a list of space-separated values, one of which is exactly equal to v.
match e, has attribute a, value is a hyphen-separated list of values beginning (from the left) with v.
HTML only. Match element e that has class v. Same as e[class~="v"]
same as e[id="v"]
match a link.
match visited link.
match a link that's clicked on but mouse button not yet released.
mouse hover over a element e
when a element is in focus (receive keys, etc. Click on a element to put focus to it.)
match e if its value of lang attribute is v.


Match Element

Make all paragraphs red.

p {color: red;}

Match Any Child

div span {color:red}

The code above will match all span tags.

[see CSS: Selector Tutorial]

Match Direct Child

div > span {color:red}

The code above will match A and B but not C.

Match Multiple Selector Expressions

Match Nth Child

Match “span” only when it is 1st child.

(Note, this is different from “match the 1st span”)

span:first-child {color:red}

This will not watch any span in the following:


Match Nth Type

span:only-of-type {color:red}
<div><div>11</div> <span>22</span> <span>33</span></div>
<div><div>AA</div> <span>BB</span> <div>CC</div></div>

In the above example, only the BB will be colored, because that span is the only span tag among siblings.

Match Attribute and Value

Color all links to Wikipedia green.

a[href*="wikipedia.org/"] {color:green}
<a href="http://en.wikipedia.org/wiki/math">math</a>

Match pseudo-classes

Make link to have underline only when mouse hovers over it.

a:visited, a:link {text-decoration:none}
a:hover {text-decoration:underline;}

Match pseudo-classes

/* make Chinese paragraphs red */
p:lang(zh) {color:red}
<p lang="zh">天下</p>

thanks to Brennan Young for correction on nth child.

Like it? Help me by telling your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

CSS Basics

  1. Basics
  2. Tag Matching Tutorial
  3. Selector Syntax
  4. Units


  1. Color Syntax
  2. Color Names
  3. Opacity
  4. Linear Gradient
  5. Radial Gradient


  1. font-family
  2. Standard Web Fonts
  3. font-size
  4. Meaning of Font Size
  5. Font Weight
  6. Chinese Font Names

Box Model

  1. Border
  2. Outline
  3. Margin vs Padding
  4. Box Sizing


  1. Layout Tutorial
  2. Display Property
  3. Position: static, relative, fixed, absolute
  4. Pure CSS Table
  5. Tabbed Menu
  6. 3 Columns Page Layout
  7. Multi-Column Layout for Text Flow
  8. Fix Element to Window
  9. Centering
  10. Flowing List
  11. z-index
  12. Text Over Image
  13. Text Flow Around Image
  14. OverFlow, Scrollbar


  1. 2D Transform
  2. Transition
  3. Animation

Line Wrap, Justification

  1. Line Wrap
  2. Text Alignment, Justification
  3. Match first-letter and first-line
  4. “:before”, “:after”
  5. Text Decoration: Underline, Overline, Line-Through
  6. Letter Spacing, Word Spacing

Box Decoration

  1. Box Shadow
  2. Text Shadow
  3. Round Corners
  4. Background Image
  5. Image Rollover


  1. Computed Style
  2. Pseudo Class vs Pseudo Element
  3. Media Query
  4. Variable
  5. calc
  6. Reset
  7. user-select
  8. Data URI Scheme
  9. protocol-relative URL
  10. Default Unit
  11. Declaring Character Set in CSS File
  12. Jargons Explained: Tag, Element, Node, Object, Attribute, Property, Method
  13. Display Table on Small Screen
  14. Fixed Aspect Ratio
  15. Add Icon to Links
  16. Progress Bar Example

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. CSS
  3. JavaScript
  4. JS Obj Ref
  5. DOM
  6. SVG
  7. Blog