CSS: Selector Tutorial

By Xah Lee. Date: . Last updated: .

The following are most common patterns in using CSS. You should memorize them by heart.

Match Tag Names

Some common examples of setting appearance for HTML elements.

body {font-family:sans-serif}

h1,h2,h3,h4,h5,h6 {font-family:serif}

p {line-height:1.5}

blockquote {color:navy}

img {margin:1px}

Match Class Value

/* matches any tag of class “xyz”*/
.xyz {color:red}

the above will match both of the following:

<div class="xyz">…</div>
<span class="xyz"></span>

Note: a HTML element can have more than one class.

[see HTML: Multiple Class Attribute Values]

Match Tag Name and Class Value

/* matches any div of class “xyz”*/
div.xyz {color:red}

the above will match

<div class="xyz">…</div>

Here's another example:

/* matches any span of class “ab”*/
span.ab {color:blue}

This will match:

<span class="ab"></span>

Match a Tag's ID

/* matches any tag with id of “xyz”*/
#xyz {color:red}

the above will match any of:

<div id="xyz"></div>
<span id="xyz"></span>
<p id="xyz"></p>

Note: a tag's id should be unique on a page. A page cannot have two tags having the same id. A frame [see HTML: Split Windows; Frameset] or iframe [see HTML: Iframe] counts as separate pages.

Change Link Appearance

Example of removing the underline in links.

a:link {text-decoration:none}
a:visited {text-decoration:none}

[see CSS: Text Decoration: Underline, Overline, Line-Through]

Multiple Tags A, B, C

If you have multiple tags and want them all to have the same appearance, you can use comma.

For example, this:

span.x, span.y, span.z {color:red}

is equivalent to:

span.x {color:red}
span.y {color:red}
span.z {color:red}

Match Nested Tags (all levels)

Sometimes, you want to match a tag only if it is nested inside another tag (regardless how many levels deep). You can use the space to specify this, like this:

div.abc span.xyz {color:red}

In the above example, it'll match any HTML span with attribute class="xyz" inside div with attribute class="abc" .

<div class="abc">

 Once <span class="xyz">upon</span> a time,

 some <span class="xyz">thing</span>


Parent Child Relation (Nested Tags (1st level only))

Sometimes you want to match a tag only if it is a immediate child of a given parent tag. You can use the > to specify a parent-child relationship.

Here's a example:

div > span {color:red}

In the following HTML code, the CSS code above will match the “upon” but not “you”.

once <span>upon</span> a time
<footer>Thank <span>you</span> for reading</footer>

Complete List of CSS Selector Syntax

CSS Selector Syntax

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