{
  "$type": "site.standard.document",
  "bskyPostRef": {
    "cid": "bafyreibhli7q7iiey2zetkcm5hppbjcbt5r6gh5nwqv2eulojexks6dmoi",
    "uri": "at://did:plc:25rdn5elo5izoxrmtis34zuk/app.bsky.feed.post/3mp57cqqfz362"
  },
  "coverImage": {
    "$type": "blob",
    "ref": {
      "$link": "bafkreicg7ns3px4h2kyb5ua6b3uwinzo57z5cin3z7r4gy4uy3poo2zjku"
    },
    "mimeType": "image/webp",
    "size": 307400
  },
  "path": "/hasansarwer/a-practical-css-variable-setup-for-lightdark-mode-without-theme-flash-22c2",
  "publishedAt": "2026-06-25T19:14:20.000Z",
  "site": "https://dev.to",
  "tags": [
    "css",
    "webdev",
    "darkmode",
    "frontend",
    "@media"
  ],
  "textContent": "Dark mode is easy to start.\n\nIt is harder to ship cleanly.\n\nMost implementations need to handle:\n\n  * light mode\n  * dark mode\n  * system preference\n  * stored user preference\n  * a toggle button\n  * no flash of the wrong theme on page load\n\n\n\nThe flash is the annoying part.\n\nYou save the user's choice in `localStorage`, but your app JavaScript usually runs after the browser has already started parsing HTML and CSS.\n\nSo the page may briefly render in the wrong theme before your JavaScript applies the saved preference.\n\nThis article shows a practical setup using:\n\n  * CSS variables\n  * `data-theme` on `<html>`\n  * `prefers-color-scheme`\n  * `localStorage`\n  * a tiny inline script in `<head>`\n\n\n\nNo framework required.\n\n##  The goal\n\nI want components to use semantic variables like this:\n\n\n\n    body {\n      background: var(--color-background);\n      color: var(--color-text);\n    }\n\n    .card {\n      background: var(--color-surface);\n      border: 1px solid var(--color-border);\n    }\n\n    .button {\n      background: var(--color-primary);\n      color: var(--color-on-primary);\n    }\n\n\nThe component should not care whether the app is currently light or dark.\n\nOnly the variable values should change.\n\n##  Define light mode variables\n\nStart with light mode as the default.\n\n\n\n    :root {\n      color-scheme: light;\n\n      --color-background: #ffffff;\n      --color-surface: #f8fafc;\n      --color-text: #0f172a;\n      --color-muted: #64748b;\n      --color-border: #e2e8f0;\n\n      --color-primary: #2563eb;\n      --color-on-primary: #ffffff;\n    }\n\n\nThis means that if nothing else happens, the site renders in light mode.\n\nThat is a safe default.\n\n##  Add explicit dark mode\n\nNow add dark mode using `data-theme=\"dark\"` on the root element.\n\n\n\n    :root[data-theme=\"dark\"] {\n      color-scheme: dark;\n\n      --color-background: #020617;\n      --color-surface: #0f172a;\n      --color-text: #f8fafc;\n      --color-muted: #94a3b8;\n      --color-border: #1e293b;\n\n      --color-primary: #60a5fa;\n      --color-on-primary: #020617;\n    }\n\n\nWhen this attribute exists:\n\n\n\n    <html data-theme=\"dark\">\n\n\nthe dark variables override the light variables.\n\n##  Add system preference support\n\nUsers may have an OS-level preference for dark mode.\n\nYou can respect that with `prefers-color-scheme`.\n\n\n\n    @media (prefers-color-scheme: dark) {\n      :root:not([data-theme=\"light\"]) {\n        color-scheme: dark;\n\n        --color-background: #020617;\n        --color-surface: #0f172a;\n        --color-text: #f8fafc;\n        --color-muted: #94a3b8;\n        --color-border: #1e293b;\n\n        --color-primary: #60a5fa;\n        --color-on-primary: #020617;\n      }\n    }\n\n\nThe important part is this selector:\n\n\n\n    :root:not([data-theme=\"light\"])\n\n\nIt means:\n\n> Use system dark mode unless the user explicitly selected light mode.\n\nSo the behavior becomes:\n\nUser choice | Result\n---|---\nNo saved choice + OS light | light\nNo saved choice + OS dark | dark\nSaved `light` | light\nSaved `dark` | dark\n\n##  Prevent the wrong theme from flashing\n\nThis is the critical piece.\n\nAdd a small inline script in `<head>` before your theme CSS.\n\n\n\n    <script>\n      (function () {\n        try {\n          var storedTheme = localStorage.getItem('theme');\n\n          if (storedTheme === 'light' || storedTheme === 'dark') {\n            document.documentElement.setAttribute('data-theme', storedTheme);\n          }\n        } catch (_) {}\n      })();\n    </script>\n\n\nDo not run this script after the page loads.\n\nAvoid:\n\n\n\n    <script defer src=\"/theme.js\"></script>\n\n\nAvoid:\n\n\n\n    document.addEventListener('DOMContentLoaded', ...)\n\n\nThe script should run immediately while the browser is still parsing the `<head>`.\n\nWhy?\n\nBecause the browser reads the document top to bottom.\n\nIf this script sets `data-theme=\"dark\"` before the CSS is applied, the correct variables are active before the first paint.\n\nThat prevents the wrong theme from flashing.\n\n##  Recommended HTML structure\n\nPut the inline script before the CSS.\n\n\n\n    <!doctype html>\n    <html lang=\"en\">\n      <head>\n        <script>\n          (function () {\n            try {\n              var storedTheme = localStorage.getItem('theme');\n\n              if (storedTheme === 'light' || storedTheme === 'dark') {\n                document.documentElement.setAttribute('data-theme', storedTheme);\n              }\n            } catch (_) {}\n          })();\n        </script>\n\n        <style>\n          :root {\n            color-scheme: light;\n\n            --color-background: #ffffff;\n            --color-surface: #f8fafc;\n            --color-text: #0f172a;\n            --color-muted: #64748b;\n            --color-border: #e2e8f0;\n\n            --color-primary: #2563eb;\n            --color-on-primary: #ffffff;\n          }\n\n          :root[data-theme=\"dark\"] {\n            color-scheme: dark;\n\n            --color-background: #020617;\n            --color-surface: #0f172a;\n            --color-text: #f8fafc;\n            --color-muted: #94a3b8;\n            --color-border: #1e293b;\n\n            --color-primary: #60a5fa;\n            --color-on-primary: #020617;\n          }\n\n          @media (prefers-color-scheme: dark) {\n            :root:not([data-theme=\"light\"]) {\n              color-scheme: dark;\n\n              --color-background: #020617;\n              --color-surface: #0f172a;\n              --color-text: #f8fafc;\n              --color-muted: #94a3b8;\n              --color-border: #1e293b;\n\n              --color-primary: #60a5fa;\n              --color-on-primary: #020617;\n            }\n          }\n        </style>\n      </head>\n\n      <body>\n        <button id=\"theme-toggle\" type=\"button\">\n          Toggle theme\n        </button>\n      </body>\n    </html>\n\n\n##  Add the toggle function\n\nNow add a simple toggle.\n\n\n\n    function toggleTheme() {\n      var html = document.documentElement;\n      var currentTheme = html.getAttribute('data-theme');\n\n      var nextTheme = currentTheme === 'dark' ? 'light' : 'dark';\n\n      html.setAttribute('data-theme', nextTheme);\n      localStorage.setItem('theme', nextTheme);\n    }\n\n    document\n      .getElementById('theme-toggle')\n      ?.addEventListener('click', toggleTheme);\n\n\nNow clicking the button switches between:\n\n\n\n    <html data-theme=\"light\">\n\n\nand:\n\n\n\n    <html data-theme=\"dark\">\n\n\n##  Add a system mode option\n\nA good theme switcher often has three options:\n\n  * light\n  * dark\n  * system\n\n\n\nFor system mode, remove the attribute and clear `localStorage`.\n\n\n\n    function resetToSystemTheme() {\n      document.documentElement.removeAttribute('data-theme');\n      localStorage.removeItem('theme');\n    }\n\n\nNow the browser falls back to:\n\n\n\n    @media (prefers-color-scheme: dark) {\n      :root:not([data-theme=\"light\"]) {\n        /* dark variables */\n      }\n    }\n\n\n##  Why `color-scheme` matters\n\nThis line is easy to forget:\n\n\n\n    color-scheme: light;\n\n\nand:\n\n\n\n    color-scheme: dark;\n\n\nIt tells the browser which color scheme your page supports.\n\nThat affects built-in UI such as:\n\n  * form controls\n  * scrollbars\n  * default input styling\n  * browser-rendered UI pieces\n\n\n\nSo your page does not only change your custom CSS variables. Native browser UI also gets a better matching appearance.\n\n##  Component CSS stays simple\n\nOnce the variables are defined, component CSS becomes boring.\n\nThat is good.\n\n\n\n    body {\n      margin: 0;\n      background: var(--color-background);\n      color: var(--color-text);\n      font-family: system-ui, sans-serif;\n    }\n\n    .card {\n      background: var(--color-surface);\n      border: 1px solid var(--color-border);\n      border-radius: 12px;\n      padding: 1rem;\n    }\n\n    .card-muted {\n      color: var(--color-muted);\n    }\n\n    .button {\n      background: var(--color-primary);\n      color: var(--color-on-primary);\n      border: 0;\n      border-radius: 8px;\n      padding: 0.625rem 1rem;\n      cursor: pointer;\n    }\n\n\nThere is no separate `.dark .button` rule.\n\nThe variables handle the mode.\n\n##  The limitation\n\nThis setup solves the theme switching structure.\n\nIt does not solve color design for you.\n\nYou still need to decide:\n\n  * which light colors to use\n  * which dark colors to use\n  * whether text has enough contrast\n  * what hover/pressed/disabled states should be\n  * what color should go on top of primary buttons\n  * how semantic colors like success/warning/danger behave in both modes\n\n\n\nFor a small project, manually writing variables may be enough.\n\nFor a larger system, it becomes repetitive.\n\n##  Generating the variables instead\n\nThis is why I started working on `salt-theme-gen`.\n\nInstead of manually maintaining separate light and dark token files, you can generate both modes from one call:\n\n\n\n    import { generateTheme } from 'salt-theme-gen';\n\n    const theme = generateTheme({ preset: 'ocean' });\n\n\nThat returns:\n\n\n\n    theme.light;\n    theme.dark;\n\n\nEach mode has the same structure:\n\n\n\n    theme.light.colors;\n    theme.light.states;\n    theme.light.spacing;\n    theme.light.radius;\n    theme.light.fontSizes;\n    theme.light.accessibility;\n\n\nSo the CSS variable setup can be generated instead of handwritten.\n\nFor example:\n\n\n\n    function kebab(str: string): string {\n      return str.replace(/([A-Z])/g, '-$1').toLowerCase();\n    }\n\n    function modeToVars(mode: typeof theme.light): string {\n      const lines: string[] = [];\n\n      for (const [key, value] of Object.entries(mode.colors)) {\n        lines.push(`  --color-${kebab(key)}: ${value};`);\n      }\n\n      for (const [key, value] of Object.entries(mode.spacing)) {\n        lines.push(`  --space-${key}: ${value}px;`);\n      }\n\n      for (const [key, value] of Object.entries(mode.radius)) {\n        lines.push(`  --radius-${key}: ${value}px;`);\n      }\n\n      for (const [key, value] of Object.entries(mode.fontSizes)) {\n        lines.push(`  --text-${key}: ${value}px;`);\n      }\n\n      return lines.join('\\n');\n    }\n\n\nThen:\n\n\n\n    const themeCSS = `\n    :root {\n      color-scheme: light;\n    ${modeToVars(theme.light)}\n    }\n\n    :root[data-theme=\"dark\"] {\n      color-scheme: dark;\n    ${modeToVars(theme.dark)}\n    }\n    `;\n\n\nThe browser still uses the same CSS variable strategy.\n\nThe difference is that the design tokens are generated from a system instead of being maintained by hand.\n\n##  The bottom line\n\nA reliable light/dark setup does not need to be complicated.\n\nThe key pieces are:\n\n\n\n    CSS variables for tokens\n    data-theme on <html>\n    prefers-color-scheme for system fallback\n    localStorage for user preferences\n    inline script in <head> to avoid theme flash\n\n\nThe most important rule:\n\n> Set the saved theme before the CSS is applied.\n\nAfter that, your components can stay clean:\n\n\n\n    background: var(--color-background);\n    color: var(--color-text);\n\n\nThe values change between light and dark mode.\n\nThe component contract stays the same.",
  "title": "A practical CSS variable setup for light/dark mode without theme flash"
}