CSS Selectors Cheat Sheet

In CSS

CSS selectors are an essential part of creating stylish and engaging web pages. They allow developers to target specific elements of a page and apply styling rules to them. Selectors can target elements based on their tag name, class name, ID, attributes, and more.

With so many different types of selectors available, it can be challenging to keep track of them all. That’s where this CSS selector cheat sheet comes in handy. It provides a comprehensive list of all the different types of selectors and their corresponding descriptions.

Cheat Sheet

Universal Selectors

SelectorDescription
*Selects all elements

Type Selectors

SelectorDescription
tagnameSelects all elements of the specified tag name

Class Selectors

SelectorDescription
.classnameSelects all elements with the specified class name

ID Selectors

SelectorDescription
#idnameSelects the element with the specified ID

Attribute Selectors

SelectorDescription
[attribute]Selects all elements that have the specified attribute
[attribute=value]Selects all elements where the specified attribute has the specified value
[attribute~=value]Selects all elements where the specified attribute contains the specified value as one of its words
[attribute=value]

Descendant Selectors

SelectorDescription
ancestor descendantSelects all descendants of the specified ancestor element

Child Selectors

SelectorDescription
parent > childSelects all direct children of the specified parent element

Adjacent Sibling Selectors

SelectorDescription
element + siblingSelects the specified sibling element that is immediately following the specified element

General Sibling Selectors

SelectorDescription
element ~ siblingSelects all sibling elements that follow the specified element

Pseudo-Classes

SelectorDescription
:activeSelects the active link
:checkedSelects all checked elements
:disabledSelects all disabled elements
:emptySelects all elements that have no children
:enabledSelects all enabled elements
:first-childSelects the first child element of its parent
:first-of-typeSelects the first element of its type
:focusSelects the element that has focus
:hoverSelects an element when the mouse pointer is over it
:last-childSelects the last child element of its parent
:last-of-typeSelects the last element of its type
:not(selector)Selects all elements that do not match the specified selector
:nth-child(n)Selects the nth child element of its parent
:nth-last-child(n)Selects the nth child element of its parent, counting from the last child
:nth-last-of-type(n)Selects the nth element of its type, counting from the last element
:nth-of-type(n)Selects the nth element of its type
:only-of-typeSelects the only element of its type
:only-childSelects the only child element of its parent
:rootSelects the document’s root element
:targetSelects the current active element in the document’s URL

Pseudo-Elements

SelectorDescription
::afterInserts content after the selected element
::beforeInserts content before the selected element
::first-letterSelects the first letter of the selected element
::first-lineSelects the first line of the selected element
::selectionSelects the portion of an element that is selected by the user

Reference:

https://developer.mozilla.org/en-US/docs/Web/CSS

#