Theme switch with a data attribute
JavaScript sets data-theme on html, CSS does the rest.
CSS · Theme switch with a data attributeIntermediate
:root, [data-theme="light"] {
--bg: #ffffff; --text: #111827; --accent: #2563eb;
}
[data-theme="dark"] {
--bg: #0b1220; --text: #e5e7eb; --accent: #60a5fa;
}
body { background: var(--bg); color: var(--text); transition: background 0.2s, color 0.2s; }
a { color: var(--accent); }themedata-themevariablestoggle