CSS Selector

The CSS selector is a set of CSS rules for selecting the elements you want. Commonly used selectors and syntax are as follows:

SelectorExampleComment
.class.fooSelects all elements with class="foo"
#id#fooselects all elements with id="foo"
elementpSelect all <p> elements
element elementdiv pSelects all <p> elements within <div> elements
element > elementdiv > pSelects all <p> elements whose parent is a <div> element
element + elementdiv + pSelects all the first <p> elements immediately after the <div> element
[attribute][target]Selects all elements with target attribute
[attribute=value][target=_blank]Selects all elements using target="_blank"
[attribute~=value][title~=flower]Selects all elements whose title attribute contains the word "flower"
[attribute|=language][lang|=en]Selects all elements with a lang attribute equal to en, or starting with en-
[attribute^=value]a[href^="https"]Selects every element whose href attribute value starts with "https"
[attribute$=value]a[href$=".pdf"]Selects each element whose href attribute value ends with ".pdf"
[attribute*=value]a[href*="foo"]Selects each element whose href attribute value contains the substring "foo"
:nth-child(n)p:nth-child(2)selects each p element that is the second child of its parent
:nth-last-child(n)p:nth-last-child(2)Selects each p element that is the second child of its parent, counting from the last child
:nth-of-type(n)p:nth-of-type(2)Selects the second p element that each p element is its parent
:nth-last-of-type(n)p:nth-last-of-type(2)Select each p element that is the second p element of its parent, counting from the last child
:first-childp:first-childselects each p element that is the first child of its parent
:last-childp:last-childselects each p element that is the last child of its parent