Pseudo-classes and pseudo-elements expand CSS from a static styling tool into a dynamic logic engine. They allow you to style elements based on user interaction, their specific parts, and their position in the document.
1Interactive States (Pseudo-classes)
Pseudo-classes (indicated by a single colon :) represent a specific state of an element.
- →`:hover`: Triggers when a mouse pointer enters the element's area. Essential for interactive buttons and links.
- →`:focus`: Activates when an element is selected (via click or Tab key). This is critical for accessibility so keyboard users know exactly where they are on the page.
- →`:active`: The 'pressed' state. Triggers while the user is actively clicking or pressing the element.
- →LVHA Rule: Links must be styled in the exact order:
Link,Visited,Hover,Active. This is a factual CSS cascading rule; breaking this order causes later states to fail.
2Structural Logic (Pseudo-elements)
Pseudo-elements (indicated by double colons ::) target specific sub-parts or create virtual ones natively in CSS.
- →`::before` & `::after`: These create 'virtual' children inside an element. They strictly require a
contentproperty (even if emptycontent: '') to render. They are perfect for injecting decorative icons, lines, or quotes without bloating the HTML. - →`::selection`: Allows you to customize the highlight color when a user selects text with their cursor.
- →`::placeholder`: Targets the hint text inside input fields, allowing separate styling from the user's typed text.
