How to Add a Dark Mode Toggle to Your Invision Theme
By Alex Rivera · Published · 4 minutes read

Dark mode is no longer a nice-to-have; a growing share of your members browse at night or on OLED screens and expect a low-light option. Building a toggle into your Invision Community theme gives them control without forcing a single look on everyone.
This guide walks through a practical implementation: define color tokens as CSS variables, add a small toggle in the header, and persist the choice per user. You do not need to duplicate your entire theme or touch core files.
Why a theme-level toggle beats a separate dark theme
Maintaining two full themes doubles your work every time you update templates or add a plugin. A single theme that swaps color variables keeps one template set and one set of customizations, so updates remain straightforward. When a new Invision Community version ships, you re-apply your variable file rather than reconciling two divergent themes.
A toggle also respects user preference. Some members want dark mode only on mobile at night; others never want it. Letting them choose avoids support requests and improves perceived performance because the switch happens instantly via CSS, without a page reload or a second theme download.
From a maintenance standpoint, a variable-based approach is easier to audit. You can see every color decision in one file, adjust contrast ratios in one place, and keep branding consistent across light and dark states. That is far safer than hunting through dozens of templates for hard-coded hex values.
- One template set to update, not two
- Instant switching without reload
- Easier contrast and branding audits
- Respects per-user preference
Step 1: Convert your palette to CSS variables
Start by identifying the colors your theme actually uses. In a typical Invision theme, that means backgrounds, surface cards, primary and secondary text, borders, links, and accent buttons. Aim for 10–16 tokens; more than that becomes hard to manage, fewer leaves gaps that force hard-coded overrides later.
Declare these tokens in your theme's custom CSS under a :root selector using the --ips prefix to avoid collisions with framework variables. For example: --ips-bg, --ips-surface, --ips-text, --ips-text-muted, --ips-border, --ips-link. Then replace hard-coded values in your custom CSS with var(--ips-bg) and so on. Do this gradually, testing each screen.
Keep the existing Invision variables in mind. Many components already reference variables like --ips-primary; you can either override those in a dark scope or map your tokens to them. Mapping is cleaner because third-party plugins that use the framework variables will inherit your dark palette automatically.
- Aim for 10–16 color tokens
- Use a consistent prefix such as --ips-
- Map tokens to existing framework variables where possible
- Test each template area after replacing values
Step 2: Define the dark palette in a scoped selector
Create a dark palette by re-declaring the same variables under a selector that only applies when dark mode is active, such as html[data-theme="dark"] or body.ipsDark. Re-declaring variables is what makes the switch instant: the browser repaints with new values and no template logic runs.
Choose dark colors deliberately. Pure black (#000) backgrounds can cause smearing on OLED and harsh contrast with white text. Most polished dark themes use a near-black or dark gray background (around #121212 to #1a1a1a) with surfaces slightly lighter (#1e1e1e to #242424). Text should be off-white (#e0e0e0 to #f0f0f0) rather than pure white to reduce eye strain.
Check contrast for body text, links, and buttons. WCAG AA requires 4.5:1 for normal text and 3:1 for large text; aim for those ratios in both palettes. Tools in your browser's inspector can sample computed colors, and you can test with a contrast checker before shipping.
- Background: #121212–#1a1a1a
- Surfaces: #1e1e1e–#242424
- Text: #e0e0e0–#f0f0f0
- Target WCAG AA contrast in both modes
Step 3: Add the toggle to your header template
Place a small button in the header, near the search or user menu. In the Invision theme templates, edit the header template and insert a button with an accessible label and an icon that reflects the current state. Keep the markup simple: a button element with a data attribute or class that your JavaScript can target.
Use a button, not a link, so keyboard users can activate it with Enter or Space. Include aria-pressed to indicate the current state and a visually hidden label if the icon alone is ambiguous. If you are already customizing navigation, see our checklist on Customizing Invision Community Header and Navigation for placement and spacing guidance.
If you use a page builder for landing pages, ensure the toggle markup is included in your global header rather than per-page. Our guide on integrating a page builder with your Invision theme covers how to keep global elements consistent across builder-driven pages.
- Use a <button> with aria-pressed
- Provide an accessible label
- Keep markup minimal and stable
- Place in the global header, not per page
Step 4: Persist the choice with JavaScript and localStorage
A few lines of JavaScript are enough: on click, toggle the data-theme attribute on the html element, update aria-pressed, and store the choice in localStorage. On page load, read the stored value and apply it before first paint where possible to avoid a flash of light mode.
Invision Community allows custom JavaScript in the theme. Add the script with a defer attribute or place it at the end of the body. Respect a user's system preference as the default: if no stored choice exists, use prefers-color-scheme to decide the initial state. This gives a sensible experience without requiring a click.
Also listen for changes to prefers-color-scheme so the theme follows the OS if the user has not explicitly chosen. Store only 'light' or 'dark'—not a third 'auto' state—to keep the logic simple, and treat the absence of a stored value as auto.
- Toggle a data-theme attribute on <html>
- Store 'light' or 'dark' in localStorage
- Default to prefers-color-scheme
- Apply early to avoid a flash
| Element | Light mode | Dark mode | Contrast target |
|---|---|---|---|
| Page background | #ffffff | #121212 | — |
| Surface / card | #f5f5f5 | #1e1e1e | — |
| Body text | #1a1a1a | #e0e0e0 | 4.5:1 against background |
| Muted text | #666666 | #a0a0a0 | 4.5:1 for normal text |
| Links | #0066cc | #66b2ff | 4.5:1 against background |
| Borders | #dddddd | #333333 | 3:1 for UI components |
Step 5: Test, document, and maintain
Test both modes across key pages: forum index, topic view, profile, search results, and any custom pages. Pay attention to embedded content, code blocks, tables, and third-party widgets that may not use your variables. For any stubborn component, add a scoped override rather than reverting to hard-coded colors.
Document the token names and where the toggle lives so future maintainers can find them. Before applying a new Invision Community release, follow a checklist to re-test the toggle; our Invision Community Theme Update Checklist covers the template and CSS areas that commonly change. And before any major edit, back up your customizations as described in How to Backup and Restore Invision Theme Customizations.
Finally, check mobile behavior. The toggle should be reachable with a thumb and not crowd the header on small screens. Our article on making your theme mobile-friendly includes breakpoint and tap-target guidance that applies directly to the toggle button.
- Test forum, topic, profile, search, custom pages
- Override third-party widgets with scoped CSS
- Document token names and toggle location
- Re-test after each platform update
- Use CSS variables for 10–16 color tokens and re-declare them under a dark selector.
- Add an accessible button in the global header and persist the choice in localStorage.
- Default to prefers-color-scheme and target WCAG AA contrast in both palettes.
- Test third-party widgets and re-check the toggle after each Invision update.
Alex has spent years building and customizing Invision Community themes for forums of all sizes. He focuses on clean code, responsive design, and performance optimization. His articles aim to make theme development accessible to beginners.


