Sub topic 10
Scrollbars and form controls with color-scheme
This chapter assumes you already use the dark: variant, have a working class-based or selector-based dark mode, and know basic CSS custom properties. You will learn why styling your own elements is not enough for a complete dark theme, and how the color-scheme property fixes the parts of the page that the browser draws itself.
The problem: parts of the page you did not style
Suppose you have a dark page with dark:bg-gray-900 on the body. Text and cards look right. Then you scroll, and a bright white scrollbar appears. You open a form, and the text input has a white background, a <select> opens a white menu, and a date picker pops up in light colors.
These parts are native UI. The browser draws them using its own default styles, and those defaults follow the browser's idea of the page's color scheme. Unless you tell it otherwise, the browser assumes the page is light. Your dark: classes change the properties you name. They do not change what the browser assumes about the page.
Native UI that is affected includes:
- Page and element scrollbars.
- Default backgrounds and text colors of
<input>,<textarea>,<select>and<button>when you have not styled them. - Checkboxes, radio buttons, range sliders and progress bars.
- Date, time and color pickers.
- The default page background and text color when no CSS sets them (the "canvas").
- Spellcheck underlines and some autofill styling.
What color-scheme does
color-scheme is a CSS property that declares which color schemes an element can render in. The browser uses it to choose between its light and dark versions of the native UI.
:root {
color-scheme: light;
}
.dark {
color-scheme: dark;
}The accepted values are:
| Value | Meaning |
|---|---|
| `normal` | No preference. The browser renders light. This is the default. |
| `light` | The element supports light only. |
| `dark` | The element supports dark only. |
| `light dark` | The element supports both. The browser picks based on the user's system setting. |
| `only light` | Forces light and opts out of automatic dark adjustments (for example, forced dark modes in some browsers). |
The property is inherited. Setting it on <html> (the :root element) applies it to the whole document, including the viewport scrollbar and the canvas background.
Using it with a class-based dark mode
If you use the class or selector strategy from earlier chapters, color-scheme must follow the same switch as your dark: variant. Otherwise a user who picks dark mode on a light-themed system gets dark cards with a light scrollbar.
The CSS-variable example from the previous chapter already shows the pattern:
:root {
--surface: #ffffff;
--text: #111827;
color-scheme: light;
}
.dark {
--surface: #111827;
--text: #f9fafb;
color-scheme: dark;
}Because the dark class is on <html>, the .dark rule wins over :root (it has the same element but a higher specificity, since a class beats a pseudo-class that only matches the root... in practice both match <html>, and .dark appears later and has equal-or-higher specificity). The result is that color-scheme flips together with your variables.
Using Tailwind utilities
Tailwind v4 includes scheme-* utilities that set color-scheme. You can use them directly on the <html> element and combine them with dark::
<html class="scheme-light dark:scheme-dark">
<body class="bg-white text-gray-900 dark:bg-gray-900 dark:text-white">
<!-- page content -->
</body>
</html>The related utilities are scheme-normal, scheme-dark, scheme-light, scheme-light-dark and scheme-only-dark, with matching only-light forms. In Tailwind v3 these utilities do not exist. Add the CSS yourself, either in your stylesheet as shown above or in a small layer:
@tailwind base;
@tailwind components;
@tailwind utilities;
@layer base {
:root {
color-scheme: light;
}
.dark {
color-scheme: dark;
}
}If you use the data-theme attribute approach instead of a class, key the rule to that attribute:
[data-theme="dark"] {
color-scheme: dark;
}Using it with the media strategy
If you follow only the operating system setting (the default media strategy), you do not need a toggle. You can declare that the page supports both schemes, and the browser chooses:
:root {
color-scheme: light dark;
}With light dark, the browser uses its dark native UI exactly when prefers-color-scheme: dark matches. This pairs with dark: classes because both react to the same system setting.
Supporting light, dark and system together
The three-way toggle (light, dark, system) is the case where color-scheme is easiest to get wrong. Your script adds or removes the dark class. The dark class then sets color-scheme. So when the user picks "system", the script resolves it to a class and the CSS follows. You do not need a separate color-scheme code path in JavaScript.
If you want to be explicit, you can also set the property from the same script:
function applyTheme() {
const isDark =
localStorage.theme === "dark" ||
(!("theme" in localStorage) &&
window.matchMedia("(prefers-color-scheme: dark)").matches);
document.documentElement.classList.toggle("dark", isDark);
document.documentElement.style.colorScheme = isDark ? "dark" : "light";
}
applyTheme();Setting style.colorScheme inline is redundant if your CSS already maps .dark to color-scheme: dark. Choose one place to define it. Keeping it in CSS is easier to maintain, and the inline version is useful when you cannot edit the stylesheet.
The meta tag
Browsers also read a <meta name="color-scheme"> tag. It tells the browser which schemes the page supports before the stylesheet loads, so the initial background can be correct while the page is still loading.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="color-scheme" content="light dark" />
<script>
document.documentElement.classList.toggle(
"dark",
localStorage.theme === "dark" ||
(!("theme" in localStorage) &&
window.matchMedia("(prefers-color-scheme: dark)").matches),
);
</script>
<link rel="stylesheet" href="/styles.css" />
</head>
<body class="bg-white dark:bg-gray-900"></body>
</html>The meta tag only declares support. It does not tell the browser which scheme to use for a manually chosen theme. With light dark, the browser follows the system setting, so a user who chose dark on a light system still needs the CSS rule to get dark native UI. Use the meta tag as a loading-time hint and the CSS property as the source of truth.
How the pieces fit together
Scoping color-scheme to part of the page
Because color-scheme is inherited, you can set it on any element. This is useful when one region has a different theme from the rest of the page, such as a dark code panel on a light page:
<section class="bg-gray-900 text-gray-100 scheme-dark">
<textarea class="w-full" rows="4"></textarea>
</section>In Tailwind v3, use an arbitrary property to get the same result:
<section class="bg-gray-900 text-gray-100 [color-scheme:dark]">
<textarea class="w-full" rows="4"></textarea>
</section>The textarea inside gets dark native styling, including its scrollbar, even if the rest of the page is light.
Styling scrollbars further
color-scheme gives you the browser's built-in light or dark scrollbar. It does not let you choose exact colors. If you want a custom look, there are two standard properties:
scrollbar-colortakes a thumb color and a track color.scrollbar-widthtakesauto,thinornone.
.dark {
color-scheme: dark;
scrollbar-color: #4b5563 #111827;
}Browsers that support scrollbar-color use it. Older WebKit-based browsers need the ::-webkit-scrollbar pseudo-elements for custom scrollbars. Start with color-scheme, and add custom scrollbar colors only when the design requires it. Custom scrollbars can reduce usability, because they may make the thumb harder to see or grab, so check the contrast of the thumb against the track.
What color-scheme does not do
- It does not change colors you set yourself. A
bg-whiteinput stays white even withcolor-scheme: dark. The property only changes the browser's defaults. - It does not replace
dark:classes. Your cards, text and borders still need them. - It does not invert images or icons.
- Its exact effect differs between browsers. Date picker icons, select menus and autofill backgrounds can look different in each, so test in at least two engines.
A common source of confusion with form controls is Tailwind's preflight, the base stylesheet. Preflight resets many form element styles, such as removing borders and making backgrounds transparent. After preflight, an <input> often shows the page background through it. So a dark page with color-scheme: dark may give you a transparent input with light text, which is correct, while a page with bg-white on the input keeps a white field. If you add the @tailwindcss/forms plugin, form elements get explicit light styling, and you need to add dark: classes to them. In that case color-scheme still helps with checkboxes, pickers and scrollbars, but not with colors the plugin sets.
<input
type="text"
class="rounded border border-gray-300 bg-white px-3 py-1 text-gray-900
dark:border-gray-600 dark:bg-gray-800 dark:text-white"
/>Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Scrollbar stays light in dark mode | `color-scheme` is not set on `:root` or `html` | Add `.dark { color-scheme: dark; }` |
| Scrollbar is dark only on dark systems, ignoring the toggle | Only `light dark` is set, with no class rule | Set `color-scheme` from the `dark` class |
| Inputs have white backgrounds in dark mode | The input has an explicit `bg-white` or a forms plugin style | Add `dark:bg-*` classes |
| Date picker icon is invisible | Icon color depends on the scheme, which is not set | Set `color-scheme: dark` on the input or an ancestor |
| Flash of a white page before dark loads | Scheme is only set after the stylesheet loads | Add the `color-scheme` meta tag and run the theme script in the head |
| `scheme-dark` does nothing | Using Tailwind v3, which has no such utility | Use `[color-scheme:dark]` or plain CSS |
To check what the browser is using, open developer tools, select the <html> element and look at the Computed tab for color-scheme. Then select an element inside a form and confirm it inherits the same value.
Exercise
A user picks "dark" in your theme toggle while their operating system is set to light. Your cards turn dark, but the page scrollbar and a native date input stay light. Your stylesheet contains only :root { color-scheme: light dark; }. Why does this happen, and what is the smallest change that fixes it?
Show answer
light dark tells the browser to choose its native styling from the system setting, which is light. The toggle only adds the dark class, and nothing connects that class to color-scheme. The fix is to add .dark { color-scheme: dark; } (or dark:scheme-dark on the html element in Tailwind v4), and keep :root { color-scheme: light; } or light dark for the default state. Then the class decides the native UI.
Exercise
You use Tailwind v3 and want a dark code panel with its own dark scrollbar inside an otherwise light page. Which class do you put on the panel, and why can you not use scheme-dark?
Show answer
Use [color-scheme:dark], an arbitrary property class. The scheme-* utilities were added in Tailwind v4, so they do not exist in v3. Because color-scheme is inherited, the textarea and any scrolling content inside the panel pick up the dark native styling.