Mermaid Diagramming
Create professional software diagrams using Mermaid's text-based syntax. Mermaid renders diagrams from simple text definitions, making diagrams version-controllable, easy to update, and maintainable alongside code.
Core Syntax Structure
All Mermaid diagrams follow this pattern:
diagramType
definition content
Key principles:
- First line declares diagram type (e.g.,
classDiagram, sequenceDiagram, flowchart)
- Use
%% for comments
- Line breaks and indentation improve readability but aren't required
- Unknown words break diagrams; parameters fail silently
Diagram Type Selection Guide
Choose the right diagram type:
Class Diagrams - Domain modeling, OOP design, entity relationships
- Domain-driven design documentation
- Object-oriented class structures
- Entity relationships and dependencies
Sequence Diagrams - Temporal interactions, message flows
- API request/response flows
- User authentication flows
- System component interactions
- Method call sequences
Flowcharts - Processes, algorithms, decision trees
- User journeys and workflows
- Business processes
- Algorithm logic
- Deployment pipelines
Entity Relationship Diagrams (ERD) - Database schemas
- Table relationships
- Data modeling
- Schema design
C4 Diagrams - Software architecture at multiple levels
- System Context (systems and users)
- Container (applications, databases, services)
- Component (internal structure)
- Code (class/interface level)
State Diagrams - State machines, lifecycle states
Git Graphs - Version control branching strategies
Gantt Charts - Project timelines, sprints, phases, milestones, dependencies → references/gantt-diagrams.md
Kanban - Workflow stages, task boards (Todo / In Progress / Done), pipeline columns → references/kanban-diagrams.md
User Journey - User experience mapping, satisfaction scoring → references/user-journey-diagrams.md
Pie/Bar Charts - Data visualization
Quick Start Examples
Class Diagram (Domain Model)
classDiagram
Title -- Genre
Title *-- Season
Title *-- Review
User --> Review : creates
class Title {
+string name
+int releaseYear
+play()
}
class Genre {
+string name
+getTopTitles()
}
Sequence Diagram (API Flow)
sequenceDiagram
participant User
participant API
participant Database
User->>API: POST /login
API->>Database: Query credentials
Database-->>API: Return user data
alt Valid credentials
API-->>User: 200 OK + JWT token
else Invalid credentials
API-->>User: 401 Unauthorized
end
Flowchart (User Journey)
flowchart TD
Start([User visits site]) --> Auth{Authenticated?}
Auth -->|No| Login[Show login page]
Auth -->|Yes| Dashboard[Show dashboard]
Login --> Creds[Enter credentials]
Creds --> Validate{Valid?}
Validate -->|Yes| Dashboard
Validate -->|No| Error[Show error]
Error --> Login
ERD (Database Schema)
erDiagram
USER ||--o{ ORDER : places
ORDER ||--|{ LINE_ITEM : contains
PRODUCT ||--o{ LINE_ITEM : includes
USER {
int id PK
string email UK
string name
datetime created_at
}
ORDER {
int id PK
int user_id FK
decimal total
datetime created_at
}
Detailed References
For in-depth guidance on specific diagram types, see:
- references/flowcharts.md - Node shapes, connections, decision logic, subgraphs, styling
- references/gantt-diagrams.md - Tasks, sections, milestones, dateFormat/axisFormat, excludes, vert markers
- references/kanban-diagrams.md - Columns, tasks, metadata (assigned, ticket, priority), ticketBaseUrl
- references/sequence-diagrams.md - Actors, participants, messages (sync/async), activations, loops, alt/opt/par blocks, notes
- references/class-diagrams.md - Domain modeling, relationships (association, composition, aggregation, inheritance), multiplicity, methods/properties
- references/erd-diagrams.md - Entities, relationships, cardinality, keys, attributes
- references/user-journey-diagrams.md - Sections, tasks, actor satisfaction scoring
- references/c4-diagrams.md - System context, container, component diagrams, boundaries
- references/architecture-diagrams.md - Cloud services, infrastructure, CI/CD deployments
- references/advanced-features.md - Themes, styling, configuration, layout options
Best Practices
- Start Simple - Begin with core entities/components, add details incrementally
- Use Meaningful Names - Clear labels make diagrams self-documenting
- Comment Extensively - Use
%% comments to explain complex relationships
- Keep Focused - One diagram per concept; split large diagrams into multiple focused views
- Version Control - Store
.mmd files alongside code for easy updates
- Add Context - Include titles and notes to explain diagram purpose
- Iterate - Refine diagrams as understanding evolves
Configuration and Theming
Configure diagrams using frontmatter:
---
config:
theme: base
themeVariables:
primaryColor: "#ff6b6b"
---
flowchart LR
A --> B
Available themes: default, forest, dark, neutral, base
Layout options:
layout: dagre (default) - Classic balanced layout
layout: elk - Advanced layout for complex diagrams (requires integration)
Look options:
look: classic - Traditional Mermaid style
look: handDrawn - Sketch-like appearance
Exporting and Rendering
Native support in:
- GitHub/GitLab - Automatically renders in Markdown
- VS Code - With Markdown Mermaid extension
- Notion, Obsidian, Confluence - Built-in support
Export options:
- Mermaid Live Editor - Online editor with PNG/SVG export
- Mermaid CLI -
npm install -g @mermaid-js/mermaid-cli then mmdc -i input.mmd -o output.png
- Docker -
docker run --rm -v $(pwd):/data minlag/mermaid-cli -i /data/input.mmd -o /data/output.png
Common Pitfalls
- Breaking characters - Avoid
{} in comments, use proper escape sequences for special characters
- Syntax errors - Misspellings break diagrams; validate syntax in Mermaid Live
- Overcomplexity - Split complex diagrams into multiple focused views
- Missing relationships - Document all important connections between entities
When to Create Diagrams
Always diagram when:
- Starting new projects or features
- Documenting complex systems
- Explaining architecture decisions
- Designing database schemas
- Planning refactoring efforts
- Onboarding new team members
Use diagrams to:
- Align stakeholders on technical decisions
- Document domain models collaboratively
- Visualize data flows and system interactions
- Plan before coding
- Create living documentation that evolves with code
1---2name: mermaid-diagrams3description: Comprehensive guide for creating software diagrams using Mermaid syntax. Use when users need to create, visualize, or document software through diagrams including class diagrams (domain modeling, object-oriented design), sequence diagrams (application flows, API interactions, code execution), flowcharts (processes, algorithms, user journeys), entity relationship diagrams (database schemas), C4 architecture diagrams (system context, containers, components), gantt charts (project timelines, sprints, milestones), kanban diagrams (workflow stages, task boards), user journey diagrams, state diagrams, git graphs, pie charts, or any other diagram type. Triggers include requests to "diagram", "visualize", "model", "map out", "show the flow", or when explaining system architecture, database design, code structure, or user/application flows.4---56# Mermaid Diagramming78Create professional software diagrams using Mermaid's text-based syntax. Mermaid renders diagrams from simple text definitions, making diagrams version-controllable, easy to update, and maintainable alongside code.910## Core Syntax Structure1112All Mermaid diagrams follow this pattern:1314```mermaid15diagramType16 definition content17```1819**Key principles:**20- First line declares diagram type (e.g., `classDiagram`, `sequenceDiagram`, `flowchart`)21- Use `%%` for comments22- Line breaks and indentation improve readability but aren't required23- Unknown words break diagrams; parameters fail silently2425## Diagram Type Selection Guide2627**Choose the right diagram type:**28291. **Class Diagrams** - Domain modeling, OOP design, entity relationships30 - Domain-driven design documentation31 - Object-oriented class structures32 - Entity relationships and dependencies33342. **Sequence Diagrams** - Temporal interactions, message flows35 - API request/response flows36 - User authentication flows37 - System component interactions38 - Method call sequences39403. **Flowcharts** - Processes, algorithms, decision trees41 - User journeys and workflows42 - Business processes43 - Algorithm logic44 - Deployment pipelines45464. **Entity Relationship Diagrams (ERD)** - Database schemas47 - Table relationships48 - Data modeling49 - Schema design50515. **C4 Diagrams** - Software architecture at multiple levels52 - System Context (systems and users)53 - Container (applications, databases, services)54 - Component (internal structure)55 - Code (class/interface level)56576. **State Diagrams** - State machines, lifecycle states587. **Git Graphs** - Version control branching strategies598. **Gantt Charts** - Project timelines, sprints, phases, milestones, dependencies → [references/gantt-diagrams.md](references/gantt-diagrams.md)609. **Kanban** - Workflow stages, task boards (Todo / In Progress / Done), pipeline columns → [references/kanban-diagrams.md](references/kanban-diagrams.md)6110. **User Journey** - User experience mapping, satisfaction scoring → [references/user-journey-diagrams.md](references/user-journey-diagrams.md)6211. **Pie/Bar Charts** - Data visualization6364## Quick Start Examples6566### Class Diagram (Domain Model)67```mermaid68classDiagram69 Title -- Genre70 Title *-- Season71 Title *-- Review72 User --> Review : creates7374 class Title {75 +string name76 +int releaseYear77 +play()78 }7980 class Genre {81 +string name82 +getTopTitles()83 }84```8586### Sequence Diagram (API Flow)87```mermaid88sequenceDiagram89 participant User90 participant API91 participant Database9293 User->>API: POST /login94 API->>Database: Query credentials95 Database-->>API: Return user data96 alt Valid credentials97 API-->>User: 200 OK + JWT token98 else Invalid credentials99 API-->>User: 401 Unauthorized100 end101```102103### Flowchart (User Journey)104```mermaid105flowchart TD106 Start([User visits site]) --> Auth{Authenticated?}107 Auth -->|No| Login[Show login page]108 Auth -->|Yes| Dashboard[Show dashboard]109 Login --> Creds[Enter credentials]110 Creds --> Validate{Valid?}111 Validate -->|Yes| Dashboard112 Validate -->|No| Error[Show error]113 Error --> Login114```115116### ERD (Database Schema)117```mermaid118erDiagram119 USER ||--o{ ORDER : places120 ORDER ||--|{ LINE_ITEM : contains121 PRODUCT ||--o{ LINE_ITEM : includes122123 USER {124 int id PK125 string email UK126 string name127 datetime created_at128 }129130 ORDER {131 int id PK132 int user_id FK133 decimal total134 datetime created_at135 }136```137138## Detailed References139140For in-depth guidance on specific diagram types, see:141142- **[references/flowcharts.md](references/flowcharts.md)** - Node shapes, connections, decision logic, subgraphs, styling143- **[references/gantt-diagrams.md](references/gantt-diagrams.md)** - Tasks, sections, milestones, dateFormat/axisFormat, excludes, vert markers144- **[references/kanban-diagrams.md](references/kanban-diagrams.md)** - Columns, tasks, metadata (assigned, ticket, priority), ticketBaseUrl145- **[references/sequence-diagrams.md](references/sequence-diagrams.md)** - Actors, participants, messages (sync/async), activations, loops, alt/opt/par blocks, notes146- **[references/class-diagrams.md](references/class-diagrams.md)** - Domain modeling, relationships (association, composition, aggregation, inheritance), multiplicity, methods/properties147- **[references/erd-diagrams.md](references/erd-diagrams.md)** - Entities, relationships, cardinality, keys, attributes148- **[references/user-journey-diagrams.md](references/user-journey-diagrams.md)** - Sections, tasks, actor satisfaction scoring149- **[references/c4-diagrams.md](references/c4-diagrams.md)** - System context, container, component diagrams, boundaries150- **[references/architecture-diagrams.md](references/architecture-diagrams.md)** - Cloud services, infrastructure, CI/CD deployments151- **[references/advanced-features.md](references/advanced-features.md)** - Themes, styling, configuration, layout options152153## Best Practices1541551. **Start Simple** - Begin with core entities/components, add details incrementally1562. **Use Meaningful Names** - Clear labels make diagrams self-documenting1573. **Comment Extensively** - Use `%%` comments to explain complex relationships1584. **Keep Focused** - One diagram per concept; split large diagrams into multiple focused views1595. **Version Control** - Store `.mmd` files alongside code for easy updates1606. **Add Context** - Include titles and notes to explain diagram purpose1617. **Iterate** - Refine diagrams as understanding evolves162163## Configuration and Theming164165Configure diagrams using frontmatter:166167```mermaid168---169config:170 theme: base171 themeVariables:172 primaryColor: "#ff6b6b"173---174flowchart LR175 A --> B176```177178**Available themes:** default, forest, dark, neutral, base179180**Layout options:**181- `layout: dagre` (default) - Classic balanced layout182- `layout: elk` - Advanced layout for complex diagrams (requires integration)183184**Look options:**185- `look: classic` - Traditional Mermaid style186- `look: handDrawn` - Sketch-like appearance187188## Exporting and Rendering189190**Native support in:**191- GitHub/GitLab - Automatically renders in Markdown192- VS Code - With Markdown Mermaid extension193- Notion, Obsidian, Confluence - Built-in support194195**Export options:**196- [Mermaid Live Editor](https://mermaid.live) - Online editor with PNG/SVG export197- Mermaid CLI - `npm install -g @mermaid-js/mermaid-cli` then `mmdc -i input.mmd -o output.png`198- Docker - `docker run --rm -v $(pwd):/data minlag/mermaid-cli -i /data/input.mmd -o /data/output.png`199200## Common Pitfalls201202- **Breaking characters** - Avoid `{}` in comments, use proper escape sequences for special characters203- **Syntax errors** - Misspellings break diagrams; validate syntax in Mermaid Live204- **Overcomplexity** - Split complex diagrams into multiple focused views205- **Missing relationships** - Document all important connections between entities206207## When to Create Diagrams208209**Always diagram when:**210- Starting new projects or features211- Documenting complex systems212- Explaining architecture decisions213- Designing database schemas214- Planning refactoring efforts215- Onboarding new team members216217**Use diagrams to:**218- Align stakeholders on technical decisions219- Document domain models collaboratively220- Visualize data flows and system interactions221- Plan before coding222- Create living documentation that evolves with code