Skip to main content
codingCourses
  • HTML
  • CSS
  • JavaScript
  • PHP
  • CSS Tutorial
  • CSS Selectors
  • CSS Properties
  • CSS Colors
  • Practical Tips
CSS
  • CSS Tutorial
  • How to Apply and Link CSS to HTML
  • currentcolor Keyword
    Represents the Current Element's Text Color
  • rem and em Units
    Relative Length Units for Font Size
  • CSS Specificity
    Selector Order, Rules, and Cheat Sheet
  • @charset
    Declaring Character Encoding for Stylesheets
  • !important Keyword
    Overriding Style Priority
  • CSS Variables
    How to Use Custom Properties in CSS
CSS Selectors
  • CSS Selector Reference
  • *
    Selects All Elements
  • element
    Select by Element Name
  • [attribute]
    Select Elements by Attribute Name
  • [attribute="value"]
    Select Elements by Attribute Name and Value
  • [attribute*="value"]
    Select Elements by Attribute Value Substring Match
  • [attribute~="value"]
    Select Elements by Attribute Value Word Match
  • [attribute^="value"]
    Select Elements by Attribute Value Prefix Match
  • [attribute$="value"]
    Select Elements by Attribute Value Suffix Match
  • Class Selector (.class)
    Select Elements by Class Name
  • ID Selector (#id)
    Select Elements by ID Value
  • :root
    Select the Root Element of the Document
  • :first-child
    Select the First Child
  • :last-child
    Select the Last Child
  • :nth-child(n)
    Select the nth child
  • :nth-last-child(n)
    Select the nth Child Element from the End
  • :nth-of-type(n)
    Select the nth Child of the Same Tag
  • :nth-last-of-type(n)
    Select the nth-to-last Child of the Same Tag
  • :first-of-type
    Select the First Child of the Same Tag
  • :last-of-type
    Select the Last Child of the Same Tag
  • :only-of-type
    Select the Only Child of Its Tag Name
  • :hover
    Select an Element While the Mouse Is Over It
  • :active
    Select an Element While the Mouse Is Pressed
  • :focus
    Selects an Element While It Has Focus
  • :focus-within
    Matches Focused Elements or Descendants
  • :checked
    Select Checked Radio Buttons & Checkboxes
  • :not(selectorX)
    Select Elements NOT Matching SelectorX
  • ::before
    Before Pseudo-Element Selector
  • ::after
    After Pseudo-Element Selector
  • ::selection
    Styling Selected Text
  • ::marker
    Select Automatically Generated List Markers
  • ::placeholder
    Styling Placeholder Text
  • selectorA selectorB
    Select All Descendant Elements
  • selectorA > selectorB
    Select All Direct Child Elements
  • selectorA + selectorB
    Select the Next Sibling Element
  • selectorA ~ selectorB
    Select All Subsequent Sibling Elements
  • selectorA, selectorB, ...
    Multiple Selectors Separated by Commas
CSS Properties
  • CSS Property Reference
  • appearance
    Control Default Browser UI Appearance
  • aspect-ratio
    Sets an Element's Width-to-Height Ratio
  • background-clip
    Specifying the Background Painting Area
  • background-origin
    Specifies the Background Positioning Area
  • border-radius
    Rounds the Corners of an Element
  • box-shadow
    Applying Shadow Effects to Element Boxes
  • box-sizing
    Defines the Element's Sizing Calculation Model
  • caret-color
    Change Caret Color
  • content
    Specify Pseudo-Element Content
  • cursor
    Styling the Mouse Cursor
  • font-kerning
    Use Font Kerning
  • letter-spacing
    Specifies Letter Spacing
  • line-height
    Setting Line Spacing for Text
  • object-fit
    How to Fit Images and Videos Perfectly
  • object-position
    Positioning with object-fit
  • pointer-events
    Controlling Pointer Interactions
  • text-align
    Setting Horizontal Alignment for Text
  • text-decoration
    Shorthand for Text Decoration Styling
  • text-decoration-line
    Specifies Line Decoration Types
  • text-decoration-color
    Specifies the Color of Decorations
  • text-decoration-style
    Specifies the Line Style of Text Decorations
  • text-decoration-thickness
    Specifies the Thickness of Decorations
  • Underline Position and Offset Properties
    Setting Underline Position and Spacing
  • text-underline-position
    Specifies the Positional Basis of Underlines
  • text-underline-offset
    Adjusting Underline Offset
  • text-emphasis
    Shorthand for Text Emphasis Marks
  • text-emphasis-style
    Specifies the Shape of Emphasis Marks
  • text-emphasis-color
    Specifies the Color of Emphasis Marks
  • text-emphasis-position
    Specifies the Position of Emphasis Marks
  • text-overflow
    Setting a Single-Line Text Ellipsis (...)
  • text-shadow
    Applying Shadow Effects to Text
  • text-size-adjust
    Controlling Mobile Text Size Adjustment
  • vertical-align
    Vertical Alignment: Inline Tags & Table-Cells
  • white-space
    Specifies White Space and Soft Wrapping
  • will-change
    A Hint for Rendering Optimization
  • word-break
    Rules for Breaking Words When Text Wraps
  • word-spacing
    Specifies Word Spacing
  • writing-mode
    Setting Text in Vertical Writing Mode
  • -webkit-line-clamp
    Setting a Multi-Line Text Ellipsis (…)
  • -webkit-text-stroke
    Styling the Stroke of Text
  • -webkit-text-stroke-color
    Specifies the Stroke Color of Text
  • -webkit-text-stroke-width
    Specifies the Stroke Width of Text
CSS Colors
  • Color Reference
  • Color Picker
    Pick a Color and Get its Code
  • Web Color Codes
    Color Palette for HTML and CSS
  • CSS Color Names
    Color Keywords for Web Design
CSS Practical Tips
  • Practical Tip Reference
  • Centering with CSS - Text
    Centering Text Made Easy
  • Centering with CSS - Images
    Centering Images Made Easy
  • Centering with CSS - Elements
    Centering Elements Made Easy
  • Text Ellipsis(...)
    Add ellipsis to long text content
  • List Marker Styling
    Examples and Customization Techniques
  • <details> Tag Styling
    Learn how to apply custom styles to <details>
  • <hr> Tag Styling
    Learn how to apply custom styles to <hr>
  • Custom Radio Buttons
    Styling Guide
  • Button Styles
    Examples of Stylish Button Designs
  • Scrollbar Customization
    Overview of CSS Scrollbars

CSS Selector Reference

CSS Next

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.

CSS Selector Reference
*
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.
CSS Next

codingCourses offers coding tutorials made for everyone.
All materials and examples are continuously reviewed to prevent errors; however, complete accuracy cannot be guaranteed.
Therefore, please carefully test and review all materials and examples to ensure they are free of errors, bugs, or vulnerabilities before relying on them.
They are available under the Creative Commons Attribution 4.0 International

Copyright © codingCourses. All Rights Reserved.