Advanced Configuration & Styling
Theming, configuration, custom styling, and troubleshooting for Mermaid diagrams.
Contents
- Configuration — init directive
- Themes and Theme Variables
- Styling — classDef, style, linkStyle
- Layout Engine — ELK renderer
- Security Levels
- Troubleshooting — special characters, debugging
- Frontmatter Configuration
- Directive Reference
- Accessibility
- Performance Tips
- Export Options
Configuration
Init Directive
Configure diagrams using the init directive. It must be immediately followed by a diagram — a directive alone renders nothing:
%%{init: { 'theme': 'dark' } }%%
flowchart LR
A --> BMulti-Line Configuration
%%{init: {
'theme': 'base',
'themeVariables': {
'primaryColor': '#3b82f6',
'primaryTextColor': '#ffffff'
}
}}%%
flowchart LR
A --> B --> CThemes
Built-in Themes
| Theme | Description |
|---|---|
default |
Default blue theme |
dark |
Dark mode |
forest |
Green nature theme |
neutral |
Grayscale |
base |
Base for customization |
Usage
%%{init: {'theme': 'forest'}}%%
flowchart LR
A --> B --> CTheme Variables
Core Variables
| Variable | Description |
|---|---|
primaryColor |
Main node color |
primaryTextColor |
Text in primary nodes |
primaryBorderColor |
Primary node border |
secondaryColor |
Secondary elements |
tertiaryColor |
Tertiary/background |
lineColor |
Edge/arrow color |
textColor |
General text |
background |
Diagram background |
Typography
| Variable | Description |
|---|---|
fontSize |
Base font size |
fontFamily |
Font family |
Custom Theme Example
%%{init: {
'theme': 'base',
'themeVariables': {
'primaryColor': '#3b82f6',
'primaryTextColor': '#ffffff',
'primaryBorderColor': '#2563eb',
'secondaryColor': '#10b981',
'tertiaryColor': '#f1f5f9',
'lineColor': '#64748b',
'textColor': '#1e293b',
'fontSize': '16px',
'fontFamily': 'Inter, sans-serif'
}
}}%%
flowchart LR
A[Start] --> B{Decision}
B -->|Yes| C[Success]
B -->|No| D[Failure]Diagram-Specific Variables
Flowchart
| Variable | Description |
|---|---|
nodeBorder |
Node border color |
nodeTextColor |
Node text |
clusterBkg |
Subgraph background |
clusterBorder |
Subgraph border |
edgeLabelBackground |
Edge label background |
Sequence Diagram
| Variable | Description |
|---|---|
actorBorder |
Actor border |
actorBkg |
Actor background |
actorTextColor |
Actor text |
activationBorderColor |
Activation border |
activationBkgColor |
Activation background |
signalColor |
Arrow/signal color |
signalTextColor |
Message text |
noteBkgColor |
Note background |
noteBorderColor |
Note border |
noteTextColor |
Note text |
State Diagram
| Variable | Description |
|---|---|
labelColor |
State label |
altBackground |
Composite state background |
Gantt Chart
| Variable | Description |
|---|---|
gridColor |
Grid lines |
todayLineColor |
Today marker |
taskTextColor |
Task text |
doneTaskBkgColor |
Completed task |
activeTaskBkgColor |
Active task |
critBkgColor |
Critical path |
taskBorderColor |
Task border |
Styling
Class-Based Styling
Define Classes
flowchart LR
A[Start]:::success --> B[Process]:::info --> C[End]:::warning
classDef success fill:#10b981,stroke:#059669,color:white
classDef info fill:#3b82f6,stroke:#2563eb,color:white
classDef warning fill:#f59e0b,stroke:#d97706,color:whiteApply to Multiple Nodes
flowchart LR
A --> B --> C --> D
class A,D success
class B,C info
classDef success fill:#10b981
classDef info fill:#3b82f6Default Class
flowchart LR
A --> B --> C
classDef default fill:#f8fafc,stroke:#cbd5e1Individual Node Styling
flowchart LR
A --> B --> C
style A fill:#10b981,stroke:#059669,color:white
style B fill:#3b82f6,stroke:#2563eb,color:white
style C fill:#ef4444,stroke:#dc2626,color:whiteStyle Properties
| Property | Example |
|---|---|
fill |
fill:#3b82f6 |
stroke |
stroke:#2563eb |
stroke-width |
stroke-width:2px |
stroke-dasharray |
stroke-dasharray:5,5 |
color |
color:white |
font-weight |
font-weight:bold |
Link Styling
Individual Links
flowchart LR
A --> B --> C --> D
linkStyle 0 stroke:green,stroke-width:2px
linkStyle 1 stroke:blue,stroke-width:2px
linkStyle 2 stroke:red,stroke-width:2px,stroke-dasharray:5All Links
flowchart LR
A --> B --> C
linkStyle default stroke:gray,stroke-width:1pxLayout Engine
ELK Renderer
For complex diagrams (v9.4+):
%%{init: {"flowchart": {"defaultRenderer": "elk"}} }%%
flowchart TB
A --> B & C & D
B & C & D --> E
E --> F & GBenefits:
- Better handling of complex layouts
- More predictable edge routing
- Improved subgraph positioning
Security Levels
Control what Mermaid can do:
| Level | Description |
|---|---|
strict |
Most secure, no HTML/JS |
loose |
Allows some interaction |
antiscript |
Allows HTML, blocks scripts |
sandbox |
iframe sandbox |
%%{init: { 'securityLevel': 'loose' }}%%
flowchart LR
A --> B
click A href "https://example.com" _blankTroubleshooting
Common Issues
Special Characters
Escape with HTML entities or quotes:
flowchart LR
A["Node with #quot;quotes#quot;"]
B["Arrow -> symbol"]
C["Hash #35; symbol"]HTML Entities
| Char | Entity |
|---|---|
# |
#35; |
" |
#quot; |
< |
#lt; |
> |
#gt; |
& |
#amp; |
{ |
#123; |
} |
#125; |
Long Labels
Use markdown strings:
flowchart LR
A["`This is a very long
label that wraps
across multiple lines`"]Debugging Tips
1. Check Syntax
- Verify diagram type declaration
- Check for unclosed brackets/quotes
- Ensure arrow syntax matches diagram type
2. Test Incrementally
- Start with minimal diagram
- Add elements one at a time
- Identify breaking change
3. Use Live Editor
Test at: https://mermaid.live
4. Platform Differences
- Check target platform support
- Some features are version-specific
- Export to PNG/SVG for guaranteed rendering
Arrow Syntax by Diagram Type
| Diagram | Solid arrow | Async/open | Dotted |
|---|---|---|---|
| Flowchart | --> |
N/A | -.-> |
| Sequence | ->> (sync) |
-) |
-->> (response) |
| Class | --> |
N/A | ..> |
| State | --> |
N/A | N/A |
Arrow syntax does not transfer between diagram types — ->> in a flowchart or -.-> in a sequence diagram are parse errors.
Frontmatter Configuration
Alternative to init directive:
---
title: My Diagram
config:
theme: forest
flowchart:
defaultRenderer: elk
---flowchart LR
A --> BDirective Reference
Diagram Directives
| Diagram | Directive |
|---|---|
| All | %%{init: {...}}%% |
| Flowchart | flowchart config |
| Sequence | sequenceDiagram config |
| Class | classDiagram config |
| State | stateDiagram config |
| ER | erDiagram config |
| Gantt | gantt config |
Common Init Options
%%{init: {
'theme': 'default',
'themeVariables': { ... },
'flowchart': {
'defaultRenderer': 'elk',
'curve': 'basis',
'padding': 15
},
'sequence': {
'showSequenceNumbers': true,
'actorMargin': 50,
'boxMargin': 10
},
'gantt': {
'barHeight': 20,
'fontSize': 11,
'sectionFontSize': 14
}
}}%%Accessibility
accTitle and accDescr
Mermaid's built-in accessibility fields render as SVG <title>/<desc> for screen readers:
flowchart LR
accTitle: Login flow
accDescr: User submits credentials, the app validates them against the auth service
A[User] --> B[App] --> C[Auth]Alt Text
Also provide context in prose before diagrams:
The following diagram shows the authentication flow:
```mermaid
sequenceDiagram
User->>App: Login
App->>Auth: Validate
## ARIA Labels
When embedding in HTML:
```html
<div class="mermaid" role="img" aria-label="Authentication flow diagram">
sequenceDiagram
User->>App: Login
</div>Performance Tips
- Limit complexity - Split large diagrams
- Use ELK for complex layouts
- Minimize styling - Class-based over inline
- Cache renders when possible
- Lazy load in documentation
Export Options
From Live Editor
- PNG (transparent or white background)
- SVG (scalable)
- Markdown
Programmatic
import mermaid from 'mermaid';
const svg = await mermaid.render('id', diagramText);CLI
# Single diagram file -> SVG (also .png, .pdf)
npx -y @mermaid-js/mermaid-cli -i diagram.mmd -o diagram.svg
# Markdown file: renders every ```mermaid block to an image, rewrites links
npx -y @mermaid-js/mermaid-cli -i input.md -o output.mdThe CLI is also the fastest way to validate generated diagrams — a syntax error exits non-zero with a parse message.