The smallest flowchart
A Mermaid flowchart is text in a fenced code block named mermaid. The first line sets the diagram type and direction; each following line connects nodes.
flowchart TD
A[Start] --> B{Is it working?}
B -->|Yes| C[Ship it]
B -->|No| D[Fix the bug]
D --> BA, B, C and D are IDs. The text in brackets is what the reader sees, and the bracket type sets the shape. Once a node is defined, refer to it by its ID alone, as D --> B does.
To see each example as a diagram, paste it into the free editor:
Direction
The word after flowchart sets the direction of the whole diagram:
| Code | Direction |
|---|---|
flowchart TD or flowchart TB | top to bottom |
flowchart BT | bottom to top |
flowchart LR | left to right |
flowchart RL | right to left |
Left to right suits short processes and wide pages; top to bottom suits long processes and decision trees.
Node shapes
| Shape | Syntax | Typical use |
|---|---|---|
| Rectangle | A[Text] | a step |
| Rounded rectangle | A(Text) | a step, softer style |
| Stadium | A([Text]) | start or end |
| Diamond | A{Text} | a decision |
| Circle | A((Text)) | a connector or event |
| Database | A[(Text)] | stored data |
| Subroutine | A[[Text]] | a reusable process |
| Hexagon | A{{Text}} | preparation |
| Parallelogram | A[/Text/] | input or output |
| Asymmetric | A>Text] | a flag or note |
A node with no brackets, such as A, is a rectangle showing its ID.
Arrows and labels
flowchart LR
A --> B
C --- D
E -.-> F
G ==> H
I --o J
K --x L| Link | Meaning |
|---|---|
--> | arrow |
--- | line without an arrow |
-.-> | dotted arrow |
==> | thick arrow |
--o | circle end |
--x | cross end |
<--> | arrow at both ends |
Add a label to any link in one of two ways:
flowchart LR
A -->|Approved| B
A -- Rejected --> CMake a link longer, to push a node further away, by adding dashes: ---> is one rank longer than -->.
Chains and branches
Several connections fit on one line:
flowchart LR
Draft --> Review --> Publish
Review --> Edit & LegalA --> B --> C chains steps; A --> B & C points one node at two. Short lines are still easier to edit, so use chains for simple sequences.
Group nodes with subgraphs
A subgraph draws a box around related nodes:
flowchart TB
subgraph client [Browser]
UI[Web app] --> Cache[(Local cache)]
end
subgraph server [API]
Auth[Auth service] --> DB[(Database)]
end
UI --> Authclient is the subgraph's ID and the text in brackets is its title. Links can point at a node inside a subgraph or at the subgraph itself. Add direction LR as the first line inside a subgraph to lay out its contents differently from the rest.
Style nodes
Define a style once and apply it to several nodes:
flowchart LR
A[Request] --> B[Check] --> C[Done]
classDef ok fill:#e6f4ea,stroke:#2e7d32
classDef warn fill:#fdecea,stroke:#c62828
class C ok
B:::warnclassDef names a style, class C ok applies it, and B:::warn applies it inline. For a single node, style A fill:#fff3cd works too. Colours may look different in dark mode, so check both if your readers use either.
Mistakes that stop a diagram rendering
- The word end.
endcloses subgraphs, so a node whose text is a lowercaseendbreaks the diagram. WriteEndorA["end"]. - Special characters in text. Parentheses, brackets and quotes inside a node's text confuse the parser. Wrap the text in double quotes:
A["Save (draft)"]. - A node ID starting with o or x after a link.
A---oBdraws a circle end, not a link tooB. Add a space,A --- oB, or rename the node. - A missing first line. Without
flowchartorgraph, Mermaid doesn't know what to draw. - The wrong fence. The block must be fenced with three backticks and the word
mermaid. See Mermaid diagram not rendering for the full checklist.
Comments start with %% on their own line and are ignored. For conversations between systems rather than processes, use a sequence diagram instead. The official flowchart documentation lists every option, including newer shapes.
Frequently asked questions
How do I make a flowchart in Markdown?
Write Mermaid code inside a fenced code block named mermaid. Start with flowchart TD for top to bottom or flowchart LR for left to right, then one connection per line, such as A[Start] --> B{Decision}. GitHub, GitLab, Obsidian and many Markdown apps render it as a diagram.
What is the difference between graph and flowchart in Mermaid?
Both start a flowchart and accept the same basic syntax. flowchart is the newer keyword and supports newer features such as more arrow types, so use flowchart for new diagrams.
How do I add text to an arrow in Mermaid?
Put the label between pipes after the arrow, A -->|Yes| B, or inside the arrow, A -- Yes --> B. Both draw the same labelled arrow.
How do I change the direction of a Mermaid flowchart?
Change the word after flowchart: TD or TB for top to bottom, BT for bottom to top, LR for left to right and RL for right to left.
Why does my Mermaid flowchart break when a node says end?
end is a keyword that closes subgraphs. Written in lowercase as node text, it breaks the diagram. Capitalise it (End), or put it in quotes inside brackets: A["end"].
How do I export a Mermaid flowchart as an image?
Paste the code into a Mermaid editor that exports images, such as the free Mermaid live editor on aeromarkdown.app, and download it as PNG or SVG.