The Difference and Application of CSS Pseudo-classes and Pseudo-elements
I. Basic Concepts
Pseudo-classes and Pseudo-elements are both extensions of CSS selectors, but their purposes and syntax are fundamentally different.
II. Detailed Analysis of Pseudo-classes
-
Definition
Pseudo-classes are used to select elements in a specific state (such as hover, focus, first child, etc.). They are based on the state of existing elements in the document tree. -
Syntax Features
Use a single colon:(the CSS3 specification allows pseudo-classes to use either a single or double colon, but a single colon is typically used for compatibility). -
Common Pseudo-class Examples
- Dynamic Interaction:
:hover(hover),:active(active),:focus(focus) - Structural Relationship:
:first-child(first child),:nth-child(n)(nth child) - Form-related:
:checked(checked state),:disabled(disabled state)
- Dynamic Interaction:
-
Code Demonstration
/* Change link color to red on hover */ a:hover { color: red; } /* Background color for odd table rows */ tr:nth-child(odd) { background: #f0f0f0; }
III. Detailed Analysis of Pseudo-elements
-
Definition
Pseudo-elements are used to create abstract elements that are not in the document tree (such as the first letter, decorative content before/after). They act as inserting virtual elements based on the original element. -
Syntax Evolution
The CSS3 specification clearly states that pseudo-elements should use a double colon::(e.g.,::before), but the earlier single-colon notation is still supported by browsers for compatibility. -
Common Pseudo-element Examples
::before/::after: Insert content before/after the element's content::first-letter: Select the first letter::selection: Select the portion highlighted by the user
-
Code Demonstration
/* Add a decorative line before paragraphs */ p::before { content: "→"; margin-right: 5px; } /* Drop cap effect */ p::first-letter { font-size: 2em; float: left; }
IV. Core Differences Comparison
-
Fundamental Difference
- Pseudo-classes select existing elements in a specific state.
- Pseudo-elements create and select non-existent virtual elements.
-
Syntax Standard
- Pseudo-classes are recommended to use
:. - Pseudo-elements are recommended to use
::.
- Pseudo-classes are recommended to use
-
DOM Structure Representation
- Pseudo-classes do not affect the DOM structure.
- Pseudo-elements can be seen in developer tools (but are not in the HTML source code).
V. Comprehensive Application Example
/* Add custom numbering to each list item */
li::before {
content: counter(list) ".";
counter-increment: list;
color: blue;
}
/* Change the number color on hover */
li:hover::before {
color: red;
}
VI. Common Mistakes and Reminders
- Pseudo-elements must be used with the
contentproperty (except for special cases like::selection). - Pseudo-classes can be chained (e.g.,
a:hover:focus), while pseudo-elements usually appear only at the end of the selector. - Older browsers may not support the double-colon syntax, so the notation should be chosen based on compatibility requirements.