This page covers CSS and JavaScript customization:
For server-rendered reusable elements in your MDX content, see Custom React components. To replace Fern’s default header or footer, see Custom header and footer.
You can also customize many things directly in your docs.yml file, including colors, typography, navbar links, layout, analytics, and metadata. Try these built-in options first before adding custom code.
You can add custom CSS to your docs to further customize the look and feel. The defined class names are applied across all MDX files. See the CSS selectors reference for a complete list of available .fern-* selectors.
For customizing the background, logo, font, and layout of your Docs via Fern’s built-in styling, check out the Global Configuration.
Fern automatically generates CSS variables from your docs.yml colors configuration and makes them available as CSS custom properties in your stylesheets. Colors are converted to oklch for consistent color management, with hex fallbacks for unsupported browsers. These variables adapt automatically between light and dark modes — light mode uses the .light and :root selectors, while dark mode uses the .dark selector.
--accent-1 through --accent-12 — accent color scale (generated from accent-primary)--accent-a1 through --accent-a12 — accent color scale with alpha transparency--grayscale-1 through --grayscale-12 — grayscale color scale--grayscale-a1 through --grayscale-a12 — grayscale with alpha transparencyFern also generates named color scales that follow Radix UI’s step paradigms. These match your accent-primary hue and include scales like --red-1 through --red-12, --blue-1 through --blue-12, and so on for other colors.
--background — page background color--card-background — card background color--border — border color--sidebar-background — sidebar background color--header-background — header background color--accent — primary accent colorThe accordions below show common patterns for using these variables to theme backgrounds, cards, text, and images.
Use CSS variables to create backgrounds that automatically adapt to the theme. Override with .dark selector only when you need theme-specific styling.
Cards automatically adapt to theme changes when using CSS variables. Add explicit dark mode overrides only for properties like shadows that need theme-specific values.
Text colors automatically adapt when using Fern’s grayscale and accent variables. Use --grayscale-12 for primary text, --grayscale-a11 for secondary text, and --accent-11 for links.
There are multiple approaches for adapting images to light and dark modes.
SVG icons with currentColor (recommended):
Swapping background images:
Using picture element with prefers-color-scheme:
The prefers-color-scheme media query follows the operating system theme preference and may not match a manual theme toggle on your site. For perfect alignment with Fern’s theme switcher, use the .dark selector approach instead.
Using CSS filters (last resort):
Avoid hardcoding hex colors in your custom CSS. Always use Fern’s CSS variables to maintain proper contrast and theme consistency.
You can use custom CSS to hide specific Fern docs components that you don’t want to display.
You can target other Fern UI components using their CSS class names. See the CSS selectors reference for all available selectors, or use your browser’s developer tools to inspect elements.
You can use custom CSS to create brand-specific styling for tables, components, and other elements in your documentation.
You can add CSS directly within an MDX page using a <style> tag. This is useful for page-specific styles that don’t need to be applied globally.
The CSS must be wrapped in curly braces and backticks ({``}) to be valid JSX. Use the .dark selector prefix to define styles for dark mode.
Customize the behavior of your Docs site by injecting custom JavaScript globally.Add a custom.js file and include it in your fern/ project:
In docs.yml, specify the path to the custom.js file:
You can also specify multiple custom JS files stored locally and remote:
We use path for local sources and url for remote sources.
Optionally, specify the strategy for each custom JavaScript file. Choose from beforeInteractive, afterInteractive (default), and lazyOnload.
Third-party integrations: For tools not natively supported in docs.yml, add analytics, session recording, support widgets, or tag managers by pasting their embed snippets into your custom JS file. See Integrating third-party tools for supported tools and examples.
Custom search: Implement custom search (also requires your Algolia credentials)
Scripts and widgets: Insert any client-side scripts or embeddable widgets