Master advanced selectors for state-based and virtual element styling
Pseudo-classes select elements based on their state or position, while pseudo-elements let you style specific parts of elements or create virtual elements.
:hover::beforeThese pseudo-classes respond to user interactions.
/* Style on mouse hover */
button:hover {
background-color: #667eea;
transform: scale(1.05);
}
a:hover {
color: red;
text-decoration: underline;
}
/* Style when being clicked */
button:active {
transform: scale(0.95);
box-shadow: inset 0 2px 5px rgba(0,0,0,0.3);
}
/* Style when focused (keyboard/click) */
input:focus {
outline: 2px solid #667eea;
border-color: #667eea;
}
/* Better focus with focus-visible (keyboard only) */
button:focus-visible {
outline: 3px solid #667eea;
outline-offset: 2px;
}
Hover, click, and focus on the button to see effects
/* Visited links */
a:visited {
color: purple;
}
/* Link states in order (LVHA) */
a:link { color: blue; }
a:visited { color: purple; }
a:hover { color: red; }
a:active { color: orange; }
Select elements based on their position in the document structure.
/* First child */
li:first-child {
font-weight: bold;
}
/* Last child */
li:last-child {
border-bottom: none;
}
/* Only child (no siblings) */
p:only-child {
text-align: center;
}
/* Nth child */
li:nth-child(2) { /* Second item */
color: red;
}
li:nth-child(odd) { /* 1st, 3rd, 5th... */
background: #f0f0f0;
}
li:nth-child(even) { /* 2nd, 4th, 6th... */
background: white;
}
li:nth-child(3n) { /* Every 3rd: 3, 6, 9... */
color: blue;
}
li:nth-child(3n+1) { /* 1st, 4th, 7th... */
color: green;
}
/* First of type */
p:first-of-type {
font-size: 1.2em;
}
/* Last of type */
p:last-of-type {
margin-bottom: 0;
}
/* Nth of type */
p:nth-of-type(2) {
color: blue;
}
/* Only of type */
p:only-of-type {
text-align: center;
}
Target form elements based on their state.
/* Enabled/Disabled */
input:enabled {
background: white;
}
input:disabled {
background: #f0f0f0;
cursor: not-allowed;
}
/* Checked (checkbox/radio) */
input:checked {
accent-color: #667eea;
}
input:checked + label {
font-weight: bold;
color: #667eea;
}
/* Required/Optional */
input:required {
border-left: 3px solid red;
}
input:optional {
border-left: 3px solid gray;
}
/* Valid/Invalid */
input:valid {
border-color: green;
}
input:invalid {
border-color: red;
}
/* In range / Out of range */
input:in-range {
border-color: green;
}
input:out-of-range {
border-color: red;
}
/* Placeholder shown */
input:placeholder-shown {
border-color: #ccc;
}
/* Not selector (exclude) */
li:not(.special) {
color: gray;
}
button:not(:disabled) {
cursor: pointer;
}
/* Empty (no children or text) */
div:empty {
display: none;
}
/* Root element (html) */
:root {
--primary-color: #667eea;
}
/* Target (URL anchor) */
:target {
background: yellow;
border: 2px solid orange;
}
Pseudo-elements create virtual elements or target specific parts of elements.
/* Add content before element */
h2::before {
content: "→ ";
color: #667eea;
font-weight: bold;
}
/* Add content after element */
h2::after {
content: " ←";
color: #764ba2;
}
/* Decorative elements */
.quote::before {
content: """;
font-size: 3em;
color: #667eea;
}
/* Icons using Unicode */
.link::before {
content: "🔗 ";
}
/* Required asterisk */
label.required::after {
content: " *";
color: red;
}
/* Style first letter (drop cap) */
p::first-letter {
font-size: 3em;
font-weight: bold;
color: #667eea;
float: left;
line-height: 1;
margin-right: 5px;
}
L orem ipsum dolor sit amet, consectetur adipiscing elit. This paragraph demonstrates the drop cap effect using the ::first-letter pseudo-element.
/* Style first line */
p::first-line {
font-weight: bold;
color: #667eea;
text-transform: uppercase;
}
/* Style selected text */
::selection {
background: #667eea;
color: white;
}
/* Firefox */
::-moz-selection {
background: #667eea;
color: white;
}
Try selecting this text! The selection highlight will be styled. (Note: In this demo, default selection styling applies, but in your CSS you can customize it)
/* Style placeholder text */
input::placeholder {
color: #999;
font-style: italic;
opacity: 0.7;
}
.quote {
position: relative;
padding: 30px 20px 20px 60px;
background: #f8f9fa;
border-left: 4px solid #667eea;
}
.quote::before {
content: """;
position: absolute;
top: 10px;
left: 10px;
font-size: 4rem;
color: #667eea;
opacity: 0.3;
font-family: Georgia, serif;
}
.tooltip {
position: relative;
cursor: help;
}
.tooltip::after {
content: attr(data-tooltip);
position: absolute;
bottom: 100%;
left: 50%;
transform: translateX(-50%);
padding: 8px 12px;
background: #333;
color: white;
border-radius: 4px;
white-space: nowrap;
opacity: 0;
pointer-events: none;
transition: opacity 0.3s;
}
.tooltip:hover::after {
opacity: 1;
}
.notification {
position: relative;
}
.notification::after {
content: attr(data-count);
position: absolute;
top: -8px;
right: -8px;
background: red;
color: white;
border-radius: 50%;
width: 20px;
height: 20px;
display: flex;
align-items: center;
justify-content: center;
font-size: 12px;
font-weight: bold;
}
/* Hide default checkbox */
input[type="checkbox"] {
display: none;
}
/* Custom checkbox */
input[type="checkbox"] + label::before {
content: "";
display: inline-block;
width: 20px;
height: 20px;
border: 2px solid #667eea;
border-radius: 3px;
margin-right: 8px;
vertical-align: middle;
}
/* Checked state */
input[type="checkbox"]:checked + label::before {
background: #667eea;
content: "✓";
color: white;
text-align: center;
line-height: 20px;
}
/* Classic clearfix */
.clearfix::after {
content: "";
display: table;
clear: both;
}
/* Alternating row colors */
tr:nth-child(odd) {
background: #f8f9fa;
}
tr:nth-child(even) {
background: white;
}
tr:hover {
background: #e3f2fd;
}
/* Hover effect on pseudo-element */
button:hover::before {
transform: scale(1.2);
}
/* First child's before element */
li:first-child::before {
content: "👑 ";
}
/* Checked state with after element */
input:checked + label::after {
content: " ✓";
color: green;
}
/* Focus visible with custom outline */
button:focus-visible::before {
content: "";
position: absolute;
inset: -4px;
border: 2px solid #667eea;
border-radius: inherit;
}
Q1: What's the difference between : and :: in selectors?
Q2: Which pseudo-class selects every other row?
Q3: What is required for ::before and ::after to work?
Q4: Which pseudo-class targets focused elements?
Q5: What does :not(.class) do?