# Customize color modes You can use CSS styling and configuration changes to customize the color modes in your project. When users switch between color modes, the documentation's appearance dynamically changes to the corresponding color mode. ## Before you begin Make sure you have the following before you begin: - A Realm project with at least a few Markdown pages. - At least beginner-level CSS knowledge. ## Configure color mode styles The styling rules in this section are added to your project's `@theme/styles.css` file. Create that file if you don't have one already. ### Override CSS variables A good approach to customizing color mode styling is to override the [CSS variables](/docs/realm/branding/css-variables) used throughout your project. Add color mode customization to your project's `@theme/styles.css` file using CSS variables. The following example shows a complete color mode configuration: ```css @theme/styles.css /* Default color variables (used as fallbacks) */ :root { --color-primary: #2563eb; --color-primary-hover: #1d4ed8; --link-text-color: #1668dc; --sidebar-background-color: #f8fafc; --sidebar-active-background-color: #e2e8f0; --text-color: #1f2937; --bg-overlay: #f2f2f2; } /* Light mode specific styling */ :root.light { --sidebar-background-color: #ffffff; --sidebar-active-background-color: var(--color-purple-2); --text-color: #374151; --navbar-background-color: #ffffff; } /* Dark mode specific styling */ :root.dark { --color-primary: #60a5fa; --color-primary-hover: #3b82f6; --link-text-color: #60a5fa; --sidebar-background-color: #1e293b; --sidebar-active-background-color: #334155; --text-color: #f1f5f9; --bg-overlay: #374151; --navbar-background-color: #0f172a; } ``` In this example, the colors and backgrounds change when users switch between light and dark modes, creating a cohesive experience across both modes. #### CSS variable composition Element-specific CSS variables are often built upon another set of [core CSS variables](/docs/realm/branding/color-mode#css-variables-as-building-blocks). Consider the level of granularity you need before adding CSS variable overrides. Let's use the example from the section above to illustrate the point. The example added the following styling rules: - The `:root` selector set the `--bg-overlay` variable. - The `:root.dark` selector set the `--sidebar-active-background-color` variable. The `--bg-overlay` serves as a building block for more than 30 other CSS variables! So, while the example *did* successfully change the sidebar's active item background, it no longer matches any other elements built with `--bg-overlay`. Generally speaking, we recommend overriding the foundational CSS variables when possible to maintain a more cohesive design. Then you can override an element-specific CSS variable only when needed. ### Annotate Markdown elements Adding annotations to your Markdown elements can be used to add selectors, making them a good solution for adding color-mode-specific styling to elements that are otherwise difficult to select. To use annotation to apply styling for different color modes: 1. Use Markdoc tag syntax to add an annotation in your Markdown, as in the following example: ```markdown # Example page Some content - List item {% class="awesome-list-entry" %} - List item 2 ``` 2. In your `@theme/styles.css` file, use the selector you added to define light and dark mode styling for the element, as in the following example: ```css :root.light .awesome-list-entry { color: blue; } :root.dark .awesome-list-entry { color: red; } ``` In the example above, the first entry in the list changes between blue and red text color when the user changes the color mode. Annotations can be used to add classes or ids. ## Set color mode logo You can configure your documentation to use a different logo for each color mode. To set the logo per color mode: 1. Add two versions of a logo to an `images` folder in the root of your project. 2. Create a single, comma-separated string that contains the logo's filepath and color mode name it corresponds with. It should follow this pattern: ` , ` and contain an entry for each color mode. 3. In your project's `redocly.yaml` file, use the `srcSet` property on the `logo` object to pass the list of logos, as in the following example: ```yaml logo: srcSet: "./images/your-light-logo.svg light, ./images/your-dark-logo.svg dark" altText: Your amazing logo link: "https://example.com" ``` In the example above, the logo in the documentation navbar changes when the user switches between color modes. ## Add new color modes You can add custom color modes beyond the default light and dark modes. This creates new options in the color mode switcher and requires component ejection. ### Before you begin Make sure you have: - A Redocly project set up and running locally - Moderate comfort with React and CSS - Familiarity with [component ejection](/docs/realm/customization/eject-components) ### Eject ColorModeIcon component [Eject](/docs/realm/customization/eject-components/eject-components-using-cli) the `ColorModeIcon` component to add custom icons: ```bash npx @redocly/cli eject component 'ColorModeSwitcher/ColorModeIcon.tsx' ``` ### Add new color mode icon Add an icon for the new color mode by creating a switch case in the ejected file: ```javascript @theme/components/ColorModeSwitcher/ColorModeIcon.tsx function Icon({ mode, className }: ColorModeIconProps) { switch (mode) { case 'high-contrast': return ( ); case 'dark': return ; case 'light': return ; default: return ; } } ``` **Important**: Pass the `className` prop through to the `` element for proper styling. ### Define custom color mode styles Add styling rules for your new color mode in `@theme/styles.css`. The class name must match the color mode name: ```css @theme/styles.css :root.high-contrast { /* High contrast color scheme */ --color-primary: #000000; --text-color: #000000; --bg-color: #ffffff; --navbar-bg-color: #ffffff; --sidebar-background-color: #ffffff; --border-color: #000000; --link-text-color: #000000; } ``` ### Add to configuration Add the new color mode to your project configuration: ```yaml redocly.yaml colorMode: modes: - 'high-contrast' - 'light' - 'dark' ``` The first mode in the list is the default. See the [colorMode configuration](/docs/realm/config/color-mode) for all available options. ### Test your new color mode 1. Run `npx @redocly/cli preview` to start your local preview 2. Click the color mode switcher to toggle between modes 3. Verify the new icon appears and styles are applied correctly 4. Refine the styles in `@theme/styles.css` as needed ## Resources - **[Custom styles guide](/docs/realm/branding/customize-styles)** - Learn to customize your documentation's appearance using CSS variables and custom stylesheets for precise brand control - **[CSS variables reference](/docs/realm/branding/css-variables)** - Complete dictionary of CSS variables available for color mode customization and styling - **[Branding overview](/docs/realm/branding)** - Explore all available branding and customization approaches from basic configuration to advanced component ejection - **[Component ejection guide](/docs/realm/customization/eject-components)** - Technical guide to ejecting and customizing components like ColorModeIcon for advanced color mode features - **[Color mode configuration](/docs/realm/config/color-mode)** - Configuration reference for setting up color modes, default modes, and mode-specific options