Implementing System‑Theme‑Aware Dark Mode in Ionic with CSS Variables
Learn how to make your Ionic app automatically follow the OS dark‑mode setting using CSS variables and the prefers‑color‑scheme media query. A step‑by‑step guide, trade‑offs, and a quick test checklist.
03 May 2026, 09:13 UTC

Problem: Keeping Ionic Apps in Sync With the OS Theme
Modern users expect apps to match the dark‑ or light‑mode setting of their device. In Ionic, however, the default theme is static unless you add logic to respond to prefers-color-scheme. Without it, your app may look out of place when a user switches the system setting.
Thesis: A Zero‑Dependency, CSS‑Only Switch
Ionic exposes its palette through CSS variables such as --ion-color-background and --ion-color-text. By overriding these variables inside a @media (prefers-color-scheme: dark) block, the entire UI automatically follows the OS theme. No JavaScript, no plugin, no rebuild – just a few lines in your global stylesheet.
Step‑by‑Step Implementation
1. Create a Fresh Project (Optional)
ionic start darkDemo blank --type=angular
cd darkDemo
Run the app to confirm the default light theme:
ionic serve
2. Add Dark‑Mode Overrides
Open src/global.scss (or src/global.css if you prefer plain CSS) and insert the following:
@media (prefers-color-scheme: dark) {
:root {
--ion-color-background: #111111;
--ion-color-text: #eeeeee;
/* Optional: Override more variables for a full dark palette */
--ion-color-primary: #bb86fc;
--ion-color-secondary: #03dac6;
}
}
Explanation:
:roottargets the top‑level element, affecting all Ionic components.- Variables prefixed with
--ion-are defined by Ionic’s core CSS. Overriding them changes the default component colors. - The
prefers-color-schememedia query fires automatically when the OS switches themes.
3. Verify in the Browser
Restart ionic serve and toggle your OS dark mode (e.g., macOS System Preferences → Appearance). The app’s background and text should flip instantly. Inspect an <ion-content> element in devtools; its computed styles should show --ion-color-background: #111111 when dark mode is active.
4. Deploy to Mobile
Build the native wrappers:
ionic capacitor copy android
ionic capacitor open android
Run the app on a device or emulator. The UI will respect the system dark‑mode toggle without a page reload. The same steps apply to iOS with ionic capacitor copy ios.
Trade‑offs and Limitations
- Older Ionic Versions: Pre‑Ionic 4 projects use SASS theming and do not expose the same CSS variable API. For those, you’ll need to rebuild the theme from scratch or upgrade.
- Partial Variable Overrides: Changing only
--ion-color-backgroundand--ion-color-textcan leave other component hues (e.g., buttons, cards) in the default palette, causing visual inconsistency. Review the full list of Ionic variables in the docs and override any that affect your UI. - Performance: The media query is evaluated by the browser’s CSS engine; it adds negligible overhead. However, if you later introduce JavaScript‑based theme toggles, ensure you clean up listeners to avoid memory leaks.
Actionable Checklist
- Confirm you’re on Ionic 4+ (check
ionic info). - Open
src/global.scssand add the@media (prefers-color-scheme: dark)block. - Test in the browser by toggling OS dark mode.
- Build and run on a device to verify native integration.
- If you need a fully custom dark palette, override all variables listed in the Ionic docs.
- Commit the changes and document the theme strategy for your team.
Conclusion
By leveraging CSS variables and the prefers-color-scheme media query, Ionic developers can deliver a native‑looking dark mode that reacts instantly to system changes. The approach is lightweight, cross‑framework (Angular, React, Vue), and requires no additional dependencies. Just remember to target the full set of variables for a cohesive look and to upgrade older projects if you’re on a pre‑4 version.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.