← All guides

Mermaid theme customisation

Last updated: 2026-05-01

Mermaid ships with four themes — default, dark, forest, and neutral. They cover most needs, and most projects should pick one and stick with it. But sometimes you need a slight tweak — a brand colour on the primary node, slightly larger fonts, a softer background — and Mermaid has a way to do that without a custom CSS file.

Picking a built-in theme

The four built-ins look distinct enough that most people can pick by eye. Quick guide:

In our preview, the theme dropdown lets you flip between all four without re-typing the diagram. Once you commit, you can lock the theme into the source itself with a directive (next section).

Locking a theme into the diagram source

Add a %%{init: ...}%% directive at the very top of the diagram, before the first keyword:

%%{init: {'theme': 'dark'}}%%
flowchart TD
  A --> B

That makes the theme part of the diagram. Wherever it renders — Notion, GitHub, your own static site — the theme follows. This is especially useful for dark documentation systems that wrap your light-themed diagrams in a dark page; locking theme: 'dark' keeps the diagram readable.

If you don’t want to lock the theme, leave the directive out and let the renderer pick. GitHub will pick based on the visitor’s GitHub theme, Notion will pick based on workspace theme, and so on.

Theme variables — a small change without a custom theme

Sometimes you want one colour different. The themeVariables block lets you override individual variables without redefining the whole theme:

%%{init: {
  'theme': 'default',
  'themeVariables': {
    'primaryColor': '#fff7ed',
    'primaryBorderColor': '#fb923c',
    'primaryTextColor': '#9a3412'
  }
}}%%
flowchart LR
  A[Primary] --> B[Secondary]

Useful variables, in rough order of how often you’ll touch them:

VariableWhat it controls
primaryColorFill of the most prominent node type
primaryBorderColorBorder of primary nodes
primaryTextColorText inside primary nodes
lineColorEdge / connector colour
backgroundPage background of the rendered SVG
mainBkgBackground fill behind the diagram
secondaryColorSecondary node fill
tertiaryColorTertiary node fill
fontFamilyFont for all text

The full list is documented at mermaid.js.org/config/theming.html.

When to write a fully custom theme

Probably never. Maintaining a custom theme means owning Mermaid version drift — when v12 ships, your theme variables may move or get renamed. Most of the time, you actually want one of:

Common mistakes

  1. Setting theme after the keyword. The directive must come before the diagram type keyword. Putting it on a later line silently does nothing.
  2. Mixing themeCSS and themeVariables. They are two different escape hatches. Use one or the other; mixing them produces hard-to-debug overrides.
  3. Forgetting securityLevel. Your hosted site should set securityLevel: 'strict' next to theme, or you have an XSS hole open. See our glossary entry.

Open the live preview · Diagram types · More guides