CSS Selectors

CSS selectors define the elements to which a set of CSS rules apply.

ID selector

Selects an element based on the value of its id attribute. There should be only one element with a given ID in a document.

#id-selector {
  /*   */
}

Class selector

Selects all elements that have the given class attribute. Syntax: .classname Example: .index will match any element that has a class of "index".

.class-selector {
/*        */
}

Tag selector

Selects all elements that have the given node name. Syntax: elementname Example: input will match any element.

body {
/*        */
}

Attribute selector

Selects all elements that have the given attribute.

a[href$=".org"] {
  color: red;
}

Universal selector

Selects all elements. Optionally, it may be restricted to a specific namespace or to all namespaces.

* {
  color: green;
}