Mermaid Flowchart Styling: Colors, classDef, linkStyle, and Themes
Mermaid gives you four levels of styling control: one node, a group of nodes, individual links, and the whole diagram. Here is when to use each, with live examples you can open in the editor.
Style one node with style
style applies CSS-like properties to a single node by its ID. It is quick, but it gets repetitive when several nodes share a look.
Highlight a single node
Common properties are fill, stroke, stroke-width, color (text), and stroke-dasharray.
flowchart LR
A[Request] --> B[Validate] --> C[Save]
style B fill:#fef3c7,stroke:#d97706,stroke-width:2px,color:#78350fStyle groups of nodes with classDef
classDef defines a reusable style. Apply it with class to a comma-separated list of nodes, or attach it inline with the ::: shorthand.
Reusable classes for success and failure paths
One class marks the happy path and another marks failures. The ::: shorthand applies a class where the node is defined.
flowchart TD
A[Checkout] --> B{Payment ok?}
B -->|Yes| C[Send receipt]:::ok
B -->|No| D[Show error]:::fail
D --> E[Retry]:::fail
classDef ok fill:#dcfce7,stroke:#16a34a,color:#14532d
classDef fail fill:#fee2e2,stroke:#dc2626,color:#7f1d1dUse classDef default to change every node that has no other class. This is the easiest way to set a base look for the whole diagram.
Style links with linkStyle
Links do not have IDs, so linkStyle refers to them by position. The first link you write is 0, the next is 1, and so on. You can list several indexes separated by commas, or use default for all links.
Emphasize the critical path
Links 0 and 2 are drawn thicker and in a different color. Every other link keeps the default style.
flowchart LR
A[Commit] --> B[Build]
B -.-> C[Lint]
B --> D[Deploy]
linkStyle 0,2 stroke:#2563eb,stroke-width:3px
linkStyle 1 stroke:#94a3b8Because indexes depend on order, adding a link near the top shifts every index below it. Keep linkStyle lines at the end of the diagram and check them after edits.
Style subgraphs
A subgraph has an ID too, so style works on it the same way as on a node:
Colored subgraph backgrounds
Give each subgraph an ID so you can style it.
flowchart LR
subgraph fe[Frontend]
UI[Web app]
end
subgraph be[Backend]
API[API] --> DB[(Database)]
end
UI --> API
style fe fill:#eff6ff,stroke:#3b82f6
style be fill:#f0fdf4,stroke:#22c55eSwitch the whole theme
Mermaid has five built-in themes: default, neutral, dark, forest, and base. Set one for a single diagram with an init directive on the first line:
The forest theme
Change forest to neutral or dark to compare. neutral works well for black-and-white printing.
%%{init: {'theme': 'forest'}}%%
flowchart LR
A[Idea] --> B[Draft] --> C[Review] --> D[Publish]Create your own palette with theme variables
Only the base theme can be customized. Set theme to base, then override themeVariables. Mermaid derives related colors, such as borders and backgrounds, from the values you set.
A custom palette with the base theme
primaryColor fills nodes, primaryBorderColor outlines them, and lineColor colors the links.
%%{init: {'theme': 'base', 'themeVariables': {'primaryColor': '#ede9fe', 'primaryBorderColor': '#7c3aed', 'primaryTextColor': '#2e1065', 'lineColor': '#7c3aed', 'fontFamily': 'Georgia, serif'}}}%%
flowchart TD
A[Research] --> B{Worth building?}
B -->|Yes| C[Prototype]
B -->|No| D[Archive]Change the link curve
The curve option controls how links are drawn. Options include basis (smooth), linear (straight segments), and step (right angles, which suit org charts and pipelines).
Right-angled links
Step curves make dense diagrams look tidier.
%%{init: {'flowchart': {'curve': 'step'}}}%%
flowchart TD
CEO --> CTO
CEO --> CFO
CTO --> Eng[Engineering]
CTO --> Ops[Operations]Which method should you use?
classDeffor anything that means something, such as status, ownership, or risk. It keeps meaning and style in one place.stylefor a one-off highlight.linkStylesparingly, because it breaks when links are reordered.- Themes and theme variables to match a brand or a documentation site, so individual diagrams need no styling at all.
Platforms such as GitHub may ignore or limit some styling, and dark-mode readers may see your colors on a dark background. Choose fills with enough contrast in both modes, or rely on themes rather than hard-coded colors.
Try it in Mermaid Preview: Open any example above in the editor and change the colors. The preview updates as you type.
Open the editor →