CSS selectors are patterns used to select elements and apply styles in CSS (Cascading Style Sheets). They offer various methods for targeting elements with precision.
|
* Selects All Elements |
The * selector (asterisk, also known as the universal selector) selects all elements in the document. |
|---|---|
|
element Select by Element Name |
The element selector selects all elements in the document that match the specified element name. For example, the h2 selector selects all <h2> elements in the document. |
|
[attribute] Select Elements by Attribute Name |
The [attribute] selector selects all elements that have an attribute whose name matches the specified attribute. For example, the [title] selector selects all elements in the document that have a title attribute. |
|
[attribute="value"] Select Elements by Attribute Name and Value |
The [attribute="value"] selector selects all elements that have a specified attribute whose value exactly matches the specified value. |
|
[attribute*="value"] Select Elements by Attribute Value Substring Match |
The [attribute*="value"] selector selects all elements that have a specified attribute whose value contains the string "value". |
|
[attribute~="value"] Select Elements by Attribute Value Word Match |
The [attribute~="value"] selector selects all elements that have a specified attribute whose value contains the word "value". |
|
[attribute^="value"] Select Elements by Attribute Value Prefix Match |
The [attribute^="value"] selector selects all elements that have a specified attribute whose value starts with "value". |
|
[attribute$="value"] Select Elements by Attribute Value Suffix Match |
The [attribute$="value"] selector selects all elements that have a specified attribute whose value ends with "value". |
|
Class Selector (.class) Select Elements by Class Name |
The .class selector selects all elements that have a class with the specified class name, "class". Placing the class name immediately after the "." symbol selects all elements that have a class with that name. |
|
ID Selector (#id) Select Elements by ID Value |
The #id selector selects elements with an id attribute that matches the specified id value. Placing the id value immediately after the # symbol selects elements whose id attribute matches that value. |
|
:root Select the Root Element of the Document |
The :root pseudo-class selector selects the root element of the document. In HTML, it selects the root <html> element. |
|
:first-child Select the First Child |
The :first-child pseudo-class selects the element that is the first child of its parent. It works the same as the :nth-child(1). |
|
:last-child Select the Last Child |
The :last-child pseudo-class selects the element that is the last child of its parent. It works the same as the :nth-last-child(1). |
|
:nth-child(n) Select the nth child |
The :nth-child(n) pseudo-class selects the element that is the nth child of its parent. For example, :nth-child(2) selects the second child element. |
|
:nth-last-child(n) Select the nth Child Element from the End |
The :nth-last-child(n) pseudo-class selector selects an element that is the nth child of its parent, counting from the end. For example, :nth-last-child(2) selects the second child element when counting backward from the last child. |
|
:nth-of-type(n) Select the nth Child of the Same Tag |
The :nth-of-type(n) pseudo-class selects the element that is the nth sibling of the same tag type. For example, p:nth-of-type(2) selects the second <p> element among only the <p> elements that share the same parent. |
|
:nth-last-of-type(n) Select the nth-to-last Child of the Same Tag |
The :nth-last-of-type(n) pseudo-class selector selects the nth element from the end among siblings of the same tag name. |
|
:first-of-type Select the First Child of the Same Tag |
The :first-of-type pseudo-class selects the element that is the first sibling of the same tag type. For example, p:first-of-type selects the first <p> element among only the <p> elements that share the same parent. |
|
:last-of-type Select the Last Child of the Same Tag |
The :last-of-type pseudo-class selects the element that is the last sibling of the same tag type. For example, p:last-of-type selects the last <p> element among only the <p> elements that share the same parent. |
|
:only-of-type Select the Only Child of Its Tag Name |
The :only-of-type pseudo-class selector selects an element that is the only one of its tag type (tag name) among its sibling elements. In other words, it matches an element that is both the first and the last of its kind within the same parent. |
|
:hover Select an Element While the Mouse Is Over It |
The :hover pseudo-class selector selects an element while a pointing device (such as a mouse cursor) is over it. |
|
:active Select an Element While the Mouse Is Pressed |
The :active pseudo-class selector selects an element from the moment a pointing device (such as a mouse button) is pressed until it is released, while over the element. |
|
:focus Selects an Element While It Has Focus |
The :focus pseudo-class selector selects an element while it has focus. An element typically receives focus when a user navigates to it by pressing the Tab key or clicks it. |
|
:focus-within Matches Focused Elements or Descendants |
The :focus-within pseudo-class selector selects an element when it, or any of its descendants, has focus. |
|
:checked Select Checked Radio Buttons & Checkboxes |
The :checked pseudo-class selector selects any radio button (<input type="radio">) or checkbox (<input type="checkbox">) that is checked. |
|
:not(selectorX) Select Elements NOT Matching SelectorX |
The :not(selectorX) functional pseudo-class, which represents negation, selects elements that are NOT selected by the selectorX selector specified as its argument. |
|
::before Before Pseudo-Element Selector |
The ::before pseudo-element selector generates a child pseudo-element directly before the selected element's actual content, acting as its first child, and allows it to be styled. |
|
::after After Pseudo-Element Selector |
The ::after pseudo-element selector generates a child pseudo-element directly after the selected element’s actual content, acting as its last child, and allows it to be styled. |
|
::selection Styling Selected Text |
The ::selection pseudo-element is a selector that allows you to style text selected by the user by dragging with a mouse or other input methods (also known as highlighted text). |
|
::marker Select Automatically Generated List Markers |
The ::marker pseudo-element selector selects the list markers automatically generated for elements whose display property is set to list-item, such as li and summary elements. |
|
::placeholder Styling Placeholder Text |
The ::placeholder pseudo-element selector is used to style the placeholder text of HTML text input controls, allowing customization of size, color, horizontal alignment, and more. |
|
selectorA selectorB Select All Descendant Elements |
The descendant combinator (whitespace) selects all target descendant elements of the element matched by the preceding selector. It uses the "selectorA selectorB" syntax, where whitespace (" ", a space character) separates the two selectors and selects all elements matching "selectorB" that are descendants of an element matching "selectorA". |
|
selectorA > selectorB Select All Direct Child Elements |
The child combinator (>) selects all target direct child elements of the element matched by the preceding selector. It uses the "selectorA > selectorB" syntax, where the greater-than sign (>) separates the two selectors and selects all elements matching "selectorB" that are direct children of an element matching "selectorA". |
|
selectorA + selectorB Select the Next Sibling Element |
The next-sibling combinator (+) selects only the target sibling element that immediately follows the element matched by the preceding selector. It uses the "selectorA + selectorB" syntax, where the plus sign (+) separates the two selectors and selects an element matching "selectorB" when it immediately follows an element matching "selectorA". |
|
selectorA ~ selectorB Select All Subsequent Sibling Elements |
The subsequent-sibling combinator (~) selects all target sibling elements that follow the element matched by the preceding selector. It uses the "selectorA ~ selectorB" syntax, where the tilde (~) separates the two selectors and selects all elements matching "selectorB" that follow an element matching "selectorA". |
|
selectorA, selectorB, ... Multiple Selectors Separated by Commas |
When multiple selectors share the same declarations, they can be grouped into a comma-separated list. It uses the "selectorA, selectorB" syntax, where a comma (,) separates the selectors and "selectorA" and "selectorB" share the same declarations. |