CSS
Dark Mode
Dark theme switching.
By EZ4Code Team
cssdark-mode
Code
/* CSS variable definition */
:root {
--bg: #ffffff;
--text: #1a1a1a;
--card-bg: #f5f5f5;
--border: #e0e0e0;
--primary: #3498db;
}
/* System dark mode */
@media (prefers-color-scheme: dark) {
:root {
--bg: #1a1a1a;
--text: #f0f0f0;
--card-bg: #2a2a2a;
--border: #404040;
--primary: #5dade2;
}
}
/* Manual switch */
[data-theme="dark"] {
--bg: #1a1a1a;
--text: #f0f0f0;
--card-bg: #2a2a2a;
--border: #404040;
--primary: #5dade2;
}
/* Apply variables */
body {
background-color: var(--bg);
color: var(--text);
transition: background-color 0.3s, color 0.3s;
}
.card {
background: var(--card-bg);
border: 1px solid var(--border);
}
/* JS toggle */
/*
const toggle = document.getElementById('theme-toggle');
toggle.addEventListener('click', () => {
const current = document.documentElement.getAttribute('data-theme');
document.documentElement.setAttribute(
'data-theme', current === 'dark' ? 'light' : 'dark'
);
});
*/Explanation
Implements dark mode via CSS variables and the data-theme attribute; prefers-color-scheme follows the system.
More CSS Snippets
Center a Div with Flexbox and Grid
Center elements horizontally and vertically using Flexbox, Grid, and absolute positioning.
CSS Selectors and Pseudo-classes
Target elements with pseudo-classes, pseudo-elements, and attribute selectors.
CSS Grid
Grid layout.
Flexbox
Flexible box layout.
Animation
CSS keyframe animations.
Transitions
CSS transition effects.