Overview
This article explains how to automatically switch light and dark modes for any custom HTML In-app template. You can use this approach for any HTML structure, ensuring that you can adapt your own In-app message templates to respect the user’s system color scheme preference. To make your custom HTML template automatically switch between light and dark modes based on the user’s system settings, you can use CSS media queries. This lightweight approach ensures your In-app message adapts seamlessly to the user’s preferences. The key CSS tools you’ll use are theprefers-color-scheme media query and the color-scheme property.
- The
prefers-color-schememedia query: This CSS feature detects whether the user’s system is set to light or dark mode. You can use the media query to apply different styles depending on the system setting to deliver a polished and user-friendly In-app message experience. The media query works in modern browsers and webviews, ensuring compatibility with In-app messages. - The
color-schemeproperty: This property instructs the browser that your template supports both light and dark modes, which can improve how certain elements (like form controls) are rendered.
With these tools, your template will dynamically adjust its appearance based on the user’s system preference, improving usability and accessibility.
Steps to Implement
Step 1: Start with Your Custom HTML Template
Use your existing HTML code as the foundation. You don’t need separate files for light and dark modes. The CSS will handle the switching within a single template. For more information on In-app HTML templates, refer to HTML In-App Templates and Sample HTML In-app Templates.Step 2: Structure Your CSS for Light and Dark Modes
Organize your CSS to define styles for both light and dark modes:- Define base styles: These are the default styles that apply when no specific color scheme is detected or when the user’s system is in the light mode. Typically, these will be your light mode styles. Include styles for all key elements in your template, such as backgrounds, text, buttons, and containers.
- Add dark mode styles with media queries: To override or add styles specifically for dark mode, use the
@media (prefers-color-scheme: dark)query. These styles will be activated when the user’s system is set to dark mode.
Some Key Points
In the code above:- Base styles: These apply by default and should cover all elements in your template for the light mode.
- Dark mode styles: The media query adjusts styles for the dark mode, ensuring a smooth transition when the user’s preference changes.
- Flexibility: Adapt the class names (for example, container and button) and properties to match your template’s structure.
Step 3: Use the color-scheme Property
Add thecolor-scheme property to the root of your CSS to signal that your template supports both modes:
CSS
- Help the browser understand that your content is ready for both light and dark modes.
- Automatically adjusts native elements (For example, form inputs, scrollbars) to match the user’s preference.
Step 4: Customize Styles for Your Template’s Elements
Since your HTML template is unique, identify the elements that need to adapt to the color scheme. Common elements of style include:- Backgrounds: Body, sections, modals, or other containers.
- Text: Headings, paragraphs, links, or labels.
- Interactive elements: Buttons, forms, or navigation items.
- Overlays: Modal backdrops or pop-ups.
- In the base styles (light mode), use colors that work well with a light background (for example, dark text on light backgrounds).
- In the dark mode media query, adjust colors for dark backgrounds (for example, light text on dark backgrounds).


Step 5. Link the CSS to Your HTML
Ensure your CSS is linked to your HTML file in the<head> section:
HTML
Step 6. Test Your Implementation
Test your template in both modes to ensure it works as expected:- Light mode: Set your system to light mode and check that the base styles are applied correctly.

- Dark mode: Switch to dark mode and verify that the media query styles override the base styles appropriately.

Example
Here’s an example template to show how this might look with a custom HTML structure: HTML- The base styles define a light theme.
- The dark mode media query adjusts the styles for a dark theme.
Additional Considerations
Handling Images and Icons
If your template includes images or icons that don’t look good in both modes (for example, a dark icon on a dark background):- Provide alternative versions for each mode and swap them using CSS.
- Use a CSS filter to adjust visibility.