Back to Blog
10 min read
Tutorial

Mermaid Diagrams: Flowcharts, Sequence Diagrams & Diagram-as-Code Guide

Create flowcharts, sequence diagrams, class diagrams, ER diagrams, and Gantt charts with Mermaid syntax. Free online Mermaid editor with live preview, export to SVG/PNG, and GitHub-ready diagram-as-code. No design tools required.

◇ Diagram-as-Code: Flowcharts, Sequence Diagrams & More in Plain Text

Mermaid is a text-based diagramming language that turns simple syntax into flowcharts, sequence diagrams, class diagrams, ER diagrams, Gantt charts, and more. The same syntax works in GitHub, GitLab, Notion, and our free Mermaid Diagram Editor—with live preview, theme options, and export to SVG or PNG. No design software required; perfect for READMEs, docs, and architecture diagrams.

What is Mermaid?

Mermaid is a JavaScript-based diagramming tool that uses a simple text syntax to generate diagrams. You write code; Mermaid renders flowcharts, sequence diagrams, UML class diagrams, entity-relationship diagrams, Gantt charts, pie charts, mind maps, and Git graphs. Because the source is text, diagrams are version-control friendly, easy to diff, and can be embedded in Markdown (e.g. ```mermaid ... ```) on GitHub, GitLab, and many static site generators.

💡 Quick Tip

Start with a template in our Mermaid Editor, then edit the syntax. The preview updates as you type—no "render" button needed.

Why Use Mermaid for Diagrams?

Text-based diagrams are easier to maintain than static images and integrate naturally into developer workflows:

📄Documentation & READMEs

  • Embed diagrams in Markdown; GitHub and GitLab render Mermaid in code blocks
  • Change the text and the diagram updates—no re-exporting from a GUI tool
  • Review diagram changes in pull requests like any other code

🏗️Architecture & Process

  • Sketch system flows, API sequences, and state machines in minutes
  • Use flowcharts for algorithms, ER diagrams for schemas, Gantt for timelines
  • Export as SVG or PNG for slides, wikis, or design handoff

Mermaid Diagram Types with Examples

Mermaid supports many diagram types. Each example below can be pasted into our Mermaid Editor for a live preview.

1. Flowcharts

Flowcharts are ideal for process flows, decision trees, and algorithms. Use flowchart TD (top-down) or flowchart LR (left-right). Node shapes: [] rectangles, diamonds for decisions, () rounded for start/end.

flowchart TD
  A[Start] --> B{Decision}
  B -->|Yes| C[Action 1]
  B -->|No| D[Action 2]
  C --> E[End]
  D --> E

Use cases: Process documentation, algorithm visualization, decision trees, user flows.

2. Sequence Diagrams

Sequence diagrams show interactions between participants over time—perfect for API calls, microservice communication, or user-to-system flows.

sequenceDiagram
  participant User
  participant API
  participant Database

  User->>API: Request Data
  API->>Database: Query
  Database-->>API: Results
  API-->>User: Response

Use cases: API documentation, system communication, user flows, request/response flows.

3. Class Diagrams

Class diagrams visualize object-oriented structure: classes, attributes, methods, and relationships (inheritance, composition).

classDiagram
  class User {
      +String name
      +String email
      +login()
      +logout()
  }
  class Admin {
      +manageUsers()
  }
  User <|-- Admin

Use cases: Software architecture, OOP design, API model documentation.

4. State Diagrams

State diagrams model state machines: states and transitions. Use stateDiagram-v2 for the modern syntax.

stateDiagram-v2
  [*] --> Idle
  Idle --> Processing: Start
  Processing --> Completed: Success
  Processing --> Error: Failure
  Completed --> [*]
  Error --> Idle: Retry

Use cases: Workflow states, process states, UI or backend state machines.

5. ER Diagrams

Entity-relationship diagrams describe database schemas: entities, attributes, and relationships (one-to-many, many-to-many).

erDiagram
  USER ||--o{ ORDER : places
  ORDER ||--|{ ORDER_ITEM : contains
  PRODUCT ||--o{ ORDER_ITEM : "ordered in"

  USER {
      string id PK
      string name
      string email
  }
  ORDER {
      string id PK
      datetime created_at
  }

Use cases: Database design, data modeling, schema documentation.

6. Gantt Charts

Gantt charts show project timelines with tasks, dates, and dependencies. Use after to chain tasks.

gantt
  title Project Timeline
  dateFormat YYYY-MM-DD
  section Phase 1
  Planning    :a1, 2024-01-01, 7d
  Design      :a2, after a1, 10d
  section Phase 2
  Development :b1, after a2, 14d
  Testing     :b2, after b1, 7d

Use cases: Project planning, sprint timelines, milestone tracking.

7. Pie Charts, Mind Maps & Git Graphs

Mermaid also supports pie (data distribution), mindmap (ideas and categories), and gitGraph (branch and commit visualization). Syntax varies; check the official mermaid.js.org docs or the Quick Reference in our tool.

Using Our Mermaid Editor (Step by Step)

Our Mermaid Diagram Editor runs entirely in your browser. No sign-up; your diagrams never leave your device.

1Choose a Template or Start Blank

Pick a diagram type from the template menu (flowchart, sequence, class, ER, Gantt, etc.) or paste your own Mermaid code. The editor has syntax highlighting and error hints.

2Edit and See Live Preview

As you type, the diagram renders in real time. Fix syntax errors using the on-screen messages. No separate "render" or "preview" button.

3Pick a Theme and Export

Switch between default, dark, forest, and neutral themes. Export the diagram as SVG (scalable) or PNG for docs, slides, or wikis. Save or share via link so others can open the same diagram.

Mermaid Syntax Quick Reference

Handy reference for flowcharts and common patterns. For full syntax, see mermaid.js.org.

ElementSyntax / Meaning
Rectangle[Text] — process step
Diamond{Text} — decision
Rounded(Text) — start/end
Arrow--> or -->|Label| — direction and optional label
Directionflowchart TD top-down, LR left-right

Best Practices for Mermaid Diagrams

Keep diagrams maintainable and readable:

🎯Keep It Simple and Focused

One diagram per concept or flow. If a flowchart has too many nodes, split it into subgraphs or multiple diagrams. Simple diagrams are easier to update and review.

📝Use Clear Labels and Naming

Name nodes and arrows with descriptive labels (e.g. -->|Yes| or [Validate Input]). Consistent naming (e.g. PascalCase for classes, clear verb phrases for steps) helps readers scan quickly.

🔗Organize with Subgraphs

For complex flowcharts, use subgraph to group related nodes. Makes the diagram easier to read and refactor.

Common Pitfalls & How to Avoid Them

Avoid these Mermaid mistakes:

Wrong Keyword or Diagram Type

Each diagram type has its own first line: flowchart TD, sequenceDiagram, classDiagram, stateDiagram-v2, erDiagram, gantt. Using graph instead of flowchart or mixing syntax from different types will cause parse errors.

⚠️Special Characters in Labels

Quotes, parentheses, and brackets inside node text can break parsing. Use double quotes around the label if it contains special characters, or simplify the label. For example: A["Check (yes/no)"].

📄Indentation and Line Breaks

In sequence diagrams, participant and message lines are usually indented for readability. Mermaid is flexible, but inconsistent indentation can make errors harder to spot. Use the live preview to catch issues early.

Pro Tips

Get more from Mermaid and our editor:

📋Use in Markdown (GitHub, GitLab)

Wrap your Mermaid code in a fenced code block with language mermaid: ```mermaid ... ```. GitHub and GitLab will render it as a diagram. Write and test in our editor, then copy into your README or docs.

🔗Pair with Other Tools

Use the Code Editor for multi-file snippets or configs that reference your architecture; use the JSON Formatter when your docs or APIs include JSON payloads alongside sequence diagrams.

✅ Create Mermaid Diagrams in Your Browser

Use our free Mermaid Diagram Editor to create flowcharts, sequence diagrams, class diagrams, ER diagrams, Gantt charts, and more. Live preview, multiple themes, export to SVG or PNG, and save or share via link. Same syntax as GitHub and GitLab—no sign-up, no install.

Try It Now

Put this guide into practice with our free tools. No sign-up required.

Try Mermaid Editor
Mermaid Diagrams: Flowcharts, Sequence Diagrams & Diagram-as-Code Guide | Spoold Blog | Spoold