Theming & Dark Mode

A Theme with Variables

Example

Swap a set of variables to change the whole theme. Click the button:

<!DOCTYPE html>
<html>
<head>
  <title>Page Title</title>
  <style>
    :root { --bg: #ffffff; --text: #1f2933; --card: #f1f3f5; --accent: #04AA6D; }
    .dark { --bg: #1d2a35; --text: #e9ecef; --card: #282A35; --accent: #38d9a9; }
    body { background: var(--bg); color: var(--text); font-family: Arial; transition: background .3s, color .3s; }
    .card { background: var(--card); padding: 12px; border-left: 4px solid var(--accent); }
    button { background: var(--accent); border: 0; padding: 8px 12px; margin-bottom: 10px; }
  </style>
</head>
<body>

<button onclick="document.body.classList.toggle('dark')">Toggle dark mode</button>
<div class="card">Colors come from CSS variables.</div>

</body>
</html>

Result

Following the System Setting

/* light by default, dark when the operating system asks for it */
:root { --bg: #fff; --text: #111; color-scheme: light dark; }

@media (prefers-color-scheme: dark) {
  :root { --bg: #121212; --text: #eee; }
}

Tip: color-scheme: light dark also makes form controls and scrollbars match the theme.