CSS selectors

There are many types of CSS selectors that enable you to format elements in an HTML document to various levels of specifity.

Selectors are followed by { } which contain the declarations which are made of a Property: Value pair

Example

p { <-- selector
color: red; <--declaration, made up of property (color) : value (red)
}

Reference

Selector Description Use
* {} Universal selector Applies to all elements in the document
h1, h2 {} Type selector Matches element names
.class {}
p.class {}
Class selector Matches an element whose class attribute matches that after the period
If there is an element before the period, matches only those elements with that class
#id {} ID selector Matches an element whose ID matches that after the #
li>a {} Child selector Matches an element that is a direct child of another
li a {} Descendant selector Matches an element that sits inside another element, even if other elements nested between them
h1+p {} Adjacent sibling selector Matches an element that is the next sibling of another (first <p> after any <h1> element)
h1~p {} General sibling selector Matches an element that is a sibling of another (would apply to all <p> elements that are siblings of an <h1> element