Quick start
The simplest way to initialize Mermaid is to callinitialize() before rendering:
startOnLoad: true, Mermaid automatically finds and renders all elements with the class mermaid when the page loads.
Initialization methods
- Automatic (startOnLoad)
- Manual (run)
- Selective rendering
- Programmatic rendering
The easiest method for static pages:Mermaid listens for the
load event and automatically processes all .mermaid elements.Configuration options
Theinitialize() function accepts a configuration object. Here are the most commonly used options:
Basic options
Security levels
Understanding security levels
Understanding security levels
- strict (default): Prevents potentially dangerous features. Tags in text are encoded, click functionality is disabled.
- loose: Allows more features but can be a security risk. Enables click events and allows some HTML.
- sandbox: Renders diagrams in a sandboxed iframe for maximum security.
Theme configuration
The run() function
Therun() function is the modern way to render diagrams:
Examples
- Custom selector
- With callback
- Error handling
- Specific nodes
The render() function
For programmatic diagram generation without DOM elements:The
render() function returns a Promise that resolves with the SVG string and optional bind functions. Multiple calls are automatically queued and executed serially.Parsing and validation
Use theparse() function to validate diagram syntax without rendering:
Deprecated: init() function
Old pattern:Framework integration patterns
- React
- Vue
- Svelte
- Angular
Best practices
- Initialize once - Call
initialize()only once, typically at application startup - Use run() for SPAs - Set
startOnLoad: falseand manually callrun()when needed - Handle errors - Always wrap render calls in try-catch or use
suppressErrors - Avoid re-initialization - Don’t call
initialize()multiple times; useupdateSiteConfig()instead - Process diagrams selectively - Use the
nodesoption for better performance with many diagrams
Next steps
Configuration
Explore detailed configuration options
Theming
Customize diagram appearance
API reference
Complete API documentation
Deployment
Learn about different deployment methods