Day 14: CSS Variables & Functions Assignment
Solution: Light/Dark Theme Toggle with CSS Variables
Here's a complete implementation of a theme switcher using CSS custom properties and the :has() pseudo-class (with a fallback for browsers that don't support it):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Theme Switcher</title>
<style>
:root {
/* Light theme variables */
--bg-color: #ffffff;
--text-color: #333333;
--primary-color: #4a6fa5;
--secondary-color: #166088;
--accent-color: #4fc3f7;
--card-bg: #f8f9fa;
--border-color: #dee2e6;
--shadow-color: rgba(0, 0, 0, 0.1);
}
/* Dark theme variables */
[data-theme="dark"] {
--bg-color: #121212;
--text-color: #e0e0e0;
--primary-color: #6a8bc1;
--secondary-color: #4d96c6;
--accent-color: #8fd8f0;
--card-bg: #1e1e1e;
--border-color: #333333;
--shadow-color: rgba(0, 0, 0, 0.3);
}
body {
background-color: var(--bg-color);
color: var(--text-color);
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
line-height: 1.6;
margin: 0;
padding: 20px;
transition: background-color 0.3s, color 0.3s;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
h1 {
color: var(--primary-color);
}
.card {
background-color: var(--card-bg);
border: 1px solid var(--border-color);
border-radius: 8px;
padding: 20px;
margin-bottom: 20px;
box-shadow: 0 2px 4px var(--shadow-color);
}
.btn {
background-color: var(--primary-color);
color: white;
border: none;
padding: 10px 15px;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s;
}
.btn:hover {
background-color: var(--secondary-color);
}
.theme-toggle {
position: fixed;
top: 20px;
right: 20px;
display: flex;
align-items: center;
gap: 10px;
}
/* Hide the default checkbox */
.theme-toggle input {
opacity: 0;
position: absolute;
}
/* Custom toggle switch */
.toggle-switch {
width: 60px;
height: 30px;
background-color: var(--border-color);
border-radius: 15px;
position: relative;
cursor: pointer;
transition: background-color 0.3s;
}
.toggle-switch::before {
content: "";
position: absolute;
width: 26px;
height: 26px;
background-color: white;
border-radius: 50%;
top: 2px;
left: 2px;
transition: transform 0.3s;
}
/* When checkbox is checked */
.theme-toggle input:checked + .toggle-switch {
background-color: var(--primary-color);
}
.theme-toggle input:checked + .toggle-switch::before {
transform: translateX(30px);
}
/* Modern approach using :has() */
@supports selector(:has(*)) {
body:has(.theme-toggle input:checked) {
background-color: var(--bg-color);
color: var(--text-color);
}
body:has(.theme-toggle input:checked) {
--bg-color: #121212;
--text-color: #e0e0e0;
--primary-color: #6a8bc1;
--secondary-color: #4d96c6;
--accent-color: #8fd8f0;
--card-bg: #1e1e1e;
--border-color: #333333;
--shadow-color: rgba(0, 0, 0, 0.3);
}
}
/* Fallback for browsers that don't support :has() */
@supports not selector(:has(*)) {
.theme-toggle input:checked ~ .theme-indicator {
display: none;
}
.theme-toggle input:checked ~ .theme-indicator.dark {
display: block;
}
}
</style>
</head>
<body>
<div class="theme-toggle">
<input type="checkbox" id="theme-switch">
<label for="theme-switch" class="toggle-switch"></label>
<span class="theme-indicator">Light</span>
<span class="theme-indicator dark" style="display: none;">Dark</span>
</div>
<div class="container">
<h1>CSS Theme Switcher</h1>
<p>This page demonstrates theme switching using CSS variables and a toggle switch.</p>
<div class="card">
<h2>Card Title</h2>
<p>This is a card component that changes colors based on the selected theme.</p>
<button class="btn">Sample Button</button>
</div>
<div class="card">
<h2>Another Card</h2>
<p>Notice how all elements automatically adjust to the theme colors defined in CSS variables.</p>
</div>
</div>
</body>
</html>
Key Features:
-
CSS Variables: Defined in :root for light theme and overridden in [data-theme="dark"] for dark theme.
-
Theme Switching:
- Uses a checkbox input with a custom toggle switch
- Implements two approaches:
- Modern
:has() selector (for browsers that support it)
- Fallback method using adjacent sibling combinator
-
Smooth Transitions: All color changes have smooth transitions for better UX.
-
Responsive Design: Works on different screen sizes.
-
Accessibility: Maintains good contrast ratios in both themes.
How It Works:
- The light theme variables are defined in
:root (default)
- The dark theme variables override these when the
[data-theme="dark"] attribute is present
- The checkbox toggle uses CSS to switch between themes without JavaScript
- The
:has() selector (modern approach) checks if the checkbox is checked and applies dark theme variables
- The fallback method shows/hides theme indicators based on checkbox state
This solution demonstrates the power of CSS variables and functions for creating maintainable, themeable interfaces.