Master advanced CSS selectors for styling element states and creating decorative elements. Learn pseudo-classes, pseudo-elements, and creative styling techniques.
Pseudo-classes and pseudo-elements are powerful CSS features that allow you to style elements based on their state or position, and create virtual elements without adding extra HTML. These selectors unlock advanced styling capabilities that keep your HTML clean while creating rich, interactive designs.
Pseudo-classes target elements in specific states (like :hover, :focus, :active) or positions (like :first-child, :nth-child), while pseudo-elements create virtual elements (::before, ::after) that you can style and position. Together, they provide the foundation for creating sophisticated user interfaces with minimal markup.
This lesson will teach you to master these advanced selectors, from basic state selectors to complex structural pseudo-classes and creative pseudo-element techniques. You'll learn to create interactive elements, decorative graphics, and complex layouts while maintaining clean, semantic HTML.
Pseudo-classes select elements based on their state or position in the document, enabling dynamic styling without JavaScript.
User Action Pseudo-classes:
1/* Basic interaction states */
2.button {
3 background: #3b82f6;
4 color: white;
5 padding: 12px 24px;
6 border: none;
7 border-radius: 8px;
8 cursor: pointer;
9 transition: all 0.3s ease;
10}
11
12.button:hover {
13 background: #2563eb;
14 transform: translateY(-2px);
15 box-shadow: 0 4px 12px rgba(59, 130, 246, 0.4);
16}
17
18.button:focus {
19 outline: none;
20 box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.5);
21}
22
23.button:active {
24 transform: translateY(0);
25 background: #1e40af;
26}
27
28.button:disabled {
29 opacity: 0.5;
30 cursor: not-allowed;
31 background: #94a3b8;
32}
33
34.button:disabled:hover {
35 transform: none;
36 box-shadow: none;
37}Form State Pseudo-classes:
1/* Input field states */
2.input-field {
3 padding: 12px;
4 border: 2px solid #e5e7eb;
5 border-radius: 8px;
6 font-size: 16px;
7 transition: all 0.2s ease;
8}
9
10.input-field:focus {
11 border-color: #3b82f6;
12 box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
13 outline: none;
14}
15
16.input-field:valid {
17 border-color: #10b981;
18}
19
20.input-field:invalid {
21 border-color: #ef4444;
22}
23
24.input-field:placeholder-shown {
25 border-color: #d1d5db;
26}
27
28.input-field:required:valid {
29 background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20' fill='%2310b981'%3E%3Cpath fill-rule='evenodd' d='M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z' clip-rule='evenodd'/%3E%3C/svg%3E");
30 background-position: right 12px center;
31 background-repeat: no-repeat;
32 background-size: 20px;
33}
34
35/* Checkbox and radio states */
36input[type="checkbox"]:checked + label {
37 color: #3b82f6;
38 font-weight: 600;
39}
40
41input[type="radio"]:checked + label::before {
42 background: #3b82f6;
43 transform: scale(1);
44}Link State Pseudo-classes:
1/* Link states - remember LVHA order */
2a:link {
3 color: #3b82f6;
4 text-decoration: none;
5}
6
7a:visited {
8 color: #7c3aed;
9}
10
11a:hover {
12 color: #1e40af;
13 text-decoration: underline;
14}
15
16a:active {
17 color: #1e3a8a;
18}
19
20/* Advanced link styling */
21.nav-link {
22 position: relative;
23 color: #64748b;
24 transition: color 0.3s ease;
25}
26
27.nav-link:hover {
28 color: #1e293b;
29}
30
31.nav-link::after {
32 content: '';
33 position: absolute;
34 bottom: -4px;
35 left: 0;
36 width: 0;
37 height: 2px;
38 background: #3b82f6;
39 transition: width 0.3s ease;
40}
41
42.nav-link:hover::after {
43 width: 100%;
44}
45
46/* Target pseudo-class */
47.section:target {
48 background: #fef3c7;
49 animation: highlight 2s ease-out;
50}
51
52@keyframes highlight {
53 from { background: #fbbf24; }
54 to { background: transparent; }
55}Root and Language Pseudo-classes:
1/* Root element styling */
2:root {
3 --primary-color: #3b82f6;
4 --secondary-color: #64748b;
5 --background: #f8fafc;
6 --text: #1e293b;
7 font-size: 16px;
8}
9
10/* Language-specific styling */
11:lang(en) {
12 quotes: '"' '"' ''' ''';
13}
14
15:lang(fr) {
16 quotes: '« ' ' »';
17}
18
19q:before {
20 content: open-quote;
21}
22
23q:after {
24 content: close-quote;
25}
26
27/* Direction pseudo-classes */
28:dir(ltr) .arrow {
29 transform: rotate(0deg);
30}
31
32:dir(rtl) .arrow {
33 transform: rotate(180deg);
34}Media and Feature Pseudo-classes:
1/* Preference pseudo-classes */
2@media (prefers-color-scheme: dark) {
3 :root {
4 --background: #1e293b;
5 --text: #f8fafc;
6 }
7}
8
9@media (prefers-reduced-motion: reduce) {
10 * {
11 animation-duration: 0.01ms !important;
12 transition-duration: 0.01ms !important;
13 }
14}
15
16/* Print-specific styling */
17@media print {
18 .no-print {
19 display: none !important;
20 }
21
22 a:after {
23 content: " (" attr(href) ")";
24 color: #666;
25 font-size: 0.8em;
26 }
27}Structural pseudo-classes select elements based on their position within their parent, enabling pattern-based styling.
Child Position Selectors:
1/* First and last child */
2.list-item:first-child {
3 border-top: none;
4 margin-top: 0;
5}
6
7.list-item:last-child {
8 border-bottom: none;
9 margin-bottom: 0;
10}
11
12.list-item:only-child {
13 border: 2px solid #3b82f6;
14 background: #eff6ff;
15}
16
17/* Type-specific first/last */
18h2:first-of-type {
19 color: #1e40af;
20 font-size: 2.5rem;
21}
22
23p:last-of-type {
24 margin-bottom: 0;
25}
26
27img:only-of-type {
28 display: block;
29 margin: 2rem auto;
30 border-radius: 12px;
31}nth-child Patterns:
1/* Odd and even rows */
2.table-row:nth-child(odd) {
3 background: #f8fafc;
4}
5
6.table-row:nth-child(even) {
7 background: white;
8}
9
10/* Every third item */
11.grid-item:nth-child(3n) {
12 margin-right: 0;
13}
14
15.grid-item:nth-child(3n+1) {
16 clear: left;
17}
18
19/* First 3 items */
20.feature:nth-child(-n+3) {
21 border: 2px solid #10b981;
22 background: #ecfdf5;
23}
24
25/* Last 2 items */
26.feature:nth-last-child(-n+2) {
27 opacity: 0.7;
28}
29
30/* Every 4th item starting from 2nd */
31.card:nth-child(4n+2) {
32 transform: scale(1.05);
33 z-index: 2;
34}
35
36/* Complex patterns */
37.gallery-item:nth-child(8n+1),
38.gallery-item:nth-child(8n+2),
39.gallery-item:nth-child(8n+7),
40.gallery-item:nth-child(8n+8) {
41 height: 300px;
42}
43
44.gallery-item:nth-child(8n+3),
45.gallery-item:nth-child(8n+4),
46.gallery-item:nth-child(8n+5),
47.gallery-item:nth-child(8n+6) {
48 height: 200px;
49}nth-of-type Patterns:
1/* Style specific types by position */
2h3:nth-of-type(1) {
3 color: #dc2626;
4 font-size: 1.8rem;
5}
6
7h3:nth-of-type(2) {
8 color: #ea580c;
9 font-size: 1.6rem;
10}
11
12h3:nth-of-type(3) {
13 color: #ca8a04;
14 font-size: 1.4rem;
15}
16
17/* Every other paragraph */
18p:nth-of-type(2n) {
19 font-style: italic;
20 padding-left: 2rem;
21 border-left: 4px solid #3b82f6;
22}
23
24/* First and last of each type */
25article h2:first-of-type {
26 margin-top: 0;
27}
28
29article p:last-of-type {
30 margin-bottom: 2rem;
31 font-weight: 500;
32}Empty and Content Selectors:
1/* Empty elements */
2.message:empty {
3 display: none;
4}
5
6.container:empty::before {
7 content: "No content available";
8 color: #64748b;
9 font-style: italic;
10 display: block;
11 text-align: center;
12 padding: 2rem;
13}
14
15/* Elements with specific content */
16.status[data-status="success"]:not(:empty) {
17 background: #dcfce7;
18 color: #166534;
19 border: 1px solid #bbf7d0;
20}
21
22.status[data-status="error"]:not(:empty) {
23 background: #fef2f2;
24 color: #991b1b;
25 border: 1px solid #fecaca;
26}
27
28/* Has pseudo-class (future CSS) */
29.card:has(img) {
30 display: grid;
31 grid-template-columns: 200px 1fr;
32 gap: 1rem;
33}
34
35.form:has(:invalid) {
36 border: 2px solid #ef4444;
37}
38
39.form:has(:invalid) .submit-btn {
40 opacity: 0.5;
41 pointer-events: none;
42}Complex Structural Combinations:
1/* Advanced combinations */
2.sidebar li:nth-child(odd):hover {
3 background: linear-gradient(90deg, #3b82f6, #8b5cf6);
4 color: white;
5}
6
7.article:nth-of-type(3n+1):first-of-type {
8 grid-column: 1 / -1;
9 font-size: 1.2rem;
10}
11
12.card:nth-child(4n+1):not(:last-child) {
13 margin-bottom: 3rem;
14}
15
16/* Responsive structural styling */
17@media (min-width: 768px) {
18 .grid-item:nth-child(3n):not(:nth-child(6n)) {
19 margin-right: 1rem;
20 }
21
22 .grid-item:nth-child(6n) {
23 margin-right: 0;
24 }
25}
26
27/* Print-specific structural styling */
28@media print {
29 .chapter:nth-of-type(odd) {
30 page-break-before: always;
31 }
32
33 .footnote:nth-child(n+10) {
34 font-size: 0.8rem;
35 }
36}Pseudo-elements create virtual elements that you can style and position, adding decorative and functional content without extra HTML.
::before and ::after Basics:
1/* Basic pseudo-element structure */
2.element::before,
3.element::after {
4 content: ''; /* Required - can be text or empty */
5 display: block; /* Or inline, inline-block, etc. */
6 position: absolute; /* Often used for positioning */
7}
8
9/* Text content examples */
10.quote::before {
11 content: open-quote;
12 font-size: 4rem;
13 color: #3b82f6;
14 line-height: 1;
15}
16
17.quote::after {
18 content: close-quote;
19 font-size: 4rem;
20 color: #3b82f6;
21 line-height: 1;
22}
23
24.required::after {
25 content: ' *';
26 color: #ef4444;
27}
28
29.external-link::after {
30 content: ' ↗';
31 color: #64748b;
32 font-size: 0.9em;
33}
34
35/* Attribute content */
36.tooltip {
37 position: relative;
38 border-bottom: 1px dotted #3b82f6;
39}
40
41.tooltip::after {
42 content: attr(data-tooltip);
43 position: absolute;
44 bottom: 100%;
45 left: 50%;
46 transform: translateX(-50%);
47 background: #1e293b;
48 color: white;
49 padding: 8px 12px;
50 border-radius: 6px;
51 font-size: 0.9rem;
52 white-space: nowrap;
53 opacity: 0;
54 pointer-events: none;
55 transition: opacity 0.3s ease;
56}
57
58.tooltip:hover::after {
59 opacity: 1;
60}Decorative Elements and Shapes:
1/* Decorative borders and dividers */
2.section-divider {
3 position: relative;
4 text-align: center;
5 margin: 3rem 0;
6}
7
8.section-divider::before {
9 content: '';
10 position: absolute;
11 top: 50%;
12 left: 0;
13 right: 0;
14 height: 1px;
15 background: linear-gradient(90deg, transparent, #d1d5db, transparent);
16 z-index: 1;
17}
18
19.section-divider span {
20 background: white;
21 padding: 0 2rem;
22 color: #64748b;
23 position: relative;
24 z-index: 2;
25}
26
27/* Decorative corners */
28.card-fancy {
29 position: relative;
30 background: white;
31 padding: 2rem;
32 border-radius: 12px;
33}
34
35.card-fancy::before,
36.card-fancy::after {
37 content: '';
38 position: absolute;
39 width: 20px;
40 height: 20px;
41 border: 3px solid #3b82f6;
42}
43
44.card-fancy::before {
45 top: 10px;
46 left: 10px;
47 border-right: none;
48 border-bottom: none;
49}
50
51.card-fancy::after {
52 bottom: 10px;
53 right: 10px;
54 border-left: none;
55 border-top: none;
56}
57
58/* Geometric shapes */
59.triangle-down::after {
60 content: '';
61 position: absolute;
62 top: 100%;
63 left: 50%;
64 transform: translateX(-50%);
65 width: 0;
66 height: 0;
67 border: 15px solid transparent;
68 border-top-color: #3b82f6;
69}
70
71.circle-indicator::before {
72 content: '';
73 position: absolute;
74 top: -5px;
75 right: -5px;
76 width: 12px;
77 height: 12px;
78 background: #ef4444;
79 border-radius: 50%;
80 border: 2px solid white;
81}Icons and Symbols:
1/* CSS-only icons using pseudo-elements */
2.hamburger-menu {
3 position: relative;
4 width: 30px;
5 height: 4px;
6 background: #1e293b;
7 cursor: pointer;
8 transition: all 0.3s ease;
9}
10
11.hamburger-menu::before,
12.hamburger-menu::after {
13 content: '';
14 position: absolute;
15 left: 0;
16 width: 100%;
17 height: 4px;
18 background: #1e293b;
19 transition: all 0.3s ease;
20}
21
22.hamburger-menu::before {
23 top: -10px;
24}
25
26.hamburger-menu::after {
27 bottom: -10px;
28}
29
30.hamburger-menu.open {
31 background: transparent;
32}
33
34.hamburger-menu.open::before {
35 top: 0;
36 transform: rotate(45deg);
37}
38
39.hamburger-menu.open::after {
40 bottom: 0;
41 transform: rotate(-45deg);
42}
43
44/* Plus/cross icon */
45.plus-icon {
46 position: relative;
47 width: 20px;
48 height: 20px;
49}
50
51.plus-icon::before,
52.plus-icon::after {
53 content: '';
54 position: absolute;
55 background: #3b82f6;
56 transition: transform 0.3s ease;
57}
58
59.plus-icon::before {
60 top: 50%;
61 left: 0;
62 right: 0;
63 height: 2px;
64 transform: translateY(-50%);
65}
66
67.plus-icon::after {
68 top: 0;
69 bottom: 0;
70 left: 50%;
71 width: 2px;
72 transform: translateX(-50%);
73}
74
75.plus-icon.close::before,
76.plus-icon.close::after {
77 transform: translateX(-50%) translateY(-50%) rotate(45deg);
78 top: 50%;
79 left: 50%;
80 width: 20px;
81 height: 2px;
82}
83
84/* Arrow icons */
85.arrow-right::after {
86 content: '';
87 display: inline-block;
88 width: 8px;
89 height: 8px;
90 border-right: 2px solid currentColor;
91 border-bottom: 2px solid currentColor;
92 transform: rotate(-45deg);
93 margin-left: 0.5rem;
94}
95
96.dropdown::after {
97 content: '';
98 display: inline-block;
99 width: 0;
100 height: 0;
101 margin-left: 0.5rem;
102 vertical-align: middle;
103 border-top: 4px solid currentColor;
104 border-right: 4px solid transparent;
105 border-left: 4px solid transparent;
106 transition: transform 0.3s ease;
107}
108
109.dropdown.open::after {
110 transform: rotate(180deg);
111}Advanced Layout Techniques:
1/* Overlay effects */
2.image-overlay {
3 position: relative;
4 overflow: hidden;
5 border-radius: 12px;
6}
7
8.image-overlay::before {
9 content: '';
10 position: absolute;
11 top: 0;
12 left: 0;
13 right: 0;
14 bottom: 0;
15 background: linear-gradient(
16 135deg,
17 rgba(59, 130, 246, 0.8),
18 rgba(139, 92, 246, 0.8)
19 );
20 opacity: 0;
21 transition: opacity 0.3s ease;
22 z-index: 1;
23}
24
25.image-overlay:hover::before {
26 opacity: 1;
27}
28
29.image-overlay::after {
30 content: 'View Details';
31 position: absolute;
32 top: 50%;
33 left: 50%;
34 transform: translate(-50%, -50%);
35 color: white;
36 font-weight: bold;
37 font-size: 1.1rem;
38 opacity: 0;
39 transition: opacity 0.3s ease 0.1s;
40 z-index: 2;
41}
42
43.image-overlay:hover::after {
44 opacity: 1;
45}
46
47/* Clearfix using pseudo-elements */
48.clearfix::after {
49 content: '';
50 display: table;
51 clear: both;
52}
53
54/* Custom list styling */
55.custom-list {
56 list-style: none;
57 padding: 0;
58}
59
60.custom-list li {
61 position: relative;
62 padding-left: 2rem;
63 margin-bottom: 0.5rem;
64}
65
66.custom-list li::before {
67 content: '✨';
68 position: absolute;
69 left: 0;
70 color: #3b82f6;
71 font-weight: bold;
72}
73
74/* Progress indicators */
75.progress-step {
76 position: relative;
77 padding-left: 3rem;
78}
79
80.progress-step::before {
81 content: counter(step-counter);
82 counter-increment: step-counter;
83 position: absolute;
84 left: 0;
85 top: 0;
86 width: 2rem;
87 height: 2rem;
88 background: #3b82f6;
89 color: white;
90 border-radius: 50%;
91 display: flex;
92 align-items: center;
93 justify-content: center;
94 font-weight: bold;
95 font-size: 0.9rem;
96}
97
98.progress-container {
99 counter-reset: step-counter;
100}Master advanced pseudo-element techniques for creating complex layouts, animations, and interactive elements.
Multi-layer Pseudo-element Effects:
1/* Layered shadow effects */
2.shadow-card {
3 position: relative;
4 background: white;
5 padding: 2rem;
6 border-radius: 16px;
7}
8
9.shadow-card::before {
10 content: '';
11 position: absolute;
12 top: 0;
13 left: 0;
14 right: 0;
15 bottom: 0;
16 background: inherit;
17 border-radius: inherit;
18 filter: blur(20px);
19 opacity: 0.3;
20 z-index: -2;
21 transform: translateY(10px) scale(0.95);
22}
23
24.shadow-card::after {
25 content: '';
26 position: absolute;
27 top: 0;
28 left: 0;
29 right: 0;
30 bottom: 0;
31 background: linear-gradient(135deg, rgba(59, 130, 246, 0.1), rgba(139, 92, 246, 0.1));
32 border-radius: inherit;
33 z-index: -1;
34 transform: translateY(-2px);
35}
36
37/* Animated pseudo-elements */
38.loading-button {
39 position: relative;
40 background: #3b82f6;
41 color: white;
42 border: none;
43 padding: 12px 24px;
44 border-radius: 8px;
45 cursor: pointer;
46 overflow: hidden;
47}
48
49.loading-button.loading::after {
50 content: '';
51 position: absolute;
52 top: 0;
53 left: -100%;
54 width: 100%;
55 height: 100%;
56 background: linear-gradient(
57 90deg,
58 transparent,
59 rgba(255, 255, 255, 0.4),
60 transparent
61 );
62 animation: shimmer 1.5s infinite;
63}
64
65@keyframes shimmer {
66 to {
67 left: 100%;
68 }
69}
70
71/* Morphing shapes */
72.morph-element {
73 position: relative;
74 width: 100px;
75 height: 100px;
76 background: #3b82f6;
77 border-radius: 50px;
78 transition: all 0.5s ease;
79}
80
81.morph-element::before,
82.morph-element::after {
83 content: '';
84 position: absolute;
85 width: 100%;
86 height: 100%;
87 background: inherit;
88 border-radius: inherit;
89 transition: all 0.5s ease;
90}
91
92.morph-element::before {
93 transform: rotate(0deg);
94}
95
96.morph-element::after {
97 transform: rotate(0deg);
98}
99
100.morph-element:hover {
101 border-radius: 0;
102 transform: rotate(45deg);
103}
104
105.morph-element:hover::before {
106 transform: rotate(45deg) scale(0.7);
107 border-radius: 50%;
108}
109
110.morph-element:hover::after {
111 transform: rotate(-45deg) scale(0.5);
112 border-radius: 50%;
113 background: #10b981;
114}Content Generation and Counters:
1/* Auto-numbering with counters */
2.chapter {
3 counter-reset: section-counter;
4}
5
6.chapter h2 {
7 counter-increment: chapter-counter;
8}
9
10.chapter h2::before {
11 content: "Chapter " counter(chapter-counter) ": ";
12 color: #3b82f6;
13 font-weight: normal;
14}
15
16.section {
17 counter-increment: section-counter;
18}
19
20.section h3::before {
21 content: counter(chapter-counter) "." counter(section-counter) " ";
22 color: #64748b;
23}
24
25/* Complex counter styling */
26.outline {
27 counter-reset: item;
28 padding-left: 0;
29}
30
31.outline li {
32 display: block;
33 margin-bottom: 0.5rem;
34}
35
36.outline li::before {
37 content: counters(item, ".") " ";
38 counter-increment: item;
39 color: #3b82f6;
40 font-weight: bold;
41 margin-right: 0.5rem;
42}
43
44.outline ol {
45 counter-reset: item;
46}
47
48/* Dynamic content from attributes */
49.product-card::after {
50 content: "$" attr(data-price);
51 position: absolute;
52 top: 10px;
53 right: 10px;
54 background: #10b981;
55 color: white;
56 padding: 4px 8px;
57 border-radius: 4px;
58 font-weight: bold;
59 font-size: 0.9rem;
60}
61
62.status-indicator::before {
63 content: attr(data-status);
64 text-transform: uppercase;
65 font-size: 0.8rem;
66 font-weight: bold;
67 padding: 2px 6px;
68 border-radius: 3px;
69 margin-right: 0.5rem;
70}
71
72.status-indicator[data-status="active"]::before {
73 background: #dcfce7;
74 color: #166534;
75}
76
77.status-indicator[data-status="pending"]::before {
78 background: #fef3c7;
79 color: #92400e;
80}
81
82.status-indicator[data-status="error"]::before {
83 background: #fef2f2;
84 color: #991b1b;
85}Responsive Pseudo-elements:
1/* Responsive pseudo-element behavior */
2.responsive-decoration::before {
3 content: '🎨';
4 font-size: 2rem;
5 position: absolute;
6 top: -10px;
7 right: -10px;
8}
9
10@media (max-width: 768px) {
11 .responsive-decoration::before {
12 content: '✨';
13 font-size: 1.5rem;
14 position: static;
15 display: inline-block;
16 margin-right: 0.5rem;
17 }
18}
19
20/* Hide pseudo-elements on mobile */
21@media (max-width: 480px) {
22 .desktop-only::before,
23 .desktop-only::after {
24 display: none;
25 }
26}
27
28/* Print-specific pseudo-elements */
29@media print {
30 .external-link[href^="http"]::after {
31 content: " (" attr(href) ")";
32 font-size: 0.8em;
33 color: #666;
34 }
35
36 .page-break::after {
37 content: "";
38 display: block;
39 page-break-after: always;
40 }
41}Performance Considerations:
1/* Efficient pseudo-element animations */
2.efficient-hover::before {
3 content: '';
4 position: absolute;
5 inset: 0;
6 background: rgba(59, 130, 246, 0.1);
7 transform: scale(0);
8 transition: transform 0.3s ease;
9 will-change: transform;
10}
11
12.efficient-hover:hover::before {
13 transform: scale(1);
14}
15
16/* Avoid expensive properties in pseudo-elements */
17.optimized-decoration::after {
18 content: '';
19 position: absolute;
20 width: 20px;
21 height: 20px;
22 background: #3b82f6;
23 border-radius: 50%;
24 /* Use transform instead of top/left changes */
25 transform: translateX(100px);
26 transition: transform 0.3s ease;
27}
28
29.optimized-decoration:hover::after {
30 transform: translateX(120px);
31}
32
33/* Minimize DOM impact */
34.minimal-pseudo {
35 position: relative;
36}
37
38.minimal-pseudo::before {
39 content: '';
40 position: absolute;
41 inset: 0;
42 border: 2px solid currentColor;
43 border-radius: inherit;
44 opacity: 0;
45 transition: opacity 0.2s ease;
46}
47
48.minimal-pseudo:focus::before {
49 opacity: 1;
50}
51
52/* Clean up animations */
53.pseudo-animation {
54 position: relative;
55}
56
57.pseudo-animation::after {
58 content: '';
59 position: absolute;
60 inset: 0;
61 background: linear-gradient(45deg, transparent, rgba(255,255,255,0.1), transparent);
62 transform: translateX(-100%);
63 transition: transform 0.6s ease;
64}
65
66.pseudo-animation:hover::after {
67 transform: translateX(100%);
68}
69
70.pseudo-animation:not(:hover)::after {
71 transition-delay: 0.1s;
72}A comprehensive demonstration of pseudo-classes and pseudo-elements creating interactive cards, forms, navigation, and decorative elements.
1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
6 <title>Pseudo-classes & Pseudo-elements Mastery</title>
7 <style>
8 * {
9 margin: 0;
10 padding: 0;
11 box-sizing: border-box;
12 }
13
14 body {
15 font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
16 line-height: 1.6;
17 color: #1e293b;
18 background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
19 counter-reset: section-counter;
20 }
21
22 .container {
23 max-width: 1200px;
24 margin: 0 auto;
25 padding: 0 2rem;
26 }
27
28 /* Header with advanced navigation */
29 .header {
30 background: linear-gradient(135deg, #1e293b 0%, #334155 100%);
31 color: white;
32 padding: 1rem 0;
33 position: sticky;
34 top: 0;
35 z-index: 1000;
36 box-shadow: 0 4px 20px rgba(0,0,0,0.1);
37 }
38
39 .nav {
40 display: flex;
41 justify-content: space-between;
42 align-items: center;
43 }
44
45 .nav-brand {
46 font-size: 1.5rem;
47 font-weight: bold;
48 text-decoration: none;
49 color: white;
50 position: relative;
51 }
52
53 .nav-brand::after {
54 content: '✨';
55 position: absolute;
56 top: -8px;
57 right: -20px;
58 font-size: 1rem;
59 opacity: 0;
60 transform: scale(0);
61 transition: all 0.3s ease;
62 }
63
64 .nav-brand:hover::after {
65 opacity: 1;
66 transform: scale(1) rotate(360deg);
67 }
68
69 .nav-menu {
70 display: flex;
71 gap: 0;
72 list-style: none;
73 }
74
75 .nav-item {
76 position: relative;
77 }
78
79 .nav-link {
80 color: white;
81 text-decoration: none;
82 padding: 0.8rem 1.5rem;
83 display: block;
84 position: relative;
85 overflow: hidden;
86 transition: color 0.3s ease;
87 }
88
89 .nav-link::before {
90 content: '';
91 position: absolute;
92 top: 0;
93 left: -100%;
94 width: 100%;
95 height: 100%;
96 background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
97 transition: left 0.5s ease;
98 }
99
100 .nav-link:hover::before {
101 left: 100%;
102 }
103
104 .nav-link::after {
105 content: '';
106 position: absolute;
107 bottom: 0;
108 left: 50%;
109 width: 0;
110 height: 3px;
111 background: linear-gradient(90deg, #3b82f6, #8b5cf6);
112 transition: all 0.3s ease;
113 transform: translateX(-50%);
114 }
115
116 .nav-link:hover::after {
117 width: 100%;
118 }
119
120 /* Hero section */
121 .hero {
122 background: linear-gradient(135deg, #3b82f6 0%, #8b5cf6 100%);
123 color: white;
124 padding: 6rem 0;
125 text-align: center;
126 position: relative;
127 overflow: hidden;
128 }
129
130 .hero::before {
131 content: '';
132 position: absolute;
133 top: -50%;
134 right: -50%;
135 width: 200%;
136 height: 200%;
137 background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 50%);
138 animation: rotate 20s linear infinite;
139 }
140
141 @keyframes rotate {
142 to { transform: rotate(360deg); }
143 }
144
145 .hero-content {
146 position: relative;
147 z-index: 2;
148 }
149
150 .hero h1 {
151 font-size: clamp(2.5rem, 5vw, 4rem);
152 margin-bottom: 1rem;
153 position: relative;
154 }
155
156 .hero p {
157 font-size: 1.2rem;
158 margin-bottom: 2rem;
159 max-width: 600px;
160 margin-left: auto;
161 margin-right: auto;
162 opacity: 0.9;
163 }
164
165 /* Section styling with counters */
166 .section {
167 padding: 4rem 0;
168 counter-increment: section-counter;
169 }
170
171 .section-title {
172 font-size: 2.5rem;
173 text-align: center;
174 margin-bottom: 3rem;
175 color: #1e293b;
176 position: relative;
177 }
178
179 .section-title::before {
180 content: counter(section-counter, decimal-leading-zero) ". ";
181 color: #3b82f6;
182 font-size: 1.5rem;
183 position: absolute;
184 left: -4rem;
185 top: 50%;
186 transform: translateY(-50%);
187 }
188
189 .section-title::after {
190 content: '';
191 position: absolute;
192 bottom: -10px;
193 left: 50%;
194 width: 0;
195 height: 4px;
196 background: linear-gradient(90deg, #3b82f6, #8b5cf6);
197 border-radius: 2px;
198 transition: width 0.8s ease 0.2s;
199 transform: translateX(-50%);
200 }
201
202 .section-title.visible::after {
203 width: 120px;
204 }
205
206 /* Interactive cards */
207 .card-grid {
208 display: grid;
209 grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
210 gap: 2rem;
211 margin: 2rem 0;
212 }
213
214 .interactive-card {
215 background: white;
216 border-radius: 20px;
217 padding: 2rem;
218 box-shadow: 0 4px 20px rgba(0,0,0,0.08);
219 transition: all 0.4s ease;
220 position: relative;
221 overflow: hidden;
222 cursor: pointer;
223 }
224
225 .interactive-card::before {
226 content: '';
227 position: absolute;
228 top: 0;
229 left: -100%;
230 width: 100%;
231 height: 100%;
232 background: linear-gradient(
233 90deg,
234 transparent,
235 rgba(59, 130, 246, 0.1),
236 transparent
237 );
238 transition: left 0.6s ease;
239 }
240
241 .interactive-card:hover {
242 transform: translateY(-8px) scale(1.02);
243 box-shadow: 0 20px 40px rgba(59, 130, 246, 0.15);
244 }
245
246 .interactive-card:hover::before {
247 left: 100%;
248 }
249
250 .interactive-card:active {
251 transform: translateY(-4px) scale(1.01);
252 }
253
254 /* Card states */
255 .card-status {
256 position: absolute;
257 top: 1rem;
258 right: 1rem;
259 width: 12px;
260 height: 12px;
261 border-radius: 50%;
262 background: #94a3b8;
263 }
264
265 .interactive-card:hover .card-status {
266 background: #10b981;
267 box-shadow: 0 0 10px rgba(16, 185, 129, 0.5);
268 }
269
270 .interactive-card:active .card-status {
271 background: #ef4444;
272 animation: pulse 0.3s ease;
273 }
274
275 @keyframes pulse {
276 0%, 100% { transform: scale(1); }
277 50% { transform: scale(1.3); }
278 }
279
280 .card-icon {
281 width: 60px;
282 height: 60px;
283 border-radius: 16px;
284 display: flex;
285 align-items: center;
286 justify-content: center;
287 font-size: 1.5rem;
288 color: white;
289 margin-bottom: 1rem;
290 position: relative;
291 transition: all 0.4s ease;
292 background: linear-gradient(135deg, #3b82f6, #1e40af);
293 }
294
295 .card-icon::after {
296 content: '';
297 position: absolute;
298 inset: -3px;
299 border-radius: 20px;
300 background: linear-gradient(135deg, #3b82f6, #8b5cf6);
301 opacity: 0;
302 transition: opacity 0.4s ease;
303 z-index: -1;
304 }
305
306 .interactive-card:hover .card-icon {
307 transform: rotateY(180deg) scale(1.1);
308 }
309
310 .interactive-card:hover .card-icon::after {
311 opacity: 0.3;
312 animation: glow 2s ease-in-out infinite alternate;
313 }
314
315 @keyframes glow {
316 from { filter: blur(5px); }
317 to { filter: blur(15px); }
318 }
319
320 /* Advanced form styling */
321 .form-section {
322 background: white;
323 border-radius: 20px;
324 padding: 3rem;
325 box-shadow: 0 4px 20px rgba(0,0,0,0.08);
326 margin: 2rem 0;
327 }
328
329 .form-group {
330 margin-bottom: 2rem;
331 position: relative;
332 }
333
334 .form-label {
335 display: block;
336 margin-bottom: 0.5rem;
337 font-weight: 600;
338 color: #374151;
339 }
340
341 .form-label.required::after {
342 content: ' *';
343 color: #ef4444;
344 }
345
346 .form-input {
347 width: 100%;
348 padding: 1rem 1.5rem;
349 border: 2px solid #e5e7eb;
350 border-radius: 12px;
351 font-size: 1rem;
352 transition: all 0.3s ease;
353 background: #f9fafb;
354 }
355
356 .form-input:focus {
357 outline: none;
358 border-color: #3b82f6;
359 background: white;
360 box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
361 transform: translateY(-2px);
362 }
363
364 .form-input:valid {
365 border-color: #10b981;
366 background: #f0fdf4;
367 }
368
369 .form-input:invalid:not(:placeholder-shown) {
370 border-color: #ef4444;
371 background: #fef2f2;
372 }
373
374 /* Validation indicators */
375 .form-input:valid + .validation-icon::after {
376 content: '✓';
377 position: absolute;
378 right: 1rem;
379 top: 50%;
380 transform: translateY(-50%);
381 color: #10b981;
382 font-weight: bold;
383 }
384
385 .form-input:invalid:not(:placeholder-shown) + .validation-icon::after {
386 content: '✗';
387 position: absolute;
388 right: 1rem;
389 top: 50%;
390 transform: translateY(-50%);
391 color: #ef4444;
392 font-weight: bold;
393 }
394
395 .validation-icon {
396 position: absolute;
397 right: 0;
398 top: 0;
399 height: 100%;
400 width: 3rem;
401 pointer-events: none;
402 }
403
404 /* Custom checkbox styling */
405 .checkbox-group {
406 display: flex;
407 align-items: center;
408 margin-bottom: 1rem;
409 cursor: pointer;
410 }
411
412 .custom-checkbox {
413 position: relative;
414 margin-right: 1rem;
415 }
416
417 .custom-checkbox input[type="checkbox"] {
418 opacity: 0;
419 position: absolute;
420 }
421
422 .custom-checkbox::before {
423 content: '';
424 display: inline-block;
425 width: 24px;
426 height: 24px;
427 border: 2px solid #d1d5db;
428 border-radius: 6px;
429 background: white;
430 transition: all 0.3s ease;
431 cursor: pointer;
432 }
433
434 .custom-checkbox input[type="checkbox"]:checked + .custom-checkbox::before,
435 .custom-checkbox:has(input[type="checkbox"]:checked)::before {
436 background: #3b82f6;
437 border-color: #3b82f6;
438 transform: scale(1.1);
439 }
440
441 .custom-checkbox input[type="checkbox"]:checked + .custom-checkbox::after,
442 .custom-checkbox:has(input[type="checkbox"]:checked)::after {
443 content: '✓';
444 position: absolute;
445 top: 50%;
446 left: 50%;
447 transform: translate(-50%, -50%);
448 color: white;
449 font-weight: bold;
450 font-size: 0.9rem;
451 }
452
453 .custom-checkbox:hover::before {
454 border-color: #3b82f6;
455 box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
456 }
457
458 /* Button with multiple states */
459 .state-button {
460 background: #3b82f6;
461 color: white;
462 border: none;
463 padding: 1rem 2rem;
464 border-radius: 12px;
465 font-weight: 600;
466 cursor: pointer;
467 position: relative;
468 overflow: hidden;
469 transition: all 0.3s ease;
470 margin: 0.5rem;
471 }
472
473 .state-button::before {
474 content: '';
475 position: absolute;
476 top: 50%;
477 left: 50%;
478 width: 0;
479 height: 0;
480 background: rgba(255,255,255,0.2);
481 border-radius: 50%;
482 transform: translate(-50%, -50%);
483 transition: all 0.4s ease;
484 }
485
486 .state-button:hover {
487 background: #2563eb;
488 transform: translateY(-3px);
489 box-shadow: 0 10px 30px rgba(59, 130, 246, 0.3);
490 }
491
492 .state-button:hover::before {
493 width: 200px;
494 height: 200px;
495 }
496
497 .state-button:active {
498 transform: translateY(-1px);
499 background: #1e40af;
500 }
501
502 .state-button:disabled {
503 background: #94a3b8;
504 cursor: not-allowed;
505 transform: none;
506 box-shadow: none;
507 }
508
509 .state-button:disabled::after {
510 content: '⏳';
511 position: absolute;
512 right: 1rem;
513 top: 50%;
514 transform: translateY(-50%);
515 animation: spin 1s linear infinite;
516 }
517
518 @keyframes spin {
519 to { transform: translateY(-50%) rotate(360deg); }
520 }
521
522 /* List with pseudo-element styling */
523 .styled-list {
524 list-style: none;
525 padding: 0;
526 margin: 2rem 0;
527 }
528
529 .styled-list li {
530 position: relative;
531 padding: 1rem 1rem 1rem 3rem;
532 margin-bottom: 1rem;
533 background: white;
534 border-radius: 12px;
535 box-shadow: 0 2px 10px rgba(0,0,0,0.05);
536 transition: all 0.3s ease;
537 }
538
539 .styled-list li::before {
540 content: counter(list-counter);
541 counter-increment: list-counter;
542 position: absolute;
543 left: 1rem;
544 top: 50%;
545 transform: translateY(-50%);
546 width: 2rem;
547 height: 2rem;
548 background: linear-gradient(135deg, #3b82f6, #8b5cf6);
549 color: white;
550 border-radius: 50%;
551 display: flex;
552 align-items: center;
553 justify-content: center;
554 font-weight: bold;
555 font-size: 0.9rem;
556 }
557
558 .styled-list {
559 counter-reset: list-counter;
560 }
561
562 .styled-list li:hover {
563 transform: translateX(10px);
564 box-shadow: 0 4px 20px rgba(59, 130, 246, 0.15);
565 }
566
567 .styled-list li:nth-child(odd)::before {
568 background: linear-gradient(135deg, #10b981, #059669);
569 }
570
571 .styled-list li:nth-child(even)::before {
572 background: linear-gradient(135deg, #f59e0b, #d97706);
573 }
574
575 /* Tooltip system */
576 .tooltip {
577 position: relative;
578 display: inline-block;
579 border-bottom: 1px dotted #3b82f6;
580 cursor: help;
581 }
582
583 .tooltip::after {
584 content: attr(data-tooltip);
585 position: absolute;
586 bottom: 125%;
587 left: 50%;
588 transform: translateX(-50%);
589 background: #1e293b;
590 color: white;
591 padding: 8px 12px;
592 border-radius: 8px;
593 font-size: 0.9rem;
594 white-space: nowrap;
595 opacity: 0;
596 visibility: hidden;
597 transition: all 0.3s ease;
598 z-index: 1000;
599 }
600
601 .tooltip::before {
602 content: '';
603 position: absolute;
604 bottom: 117%;
605 left: 50%;
606 transform: translateX(-50%);
607 width: 0;
608 height: 0;
609 border: 6px solid transparent;
610 border-top-color: #1e293b;
611 opacity: 0;
612 visibility: hidden;
613 transition: all 0.3s ease;
614 }
615
616 .tooltip:hover::after,
617 .tooltip:hover::before {
618 opacity: 1;
619 visibility: visible;
620 }
621
622 /* Decorative elements */
623 .decorative-section {
624 position: relative;
625 margin: 4rem 0;
626 text-align: center;
627 }
628
629 .decorative-section::before,
630 .decorative-section::after {
631 content: '';
632 position: absolute;
633 top: 50%;
634 width: 150px;
635 height: 1px;
636 background: linear-gradient(90deg, transparent, #d1d5db, transparent);
637 }
638
639 .decorative-section::before {
640 left: 0;
641 }
642
643 .decorative-section::after {
644 right: 0;
645 }
646
647 .decorative-section h3 {
648 background: white;
649 padding: 0 2rem;
650 color: #64748b;
651 position: relative;
652 display: inline-block;
653 }
654
655 /* nth-child patterns */
656 .pattern-grid {
657 display: grid;
658 grid-template-columns: repeat(auto-fit, minmax(80px, 1fr));
659 gap: 1rem;
660 margin: 2rem 0;
661 }
662
663 .pattern-item {
664 aspect-ratio: 1;
665 border-radius: 12px;
666 display: flex;
667 align-items: center;
668 justify-content: center;
669 color: white;
670 font-weight: bold;
671 transition: all 0.3s ease;
672 }
673
674 .pattern-item:nth-child(6n+1) { background: #ef4444; }
675 .pattern-item:nth-child(6n+2) { background: #f97316; }
676 .pattern-item:nth-child(6n+3) { background: #eab308; }
677 .pattern-item:nth-child(6n+4) { background: #22c55e; }
678 .pattern-item:nth-child(6n+5) { background: #3b82f6; }
679 .pattern-item:nth-child(6n) { background: #8b5cf6; }
680
681 .pattern-item:hover {
682 transform: scale(1.1) rotate(5deg);
683 box-shadow: 0 8px 25px rgba(0,0,0,0.2);
684 }
685
686 /* Progress indicators */
687 .progress-list {
688 list-style: none;
689 padding: 0;
690 margin: 2rem 0;
691 counter-reset: progress-counter;
692 }
693
694 .progress-item {
695 counter-increment: progress-counter;
696 position: relative;
697 padding: 1.5rem 0 1.5rem 4rem;
698 border-left: 3px solid #e5e7eb;
699 }
700
701 .progress-item::before {
702 content: counter(progress-counter);
703 position: absolute;
704 left: -1.5rem;
705 top: 1.5rem;
706 width: 3rem;
707 height: 3rem;
708 background: #3b82f6;
709 color: white;
710 border-radius: 50%;
711 display: flex;
712 align-items: center;
713 justify-content: center;
714 font-weight: bold;
715 font-size: 1.1rem;
716 border: 4px solid white;
717 box-shadow: 0 2px 10px rgba(0,0,0,0.1);
718 }
719
720 .progress-item:last-child {
721 border-left-color: transparent;
722 }
723
724 .progress-item.completed::before {
725 background: #10b981;
726 content: '✓';
727 }
728
729 .progress-item.active::before {
730 background: #f59e0b;
731 animation: pulse 2s ease-in-out infinite;
732 }
733
734 /* Mobile optimizations */
735 @media (max-width: 768px) {
736 .section-title::before {
737 position: static;
738 display: block;
739 text-align: center;
740 margin-bottom: 0.5rem;
741 }
742
743 .card-grid {
744 grid-template-columns: 1fr;
745 }
746
747 .pattern-grid {
748 grid-template-columns: repeat(4, 1fr);
749 }
750 }
751 </style>
752</head>
753<body>
754 <!-- Header with advanced navigation -->
755 <header class="header">
756 <div class="container">
757 <nav class="nav">
758 <a href="#" class="nav-brand">🎭 Pseudo Master</a>
759 <ul class="nav-menu">
760 <li class="nav-item">
761 <a href="#states" class="nav-link">States</a>
762 </li>
763 <li class="nav-item">
764 <a href="#selectors" class="nav-link">Selectors</a>
765 </li>
766 <li class="nav-item">
767 <a href="#elements" class="nav-link">Elements</a>
768 </li>
769 <li class="nav-item">
770 <a href="#patterns" class="nav-link">Patterns</a>
771 </li>
772 </ul>
773 </nav>
774 </div>
775 </header>
776
777 <!-- Hero section -->
778 <section class="hero">
779 <div class="container">
780 <div class="hero-content">
781 <h1>Master Pseudo-classes & Elements</h1>
782 <p>Create sophisticated interactions and decorative elements using advanced CSS selectors and virtual elements.</p>
783 </div>
784 </div>
785 </section>
786
787 <!-- Pseudo-classes section -->
788 <section id="states" class="section">
789 <div class="container">
790 <h2 class="section-title">Pseudo-class States</h2>
791
792 <div class="card-grid">
793 <div class="interactive-card">
794 <div class="card-status"></div>
795 <div class="card-icon">:hover</div>
796 <h3>Hover Effects</h3>
797 <p>Mouse over this card to see multiple hover states in action, including status changes and icon animations.</p>
798 </div>
799
800 <div class="interactive-card">
801 <div class="card-status"></div>
802 <div class="card-icon">:active</div>
803 <h3>Active States</h3>
804 <p>Click and hold this card to see active state styling with different transforms and color changes.</p>
805 </div>
806
807 <div class="interactive-card">
808 <div class="card-status"></div>
809 <div class="card-icon">:focus</div>
810 <h3>Focus Management</h3>
811 <p>Tab to this card or click it to see focus styles that improve accessibility and user experience.</p>
812 <div tabindex="0" style="outline: none;">Focusable element</div>
813 </div>
814 </div>
815 </div>
816 </section>
817
818 <!-- Form states section -->
819 <section class="section" style="background: #f8fafc;">
820 <div class="container">
821 <h2 class="section-title">Form State Styling</h2>
822
823 <div class="form-section">
824 <form>
825 <div class="form-group">
826 <label for="name" class="form-label required">Full Name</label>
827 <input type="text" id="name" class="form-input" placeholder="Enter your name" required minlength="2">
828 <div class="validation-icon"></div>
829 </div>
830
831 <div class="form-group">
832 <label for="email" class="form-label required">Email Address</label>
833 <input type="email" id="email" class="form-input" placeholder="Enter your email" required>
834 <div class="validation-icon"></div>
835 </div>
836
837 <div class="form-group">
838 <label for="phone" class="form-label">Phone Number</label>
839 <input type="tel" id="phone" class="form-input" placeholder="Optional phone number" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}">
840 <div class="validation-icon"></div>
841 </div>
842
843 <div class="checkbox-group">
844 <input type="checkbox" id="agree" required>
845 <label for="agree" class="custom-checkbox"></label>
846 <label for="agree">I agree to the terms and conditions</label>
847 </div>
848
849 <div class="checkbox-group">
850 <input type="checkbox" id="newsletter">
851 <label for="newsletter" class="custom-checkbox"></label>
852 <label for="newsletter">Subscribe to newsletter</label>
853 </div>
854
855 <button type="submit" class="state-button">Submit Form</button>
856 <button type="button" class="state-button" disabled>Processing...</button>
857 </form>
858 </div>
859 </div>
860 </section>
861
862 <!-- Structural selectors section -->
863 <section id="selectors" class="section">
864 <div class="container">
865 <h2 class="section-title">Structural Selectors</h2>
866
867 <ol class="styled-list">
868 <li>First item with special counter styling and hover effects</li>
869 <li>Second item showing alternating colors and animations</li>
870 <li>Third item demonstrating nth-child pattern styling</li>
871 <li>Fourth item with hover transforms and shadow effects</li>
872 <li>Fifth item showcasing complete pseudo-element mastery</li>
873 </ol>
874
875 <div class="decorative-section">
876 <h3>Pattern Demonstration</h3>
877 </div>
878
879 <div class="pattern-grid">
880 <div class="pattern-item">1</div>
881 <div class="pattern-item">2</div>
882 <div class="pattern-item">3</div>
883 <div class="pattern-item">4</div>
884 <div class="pattern-item">5</div>
885 <div class="pattern-item">6</div>
886 <div class="pattern-item">7</div>
887 <div class="pattern-item">8</div>
888 <div class="pattern-item">9</div>
889 <div class="pattern-item">10</div>
890 <div class="pattern-item">11</div>
891 <div class="pattern-item">12</div>
892 <div class="pattern-item">13</div>
893 <div class="pattern-item">14</div>
894 <div class="pattern-item">15</div>
895 <div class="pattern-item">16</div>
896 <div class="pattern-item">17</div>
897 <div class="pattern-item">18</div>
898 </div>
899 </div>
900 </section>
901
902 <!-- Pseudo-elements section -->
903 <section id="elements" class="section" style="background: white;">
904 <div class="container">
905 <h2 class="section-title">Pseudo-element Magic</h2>
906
907 <p>This section demonstrates various <span class="tooltip" data-tooltip="Virtual elements created with CSS">pseudo-elements</span> including tooltips, decorative elements, and content generation. Notice how the <span class="tooltip" data-tooltip="::before and ::after create virtual content">pseudo-elements</span> enhance the design without cluttering the HTML.</p>
908
909 <ul class="progress-list">
910 <li class="progress-item completed">Understanding pseudo-class fundamentals</li>
911 <li class="progress-item completed">Mastering form state styling</li>
912 <li class="progress-item active">Learning structural selectors</li>
913 <li class="progress-item">Implementing pseudo-elements</li>
914 <li class="progress-item">Creating interactive components</li>
915 </ul>
916 </div>
917 </section>
918
919 <!-- Advanced patterns section -->
920 <section id="patterns" class="section">
921 <div class="container">
922 <h2 class="section-title">Advanced Patterns</h2>
923
924 <div class="card-grid">
925 <div class="interactive-card">
926 <div class="card-icon">nth</div>
927 <h3>nth-child Patterns</h3>
928 <p>Advanced nth-child selectors for creating complex patterns, zebra striping, and responsive grid layouts.</p>
929 </div>
930
931 <div class="interactive-card">
932 <div class="card-icon">::</div>
933 <h3>Pseudo-elements</h3>
934 <p>Virtual elements for tooltips, decorative graphics, icons, and complex layout effects without extra HTML.</p>
935 </div>
936
937 <div class="interactive-card">
938 <div class="card-icon">:is</div>
939 <h3>Logical Selectors</h3>
940 <p>Modern selector functions like :is(), :where(), and :has() for more efficient and maintainable CSS.</p>
941 </div>
942 </div>
943 </div>
944 </section>
945
946 <!-- Summary -->
947 <section class="section" style="text-align: center; background: #1e293b; color: white;">
948 <div class="container">
949 <h2 style="color: white; margin-bottom: 2rem;">🎭 Pseudo-class & Element Mastery!</h2>
950 <p style="font-size: 1.2rem; margin-bottom: 2rem; opacity: 0.9;">
951 You've mastered advanced CSS selectors for creating sophisticated interactive experiences!
952 </p>
953
954 <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 1rem; margin: 2rem 0;">
955 <div style="background: rgba(255,255,255,0.1); padding: 1.5rem; border-radius: 12px;">
956 <strong>✅ Pseudo-classes</strong><br>
957 <small>State-based styling with :hover, :focus, :active</small>
958 </div>
959 <div style="background: rgba(255,255,255,0.1); padding: 1.5rem; border-radius: 12px;">
960 <strong>✅ Structural Selectors</strong><br>
961 <small>Pattern-based targeting with nth-child variations</small>
962 </div>
963 <div style="background: rgba(255,255,255,0.1); padding: 1.5rem; border-radius: 12px;">
964 <strong>✅ Pseudo-elements</strong><br>
965 <small>Virtual content creation with ::before, ::after</small>
966 </div>
967 <div style="background: rgba(255,255,255,0.1); padding: 1.5rem; border-radius: 12px;">
968 <strong>✅ Interactive Design</strong><br>
969 <small>Clean HTML with advanced CSS capabilities</small>
970 </div>
971 </div>
972 </div>
973 </section>
974
975 <script>
976 // Intersection Observer for section titles
977 const observerOptions = {
978 threshold: 0.1,
979 rootMargin: '0px 0px -50px 0px'
980 };
981
982 const observer = new IntersectionObserver((entries) => {
983 entries.forEach(entry => {
984 if (entry.isIntersecting) {
985 entry.target.classList.add('visible');
986 }
987 });
988 }, observerOptions);
989
990 // Observe all section titles
991 document.querySelectorAll('.section-title').forEach(title => {
992 observer.observe(title);
993 });
994
995 // Form validation demo
996 document.addEventListener('DOMContentLoaded', function() {
997 const form = document.querySelector('form');
998 const submitBtn = form.querySelector('button[type="submit"]');
999
1000 // Add form submission handling
1001 form.addEventListener('submit', function(e) {
1002 e.preventDefault();
1003
1004 submitBtn.disabled = true;
1005 submitBtn.textContent = 'Processing...';
1006
1007 setTimeout(() => {
1008 submitBtn.disabled = false;
1009 submitBtn.textContent = 'Form Submitted!';
1010 submitBtn.style.background = '#10b981';
1011
1012 setTimeout(() => {
1013 submitBtn.textContent = 'Submit Form';
1014 submitBtn.style.background = '';
1015 }, 2000);
1016 }, 1500);
1017 });
1018
1019 // Add interactive card click handling
1020 document.querySelectorAll('.interactive-card').forEach(card => {
1021 card.addEventListener('click', function() {
1022 this.style.transform = 'scale(0.98)';
1023 setTimeout(() => {
1024 this.style.transform = '';
1025 }, 150);
1026 });
1027 });
1028
1029 // Progress demo
1030 const progressItems = document.querySelectorAll('.progress-item');
1031 let currentProgress = 2; // Start at item 3 (active)
1032
1033 setInterval(() => {
1034 if (currentProgress < progressItems.length) {
1035 progressItems[currentProgress].classList.remove('active');
1036 progressItems[currentProgress].classList.add('completed');
1037 currentProgress++;
1038
1039 if (currentProgress < progressItems.length) {
1040 progressItems[currentProgress].classList.add('active');
1041 }
1042 } else {
1043 // Reset demo
1044 progressItems.forEach((item, index) => {
1045 item.classList.remove('completed', 'active');
1046 if (index < 2) {
1047 item.classList.add('completed');
1048 } else if (index === 2) {
1049 item.classList.add('active');
1050 }
1051 });
1052 currentProgress = 2;
1053 }
1054 }, 3000);
1055 });
1056 </script>
1057</body>
1058</html>This comprehensive pseudo-classes and pseudo-elements demonstration showcases advanced CSS selector techniques:
🎭 Comprehensive Pseudo-class Coverage:
🔍 Structural Selector Mastery:
✨ Pseudo-element Excellence:
🎨 Advanced Styling Techniques:
💡 Real-World Applications:
This example demonstrates how pseudo-classes and pseudo-elements enable sophisticated styling while keeping HTML clean and semantic, showcasing the full power of advanced CSS selectors.
Excellent work mastering pseudo-classes and pseudo-elements! You now have the skills to create sophisticated interactions and beautiful decorative elements with clean, semantic HTML. Next, we'll explore "CSS Variables & Custom Properties" where you'll learn to create dynamic, maintainable stylesheets with custom properties, theming systems, and calculated values. Get ready to make your CSS more powerful and flexible!
Your progress is saved in this browser, on this device only.