Configuration hierarchy
When Mermaid renders a diagram, configuration is applied in the following order:- Default configuration - Built-in defaults for all options
- Site configuration - Set via
mermaid.initialize()and applied to all diagrams - Frontmatter - Diagram-level configuration in YAML format (v10.5.0+)
- Directives - Deprecated diagram-level configuration
Site configuration with initialize
Theinitialize() method sets site-wide configuration that applies to all diagrams. This should be called once when your application loads.
- JavaScript
- HTML
The
initialize() call is applied only once. Subsequent calls will not override the initial configuration.Frontmatter configuration
Frontmatter allows diagram authors to override configuration for individual diagrams. The frontmatter is a YAML block at the top of the diagram. Theconfig key can contain any configuration option except secure options (like securityLevel).
Available frontmatter options
- title - Set an accessible title for the diagram
- config - Override any non-secure configuration options
- theme - Change the theme (default, base, dark, forest, neutral)
- themeVariables - Customize theme colors and styles
Common configuration options
Top-level options
These options apply to all diagram types:| Option | Type | Default | Description |
|---|---|---|---|
theme | string | 'default' | Visual theme (default, base, dark, forest, neutral) |
fontFamily | string | '"trebuchet ms", verdana, arial, sans-serif' | Font family for all text |
logLevel | number | 5 | Logging level (1=debug, 5=fatal) |
securityLevel | string | 'strict' | Security level (strict, loose, antiscript, sandbox) |
startOnLoad | boolean | true | Automatically render diagrams on page load |
htmlLabels | boolean | true | Enable HTML in labels |
maxTextSize | number | 50000 | Maximum text size for diagrams |
maxEdges | number | 500 | Maximum number of edges in a diagram |
Diagram-specific options
Each diagram type has its own configuration namespace:- Flowchart
- Sequence
- Gantt
Resetting configuration
Thereset() method resets the current configuration back to the site configuration:
Security considerations
Certain configuration options cannot be overridden by diagram authors for security reasons. These secure options include:securityLevelsecure(the list of secure keys itself)- Any keys listed in
siteConfig.secure
Configuration flow
Best practices
- Use initialize for site-wide settings - Set common options once instead of repeating them
- Use frontmatter for diagram-specific changes - Override only what’s needed per diagram
- Avoid directive syntax - Directives are deprecated; use frontmatter instead
- Store config in variables - Avoid calling
getConfig()repeatedly - Validate your configuration - Invalid options will be silently ignored
Next steps
Directives
Learn about the deprecated directive syntax
Accessibility
Configure accessibility features