CSS3 selectors Cheat Sheet

6 mins read

CSS Selectors

CSS selectors are utilized to select the content you need to style. In CSS Rule Set, Selectors are the part. CSS selectors select HTML elements as per their id, class, type, attribute, etc.

CSS selectors are divided into five categories:

  1. Simple/Basic selectors (select elements based on name, id, class)
  2. Combinator selectors (select elements based on a specific relationship between them)
  3. Pseudo-classes selectors (select elements based on a certain state)
  4. Pseudo-elements selectors (select and style a part of an element)
  5. Attribute selectors (select elements based on an attribute or attribute value)

Simple Selectors

SelectorExampleExample description
#id#firstnameSelects the element with id="firstname"
.class.introSelects all elements with class="intro"
element.classp.introSelects only <p> elements with class="intro"
**Selects all elements
elementpSelects all <p> elements
element,element,..div, pSelects all <div> elements and all <p> elements

Basic Selectors

SelectorDescriptionExample
elementType selector. Matches an element.p { color: red } <br/> matches paragraphs
.classClass selector. Matches the value of anclassattribute..warning { color: red } <br/>matches elements containing class="warning"
#idID selector. Matches the value of an id attribute.#warning { color: red } <br/>matches elements containing id="warning"
*Universal selector. Matches everything.* { color: red } <br/>matches everything

Attribute selectors

SelectorDescriptionExample
[attribute]Matches elements containing a given attribute.a[href] { <br/>color: red;<br/>} <br/>matches a elements with an href attribute
[attribute="x"]Matches elements containing a given attribute with a given value.a[href="/x/"]<br/>{color: red;} <br/> matches a elements with the attribute and value href="/x/"
[attribute~="x"]Matches elements containing a given attribute with a value that contains a sub-value within a space-separated list.abbr[title~="x"]<br/>{ color: red;}<br/>matches abbr elements with a title that contains 'x' (such as in title="Cascading Style Sheets")
[attribute|="x"]Matches elements containing a given attribute with a value that contains a sub-value within a hyphen-separated list.<br/>Matches html elements with a lang attribute that contains 'en' (such as in lang="en-gb")
[attribute^="x"]Matches elements containing a given attribute with a value that starts with something.a[href^="http://"]<br/>{color: red;} matches a elements with an href attribute, the value of which begins with 'http://'
[attribute$="x"]Matches elements containing a given attribute with a value that ends with something.a[href$=".com"] <br/>{color: red; }<br/>matches a elements with an href attribute, the value of which ends with '.com'
[attribute*="x"]Matches elements containing a given attribute with a value that contains something.a[href*="hdog"]<br/>{color: red;}<br/>matches a elements with an href attribute, the value of which contains 'hdog'

Pseudo-classes Selectors

SelectorDescriptionExample
:linkMatches a link that has not been visited.a:link { color: blue }
:visitedMatches a link that has been visited.a:visited { color: purple }
:activeMatches an element that is being activated, such as a link being clicked on.a:active { color: red }
:hoverMatches an element whose box is being hovered over by a cursor.a:hover { text-decoration: none }
:focusMatches an element that has focus, such as one that has been tabbed to.a:focus { border: 1px solid yellow }
:targetMatches an element that has been linked to (via<a href="#x"…,for example).h2:target { color: red }<br/>matches a second-level heading that has been linked to
:lang()Matches an element of a given language.p:lang(fr) { color: red }<br/>matches paragraphs that are declared, or otherwise considered, as French
:first-childMatches the first child of an element.p:first-child { color: red }<br/>matches the first child, if it is a paragraph, of an element
:last-childMatches the last child of an element.div p:last-child { color: blue }<br/>matches the last child, if it is a paragraph, of an element
:first-of-typeMatches the first sibling of its type in an element.li:first-of-type { color: red }<br/>matches the first instance of a list item inside an element
:last-of-typeMatches the last sibling of its type in an element.li:last-of-type { color: blue }<br/>matches the last instance of a list item inside an element
:nth-child()Matches an element that is the ordinal number child of its parent.p:nth-child(3) { color: red }<br/>matches the third child, if it is a paragrpah, of an element
:nth-last-child()Matches an element that is the ordinal number child, in reverse order, of its parent.p:nth-last-child(2) { color: blue }<br/>matches the next-to-last child, if it is a paragraph, of an element
:nth-of-type()Matches an element that is the ordinal number sibling of its type.li:nth-of-type(5) { color: red }<br/>matches the fifth instance of a list item inside an element
:nth-last-of-type()Matches an element that is the ordinal number sibling, in reverse order, of its type.li:nth-of-type(5) { color: red }<br/>matches the next-to-last instance of a list item inside an element
:only-childMatches an element if it is the only child of its parent.article p:only-child { color: red }<br/>matches a paragraph if it is the only child of an article element
:only-of-typeMatches an element if it is the only sibling of its type.article aside:only-of-type { color: blue }<br/>matches an aside element if it is the only aside element in an article element
:emptyMatches an element with no children, or content.td:empty { border-color: red }<br/>matches table data cells with nothing in 'em
:rootMatches the root element of a document. This will be the html element in HTML.:root { background: yellow }
:enabledMatches form control elements that are not disabled.input:enabled { border-color: lime }<br/>matches input elements that are not disabled
:disabledMatches form control elements that are disabled.input:enabled { border-color: red }<br/>matches input elements that are disabled
:checkedMatches a radio or checkbox type input element that is checked.input:checked { outline: 3px solid yellow }<br/>matches checked input elements
:not()Negotiation pseudo-class. Matches an element that does not match a selector.p:not(:first-child) { color: orange }<br/>matches paragraphs that are not first children

Pseudo-elements Selectors

SelectorDescriptionExample
::first-lineMatches the first textual line in an element.p::first-line { font-weight: bold }<br/>matches the first line in a paragraph
::first-letterMatches the first letter in an element.p::first-letter { font-size: 2em }<br/>matches the first letter in a paragraph
::beforeUsed with the content property to generate content before the initial content of an element.h1::before { content: "*" }<br/>places an asterisk at the start of a top-level heading
::afterUsed with the content property to generate content after the initial content of an element.h1::after { content: "+" }<br/>places a plus-sign at the end of a top-level heading

Combinator Selectors

SelectorDescriptionExample
selector selectorDescendant combinator. Matches elements that are descendants of another element.aside p { color: red }<br/>matches paragraphs inside elements containing class="warning"
selector > selectorChild combinator. Matches elements that are children of another element..warning > p { color: red }<br/>matches paragraphs that are children of elements containing class="warning"
selector + selectorAdjacent sibling combinator. Matches elements that immediately follow another element.h1 + * { color: red }<br/>matches the first element to follow a top-level heading
selector ~ selectorGeneral sibling combinator. Matches elements that follow another element.h2 ~ p { color: red }<br/>matches every paragraph that follows a second-level heading

CSS Group Selector

The grouping selector in CSS picks all the HTML elements with the same style definitions.

1h1 { 2 text-align: center; 3 color: red; 4} 5h2 { 6 text-align: center; 7 color: red; 8} 9p { 10 text-align: center; 11 color: red; 12} 13

To minimize the code, just apply the CSS grouping selectors. Simply group the selectors by separating each selector with a comma. Let's see the following code after CSS Grouping Selectors:

1h1, h2, p { 2 text-align: center; 3 color: red; 4} 5