Pseudo-classes are selectors that target elements in a specific state or position:
/* Main pseudo-classes */
a:hover { color: red; }
input:focus { border: 2px solid blue; }
li:first-child { font-weight: bold; }
p:nth-child(2n) { background: #f0f0f0; }Pseudo-classes are like smart filters that select elements not by their type or class, but by state, position, or other characteristics! 🎯
/* Single colon for pseudo-classes */
element:pseudo-class {
property: value;
}
/* Can be combined */
element.class:pseudo-class {
property: value;
}React to user actions:
/* Hover */
a:hover {
color: #007bff;
text-decoration: underline;
}
/* Focus */
input:focus {
outline: none;
border: 2px solid #007bff;
box-shadow: 0 0 5px rgba(0, 123, 255, 0.3);
}
/* Active state */
button:active {
transform: scale(0.98);
background: #0056b3;
}
/* Visited links */
a:visited {
color: purple;
}Select elements by position:
/* First and last */
li:first-child {
margin-top: 0;
font-weight: bold;
}
li:last-child {
margin-bottom: 0;
border-bottom: none;
}
/* By number */
tr:nth-child(odd) {
background: #f9f9f9;
}
tr:nth-child(even) {
background: white;
}
/* Every third */
.item:nth-child(3n) {
margin-right: 0;
}Work with element type:
/* First element of type */
h2:first-of-type {
margin-top: 0;
color: #333;
}
/* Last element of type */
p:last-of-type {
margin-bottom: 0;
}
/* Only element of type */
img:only-of-type {
display: block;
margin: 0 auto;
}Excludes elements from selection:
/* All buttons except disabled */
button:not(:disabled) {
cursor: pointer;
opacity: 1;
}
/* All links except external */
a:not([href^="http"]) {
color: #007bff;
}
/* All elements except first */
.menu-item:not(:first-child) {
margin-left: 20px;
}For form elements:
/* Required fields */
input:required {
border-left: 3px solid red;
}
/* Valid fields */
input:valid {
border-color: green;
}
/* Invalid fields */
input:invalid {
border-color: red;
background: #ffe6e6;
}
/* Disabled elements */
input:disabled {
opacity: 0.5;
cursor: not-allowed;
}
/* Checked checkboxes */
input:checked + label {
font-weight: bold;
color: green;
}/* Elements in specific language */
p:lang(en) {
font-family: "Times New Roman", serif;
}
p:lang(ru) {
font-family: "Arial", sans-serif;
}/* Target element (by anchor) */
:target {
background: yellow;
padding: 10px;
}
/* Links to current page */
a[href="#"]:hover {
cursor: default;
text-decoration: none;
}.nav-item {
padding: 10px 15px;
transition: all 0.3s ease;
}
.nav-item:hover {
background: #f0f0f0;
transform: translateY(-2px);
}
.nav-item:first-child {
border-radius: 5px 0 0 5px;
}
.nav-item:last-child {
border-radius: 0 5px 5px 0;
}table tr:nth-child(even) {
background: #f8f9fa;
}
table tr:hover {
background: #e9ecef;
cursor: pointer;
}
table th:first-child,
table td:first-child {
padding-left: 20px;
}.form-group input:focus {
border-color: #007bff;
box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25);
}
.form-group input:valid {
border-color: #28a745;
}
.form-group input:invalid:not(:focus) {
border-color: #dc3545;
}/* Multiple pseudo-classes */
a:hover:not(:visited) {
color: #ff6b6b;
}
/* With other selectors */
.button:hover:not(:disabled) {
background: #0056b3;
transform: translateY(-1px);
}
/* Nested states */
.card:hover .card-title:first-child {
color: #007bff;
}| Pseudo-classes | Pseudo-elements |
|---|---|
Single colon : | Double colon :: |
| Element state | Part of element |
:hover, :focus | ::before, ::after |
Most pseudo-classes are supported by all modern browsers:
:hover, :focus, :active — full support:nth-child(), :first-child — IE9+:not() — IE9+ (limited):focus-visible — modern browsers:hover, :focus 🖱️:first-child, :nth-child() 📋:valid, :invalid ✅Pseudo-classes are powerful tools for creating interactive and adaptive interfaces:
:hover, :focus, :active:first-child, :nth-child(), :last-child:valid, :invalid, :required:not(), :targetUse them to create lively and responsive interfaces! 🎨