Diagrams as code
Mermaid flowchart syntax: a practical cheat sheet
By Abdessamad BettalPublished 4 min read
The short answer
A Mermaid flowchart starts with flowchart and a direction (TD for top-down, LR for left-to-right), then lists connections such as A[Start] --> B{Valid?}. Brackets set the shape: [ ] rectangle, ( ) rounded, { } decision, [( )] database. Links are --> for an arrow, -.-> dotted, ==> thick, and text goes in -->|yes| or -- yes -->. Group nodes with subgraph … end and style them with classDef. Most broken diagrams come from unquoted special characters or a node literally named end.
Mermaid turns a few lines of text into a flowchart, and GitHub, GitLab, Notion, Obsidian and most documentation generators render it inline. That makes it the easiest way to keep a diagram next to the code it describes, reviewed in the same pull request.
This is the syntax you actually use, on one page, plus the handful of gotchas that cause most "why won't this render" moments. Every example renders in the Mermaid editor as you paste it.
The skeleton
flowchart TD
A[Start] --> B{Valid?}
B -->|yes| C[Save]
B -->|no| D[Show error]
D --> A
The first line declares the diagram type and direction. Every later line is a node, a link, or both. Indentation is ignored but helps humans.
Node ids and labels are separate. A[Start] creates a node with the id A and the label Start. Later lines refer to it by id alone (A), so you only write the label once. Short ids keep long charts readable; descriptive ids (checkStock) keep them maintainable. Pick one style.
Directions
| Keyword | Direction |
|---|---|
TD or TB |
Top to bottom |
BT |
Bottom to top |
LR |
Left to right |
RL |
Right to left |
LR suits pipelines and anything with long labels; TD suits decision-heavy flows that read like a story. Subgraphs can set their own direction with a direction LR line inside them.
Node shapes
The brackets around the label choose the shape. These are the ones that map to standard flowchart symbols:
| Syntax | Shape | Flowchart meaning |
|---|---|---|
A[Text] |
Rectangle | Process step |
A(Text) |
Rounded rectangle | Alternate process, or a soft start/end |
A([Text]) |
Stadium | Terminator: start or end |
A{Text} |
Diamond | Decision |
A[/Text/] |
Parallelogram | Input or output |
A[(Text)] |
Cylinder | Database |
A[[Text]] |
Subroutine | Predefined process |
A((Text)) |
Circle | On-page connector |
A{{Text}} |
Hexagon | Preparation |
A>Text] |
Asymmetric flag | Often used for a signal or note |
Mermaid 11.3 and later also accept an expanded syntax that names the shape directly, such as A@{ shape: doc, label: "Invoice" } for a document symbol. It covers more shapes, but older renderers, including some hosted platforms that lag behind, show it as an error, so check where the diagram will be displayed before relying on it.
Links
| Syntax | Link |
|---|---|
A --> B |
Arrow |
A --- B |
Line, no arrow |
A -.-> B |
Dotted arrow |
A ==> B |
Thick arrow |
A --o B |
Circle end |
A --x B |
Cross end |
A <--> B |
Arrows at both ends |
A ~~~ B |
Invisible link, for nudging layout |
A ---> B |
Longer arrow: each extra dash adds a rank |
Two ways to label a link, which render identically:
flowchart LR
A -->|yes| B
A -- no --> C
Chain and fan out to save lines:
flowchart LR
Build --> Test --> Deploy
Deploy --> Web & Worker
Lint & Typecheck --> Build
Subgraphs
Group related nodes with subgraph, an optional id and a title in brackets, closed by end:
flowchart LR
subgraph ci [Continuous integration]
direction TB
Lint --> Test
end
subgraph cd [Delivery]
Stage --> Prod
end
ci -->|on main| cd
Links can point at a subgraph id as a whole, as the last line does. Prefer that over linking the nodes inside: a subgraph's own direction is ignored as soon as any node inside it links to a node outside it, and Mermaid falls back to the parent direction.
Styling
Define a class once and apply it to many nodes. This keeps colors out of the logic and makes a palette change a one-line edit:
flowchart TD
A[Order placed] --> B{In stock?}
B -->|no| C[Back-order]:::warn
B -->|yes| D[Ship]:::ok
classDef ok fill:#e6f4ea,stroke:#1e7a3c
classDef warn fill:#fdecea,stroke:#b3261e
:::name attaches a class inline; class C,D name does it in one line for several nodes. style A fill:#fff styles a single node, and linkStyle 0 stroke:#888 styles the first link (links are numbered from 0 in the order they appear). Comments start with %%.
Gotchas that break a diagram
A node called end. end closes a subgraph, so a node labelled exactly end in lowercase breaks the parser. Write End, or use an id with a label: E[end].
Special characters in labels. Parentheses, brackets, colons and semicolons inside a label confuse the parser. Wrap the label in double quotes: A["Total (incl. VAT)"]. For a literal double quote inside a label, use the entity #quot;.
Ids that start with o or x. A---oB is read as a line with a circle end pointing at B, not a line to a node oB. Add a space (A --- oB) or capitalise the id.
graph versus flowchart. Both keywords produce a flowchart. graph is the original; flowchart is newer and is needed for some newer features. Use flowchart.
Click handlers that do nothing. click A "https://…" works only where the host allows it. GitHub and most platforms render Mermaid with a strict security level that disables interactions, so treat links in diagrams as a bonus, not a navigation path.
Layout you cannot control. Mermaid decides placement. You can influence it with link order, longer links (--->) and invisible links (~~~), but not set positions. When one box must sit in a particular place, Mermaid is the wrong tool for the final version of that diagram.
When to leave Mermaid
Mermaid is ideal while a diagram lives in a repository and changes with the code. It is a poor fit for a diagram that needs precise placement, consistent styling across a deck, or symbols outside its set.
In flowchart.tools, the Mermaid editor renders every Mermaid diagram type and exports SVG or PNG. For flowcharts, Open in editor converts the nodes and links into real shapes and connectors you can drag, restyle and export to draw.io. The editor also goes the other way: any diagram drawn there exports as a .mmd file, so a chart polished on the canvas can go back into a README. If you would rather not learn the syntax at all, text to flowchart accepts plain Step -> Next step lines and shows the Mermaid code it would produce.
For the design side of flowcharts, which shapes to use and how to route decisions, see how to draw decisions without the spaghetti.
Common questions
- What is the difference between graph and flowchart in Mermaid?
- They are the same diagram type. graph is the original keyword and still works. flowchart is the newer keyword and is required for some newer features, so use flowchart in new diagrams.
- Why does my Mermaid flowchart break when a node is called end?
- end closes a subgraph, so a node whose text is exactly end in lowercase confuses the parser. Capitalise it (End), or give the node an id and put the word in its label: E[end].
- How do I put special characters in a Mermaid label?
- Wrap the label in double quotes: A["Price (incl. VAT)"]. Inside quotes, parentheses, brackets and colons are treated as text. For a literal double quote use the entity #quot;.
- Does GitHub render Mermaid flowcharts?
- Yes. GitHub renders fenced code blocks marked mermaid in Markdown files, issues, pull requests and wikis. GitLab, Notion, Obsidian and many documentation generators do too, though each may run a different Mermaid version.