Skip to main content
Mermaid provides extensive configuration options to control diagram rendering, appearance, and behavior. Configuration can be applied globally, per-site, or per-diagram.

Configuration hierarchy

Mermaid uses a layered configuration system:
  1. Default configuration - Built-in defaults from the library
  2. Site configuration - Set via initialize() or setSiteConfig()
  3. Directive configuration - Per-diagram using %%{init: {}}%% syntax
  4. Frontmatter configuration - Per-diagram using YAML frontmatter
Later configurations override earlier ones, with frontmatter having the highest priority.

Setting configuration

Global configuration options

Core options

{
  // Automatically render diagrams on page load
  startOnLoad: true,
  
  // Maximum text length allowed in diagrams
  maxTextSize: 50000,
  
  // Use deterministic IDs for reproducible output
  deterministicIds: false,
  deterministicIDSeed: undefined,
  
  // Font family for all text
  fontFamily: '"trebuchet ms", verdana, arial, sans-serif',
  
  // Font size
  fontSize: 16,
  
  // Logging level: 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal'
  logLevel: 'fatal',
  
  // Security level: 'strict' | 'loose' | 'sandbox'
  securityLevel: 'strict',
  
  // Theme: 'default' | 'dark' | 'forest' | 'neutral' | 'base'
  theme: 'default',
  
  // Whether to use HTML labels globally
  htmlLabels: true,
}

Security configuration

The securityLevel option has important security implications when rendering user-generated content.
mermaid.initialize({
  // strict: Prevents dangerous features, encodes tags
  // loose: Allows more features (security risk with user content)
  // sandbox: Renders in sandboxed iframe (safest)
  securityLevel: 'strict',
  
  // Additional security options
  secure: [
    // Keys that cannot be overridden by directives/frontmatter
    'secure',
    'securityLevel',
    'startOnLoad',
    'maxTextSize'
  ]
});

Visual options

mermaid.initialize({
  theme: 'default',
  
  // Custom theme variables (requires theme: 'base')
  themeVariables: {
    primaryColor: '#BB2528',
    primaryTextColor: '#fff',
    primaryBorderColor: '#7C0000',
    lineColor: '#F8B229',
    secondaryColor: '#006100',
    tertiaryColor: '#fff'
  },
  
  // Custom CSS (advanced)
  themeCSS: '.node rect { fill: #f9f; }',
  
  // Custom font family
  fontFamily: 'Arial, sans-serif',
  
  // Font size in pixels
  fontSize: 16
});

Layout and rendering

mermaid.initialize({
  // Layout engine: 'dagre' | 'elk'
  layout: 'dagre',
  
  // Visual look: 'classic' | 'handDrawn'
  look: 'classic',
  
  // ELK layout options (when layout: 'elk')
  elk: {
    mergeEdges: false,
    nodePlacementStrategy: 'BRANDES_KOEPF', // 'SIMPLE' | 'NETWORK_SIMPLEX' | 'LINEAR_SEGMENTS' | 'BRANDES_KOEPF'
  }
});

Diagram-specific configuration

Each diagram type has its own configuration section:
mermaid.initialize({
  flowchart: {
    // Curve style: 'basis' | 'linear' | 'cardinal'
    curve: 'basis',
    
    // Padding around the diagram
    padding: 15,
    
    // Use HTML labels in nodes
    htmlLabels: true,
    
    // Default arrow marker
    defaultRenderer: 'dagre-wrapper',
    
    // Rank direction
    rankSpacing: 50,
    nodeSpacing: 50,
    
    // Wrapping width for node labels
    wrappingWidth: 200
  }
});

Configuration API

Mermaid exposes several functions for working with configuration:
import {
  initialize,        // Set initial configuration
  getConfig,        // Get current configuration
  setConfig,        // Update current configuration  
  getSiteConfig,    // Get site-level configuration
  setSiteConfig,    // Set site-level configuration
  updateSiteConfig, // Update site-level configuration
  reset,            // Reset to site configuration
  defaultConfig     // Get default configuration
} from 'mermaid';

// Initialize (call once at startup)
mermaid.initialize({
  theme: 'dark',
  logLevel: 'error'
});

// Get current configuration
const config = getConfig();
console.log(config.theme); // 'dark'

// Update site config at runtime
updateSiteConfig({
  theme: 'forest'
});

// Reset to site config (clears directive/frontmatter overrides)
reset();

// Reset to specific config
reset(defaultConfig);
Avoid calling getConfig() repeatedly. Store the result in a variable and reuse it. The function creates a deep copy each time it’s called.

Configuration best practices

1. Initialize once

Call initialize() only once at application startup:
// Good
mermaid.initialize({ theme: 'dark' });

// Bad - don't re-initialize
mermaid.initialize({ theme: 'dark' });
// ... later ...
mermaid.initialize({ theme: 'forest' }); // Don't do this

2. Use updateSiteConfig for runtime changes

// Good
import { updateSiteConfig } from 'mermaid';
updateSiteConfig({ theme: 'forest' });

// Avoid
mermaid.initialize({ theme: 'forest' }); // Re-initializing

3. Prefer frontmatter over directives

Frontmatter is more readable and maintainable: Versus:

4. Secure user-generated content

When rendering untrusted diagrams:
mermaid.initialize({
  securityLevel: 'strict',
  secure: [
    'secure',
    'securityLevel',
    'startOnLoad',
    'maxTextSize'
  ]
});

5. Validate configuration

Mermaid silently ignores invalid configuration keys. Use TypeScript for type safety:
import type { MermaidConfig } from 'mermaid';

const config: MermaidConfig = {
  theme: 'dark',
  // TypeScript will catch typos
  // them: 'dark', // Error: Object literal may only specify known properties
};

mermaid.initialize(config);

Common configuration patterns

mermaid.initialize({
  theme: 'dark',
  themeVariables: {
    darkMode: true,
    background: '#1e1e1e',
    primaryColor: '#4dabf7',
    secondaryColor: '#748ffc',
    tertiaryColor: '#f783ac',
    primaryTextColor: '#fff',
    secondaryTextColor: '#fff',
    tertiaryTextColor: '#fff',
    lineColor: '#f8f8f8',
    textColor: '#f8f8f8'
  }
});

Deprecated options

Some configuration options have been deprecated. Mermaid will log warnings when these are used.
// Deprecated
{
  flowchart: {
    htmlLabels: true // Use global htmlLabels instead
  },
  lazyLoadedDiagrams: [], // Use registerExternalDiagrams instead
  loadExternalDiagramsAtStartup: false // Use registerExternalDiagrams instead
}

// Preferred
{
  htmlLabels: true
}

Next steps

Theming

Customize colors and visual appearance

Initialization

Learn about initialization patterns

API reference

Complete configuration API reference

Security

Learn about security best practices