Mermaid Cheat Sheet: Syntax for Flowcharts, Sequence and ER Diagrams
Mermaid is easy to start and easy to get subtly wrong: a lowercase end that closes a flowchart, a label with brackets that needs quotes, a cardinality marker the wrong way round. This cheat sheet lists the syntax people look up most, in small snippets. Every snippet has an "Open in editor" link that loads it into the Mermaid editor so you can change it and see the result.
Flowchart
Start with a direction: TD or TB (top to bottom), BT, LR, RL. Then write nodes and arrows.
flowchart LR
A[Start] --> B{Valid?}
B -->|yes| C[Save]
B -->|no| D[Show error]
D --> ANode shapes
[text] rectangle, (text) rounded, {text} diamond, ((text)) circle, [(text)] cylinder, ([text]) stadium.
flowchart TD
rect[Rectangle]
round(Rounded)
diamond{Decision}
circle((Circle))
db[(Database)]
pill([Stadium])
rect --> round --> diamond
diamond --> circle
diamond --> db
diamond --> pillArrows and labels
--> arrow, --- line, -.-> dotted, ==> thick. A label goes between pipes (-->|text|) or inside the arrow (-- text -->).
flowchart LR
A --> B
B --- C
C -.-> D
D ==> E
E -->|label| F
F -- another label --> GSubgraphs and styles
Group nodes with subgraph and close it with end. Define a style with classDef and apply it with class.
flowchart TB
subgraph backend [Backend]
api[API] --> db[(Database)]
end
user([User]) --> api
classDef hot fill:#fde68a,stroke:#b45309,color:#1f2937
class api hotSequence diagram
participant and actor declare who takes part. ->> is a solid line with an arrowhead (a call), -->> a dotted line with an arrowhead (a reply), -) an open arrow for an asynchronous message. Note over A,B: text adds a note, loop, alt/else, opt and par wrap messages and close with end, and autonumber numbers the messages.
sequenceDiagram
autonumber
actor User
participant App
participant API
User->>App: Click save
App->>API: POST /items
API-->>App: 201 Created
App-)User: Show toast
Note over App,API: Retries on 5xx
loop every 30 s
App->>API: Sync
end
alt saved
App->>User: Done
else failed
App->>User: Try again
endER diagram
A relationship is two cardinality markers around --. Left of the line: |o zero or one, || exactly one, }o zero or more, }| one or more. Right of the line the markers are mirrored: o|, ||, o{, |{. In CUSTOMER ||--o{ ORDER : places, one customer places zero or more orders. Starting from SQL tables instead? The ER Diagram Generator draws the diagram from CREATE TABLE statements.
erDiagram
CUSTOMER ||--o{ ORDER : places
ORDER ||--|{ LINE_ITEM : contains
CUSTOMER {
int id PK
string name
}
ORDER {
int id PK
int customer_id FK
}Class, state, Gantt and pie in two lines each
classDiagram
class Animal {
+String name
+eat() void
}
Animal <|-- DogstateDiagram-v2
[*] --> Idle
Idle --> Running : start
Running --> Idle : stop
Running --> [*]gantt
title Release plan
dateFormat YYYY-MM-DD
section Build
Design :a1, 2026-01-05, 5d
Implement :after a1, 10dpie title Traffic
"Search" : 60
"Direct" : 30
"Other" : 10Comments, quotes and reserved words
A line that starts with %% is a comment. A label that contains brackets or other special characters goes in double quotes. The word end in lowercase closes a block, so a node called end breaks the diagram: write End instead.
flowchart LR
%% This line is a comment and is ignored
A["Label with (brackets) needs quotes"] --> B[Done]
C[End]Where to go next
The official syntax pages at mermaid.js.org list every option. For full copy-paste diagrams see Mermaid diagram examples, and to turn a diagram into an image see how to export a Mermaid diagram to PNG, SVG or PDF.
Frequently asked questions
- How do I start a flowchart in Mermaid?
- Begin with flowchart TD (top to bottom) or flowchart LR (left to right), then add nodes and arrows such as A[Start] --> B{Ok?}. The older keyword graph still works.
- What node shapes does a Mermaid flowchart support?
- The common ones are [text] rectangle, (text) rounded, {text} diamond, ((text)) circle, [(text)] cylinder (database) and ([text]) stadium.
- How do I label an arrow?
- Put the label between pipes after the arrow, A -->|yes| B, or write it inside the arrow, A -- yes --> B.
- How do I add a comment?
- Start a line with %%. Mermaid ignores it.
- Why does my flowchart break when a node is named "end"?
- In lowercase, end is a reserved word that closes blocks such as subgraph. Capitalise it (End) or capitalise any letter of it.
- What do the ER diagram cardinality symbols mean?
- On the left of the line: |o zero or one, || exactly one, }o zero or more, }| one or more. On the right the same markers are mirrored: o|, ||, o{, |{. In CUSTOMER ||--o{ ORDER : places, one customer places zero or more orders.
- What is the difference between ->> and -->> in a sequence diagram?
- ->> is a solid line with an arrowhead (a call); -->> is a dotted line with an arrowhead (a reply).
- How do I preview Mermaid code while I learn?
- Paste it into an online editor with live preview, such as the Hexabench Mermaid editor, and change it until it renders.