Sub topic 10
Scrollbars and form controls with color-scheme
This chapter assumes you already have a working dark variant, either the default prefers-color-scheme strategy or a .dark class, and that you know the Tailwind utilities used in earlier chapters. You should also be comfortable with plain CSS custom properties.
The problem: dark page, light controls
Setting dark:bg-gray-900 on a page restyles the elements you target. It does not restyle the parts of the page that the browser draws itself. Scrollbars, checkboxes, date pickers, <select> popups, and the default background of a text input all come from the browser's built-in stylesheet. That stylesheet assumes a light page unless you tell it otherwise.
The result is a dark page with a bright white scrollbar track and a light form control. It looks broken, and it is not fixed by adding more dark: classes.
What color-scheme does
The CSS color-scheme property tells the browser which color schemes an element supports. The browser then picks matching defaults for the things it draws itself. Those include:
- Scrollbars
- Form controls such as inputs, textareas, selects, and checkboxes
- The default canvas background and default text color of the page, when you have not set your own
The main values are:
| Value | Meaning |
|---|---|
| `normal` | No preference. Browsers render light. |
| `light` | Render native UI in the light style. |
| `dark` | Render native UI in the dark style. |
| `light dark` | Both are supported. The browser follows the user's system setting. |
color-scheme changes only the browser's default styling. Anything you style yourself, such as a bg-white class, still wins.
Setting it with the class strategy
If you use the manual .dark class strategy, the browser has no way to know that your class means "dark". You have to set color-scheme yourself, alongside your theme variables:
:root {
--surface: #ffffff;
--text: #111827;
color-scheme: light;
}
.dark {
--surface: #111827;
--text: #f9fafb;
color-scheme: dark;
}Because the property sits on the same selectors that switch your theme, the native controls change in step with everything else. When the toggle script adds or removes dark on <html>, the scrollbar and controls update immediately.
Setting it on <html> matters. The page-level scrollbar belongs to the root element, so color-scheme on a nested <div> will not change it.
Setting it with Tailwind utilities
Tailwind v3.x does not ship a color-scheme utility, but you do not need one. Two options work in both v3 and v4.
The first is an arbitrary property on the root element:
<html class="[color-scheme:light] dark:[color-scheme:dark]">The second is the plain CSS shown above, placed in your stylesheet. Choose the CSS version when you already use CSS variables for theming, because the two switches stay together in one place.
Tailwind v4 also provides scheme-light, scheme-dark, and scheme-light-dark utilities. In v4 you can write class="scheme-light dark:scheme-dark" on the root element.
Following the system without a class
With the default media strategy, you can let the browser handle everything. Declare that both schemes are supported:
:root {
color-scheme: light dark;
}The browser then switches native UI whenever the operating system changes. This pairs naturally with dark: classes that also follow the system.
Do not use light dark when you have a manual toggle that can disagree with the system. A user who picks light mode on a dark system would get dark scrollbars on a light page. In that case, set a single explicit value and change it with the toggle.
Seeing the difference
The diagram shows how one color-scheme value reaches the parts of the page that your own classes do not.
A quick check
Add this to a page with your dark mode active:
<html class="dark [color-scheme:dark]">
<body class="bg-gray-900 text-white">
<input type="date" class="border p-1" />
<select><option>One</option><option>Two</option></select>
<div style="height: 200vh"></div>
</body>
</html>Remove [color-scheme:dark] and reload. The date picker icon, the select popup, and the page scrollbar return to light styling. Add it back and they turn dark.
Exercise
Your site uses a manual toggle that adds a dark class to <html>. Dark mode looks right, but the page scrollbar and a <select> popup stay light. What is missing, and where should you put it?
Show answer
The color-scheme: dark declaration is missing. Add it to the .dark rule (or use dark:[color-scheme:dark] or v4's dark:scheme-dark) on the <html> element, so the browser draws native UI in its dark style whenever the class is present.
Exercise
Why is color-scheme: light dark a poor choice for a site with a three-way light, dark and system toggle?
Show answer
It makes the browser follow the operating system. If a user explicitly picks light on a dark system, your page turns light but the scrollbars and controls stay dark. Set an explicit light or dark value that changes with the toggle instead.