Mermaid diagrams from text: flowcharts, sequence, Gantt, class, state and ER examples

Mermaid is a JavaScript library that turns a short text description into an SVG diagram. Because the source is plain text, diagrams live next to your code, show up in diffs, and get reviewed like anything else. GitHub and GitLab render mermaid code blocks in Markdown natively, and most documentation tools have a plugin (this blog renders them with astro-mermaid, so every example below is drawn live from its source).
Flowcharts
Section titled “Flowcharts”Nodes are declared inline; --> draws an arrow and |text| labels it. TD means top-down (LR is left-to-right).
flowchart TD
A[Start] --> B{Decision}
B -->|Yes| C[Do something]
B -->|No| D[Do something else]
C --> E[End]
D --> E
Bracket shape sets node shape: [ ] rectangle, { } diamond, ( ) rounded. graph still works as an alias for flowchart.
Sequence diagrams
Section titled “Sequence diagrams”Good for request/response flows between services. ->> is a solid arrow, -->> a dashed reply.
sequenceDiagram
participant U as User
participant S as System
U->>S: Request data
S-->>U: Return data
Gantt charts
Section titled “Gantt charts”Each task has an optional status (done, active, crit), an ID, a start (a date or after <id>), and an end date or duration.
gantt
title Project timeline
dateFormat YYYY-MM-DD
section Design
Complete design :done, des1, 2024-09-01, 2024-09-10
Review design :active, des2, 2024-09-11, 5d
section Development
Set up environment :des3, after des2, 3d
Develop features :des4, after des3, 10d
Class diagrams
Section titled “Class diagrams”+ marks public members; <|-- is inheritance.
classDiagram
class Animal {
+String name
+int age
+makeSound()
}
class Dog {
+bark()
}
class Cat {
+meow()
}
Animal <|-- Dog
Animal <|-- Cat
State diagrams
Section titled “State diagrams”[*] is the start (or end) state.
stateDiagram-v2
[*] --> Idle
Idle --> Processing: Start
Processing --> Idle: Complete
Processing --> Error: Error
Error --> Idle: Retry
Entity relationship diagrams
Section titled “Entity relationship diagrams”The symbols on each side of -- give cardinality: || exactly one, o{ zero or more, |{ one or more.
erDiagram
CUSTOMER ||--o{ ORDER : places
ORDER ||--|{ LINE-ITEM : contains
CUSTOMER {
string name
string address
int customerID
}
ORDER {
int orderID
date orderDate
}
Themes and configuration
Section titled “Themes and configuration”Built-in themes are default, neutral, dark, forest and base (the one to customise with themeVariables). Set per diagram with a YAML frontmatter config block:
---
config:
theme: forest
---
flowchart LR
A --> B --> C
Older examples use %%{init: {"theme": "forest"}}%% directives instead. Directives have been deprecated since Mermaid v10.5.0 in favour of frontmatter config.
Links and security
Section titled “Links and security”Flowchart nodes can link somewhere with click:
flowchart LR A[Docs] --> B[Live editor] click B "https://mermaid.live" "Open the live editor"Clicks only work if the page initialises Mermaid with securityLevel set to loose or antiscript. The default, strict, encodes HTML in labels and disables click handlers, which is what you want when rendering diagrams written by untrusted users. There’s also sandbox, which renders each diagram inside a sandboxed iframe.
Rendering Mermaid yourself
Section titled “Rendering Mermaid yourself”On a plain HTML page, load the ES module from a CDN and put diagram source inside <pre class="mermaid">:
<pre class="mermaid">flowchart LR A --> B</pre>
<script type="module"> import mermaid from "https://cdn.jsdelivr.net/npm/mermaid@12/dist/mermaid.esm.min.mjs"; mermaid.initialize({ startOnLoad: true });</script>To produce image files (for slides, PDFs, or anywhere without JavaScript), use the official CLI, mmdc:
npx -p @mermaid-js/mermaid-cli mmdc -i diagram.mmd -o diagram.svgIt also accepts .png and .pdf output.
For quick experiments, the free Mermaid Live Editor renders as you type and gives you a shareable link. Mermaid Chart is the hosted, commercial editor from the team behind Mermaid; see the Mermaid Chart Guide for more examples.
Gotchas
Section titled “Gotchas”- Syntax errors fail the whole diagram. Paste it into the live editor to see which line is wrong.
- Words like
endin lowercase break flowcharts as node text; capitalise at least one letter (End). - Large graphs lay out poorly. Split them, or switch direction (
LRvsTD). - Renderers pin different Mermaid versions, so newer diagram types may work in the live editor but not on GitHub or in your docs tool yet.
To generate images on the web, check out this Mermaid Chart Tool. Please note that this is an affiliate link, and using it supports us at no additional cost to you.
Further reading
Section titled “Further reading”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)