Mermaid flowchart syntax: shapes, arrows, subgraphs and styling

Write Mermaid flowcharts in Markdown: direction, node shapes, arrow types and labels, subgraphs, styles, and the mistakes that stop a diagram rendering.

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.

Mermaid
flowchart TD
  A[Start] --> B{Is it working?}
  B -->|Yes| C[Ship it]
  B -->|No| D[Fix the bug]
  D --> B

A, 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:

CodeDirection
flowchart TD or flowchart TBtop to bottom
flowchart BTbottom to top
flowchart LRleft to right
flowchart RLright to left

Left to right suits short processes and wide pages; top to bottom suits long processes and decision trees.

Node shapes

ShapeSyntaxTypical use
RectangleA[Text]a step
Rounded rectangleA(Text)a step, softer style
StadiumA([Text])start or end
DiamondA{Text}a decision
CircleA((Text))a connector or event
DatabaseA[(Text)]stored data
SubroutineA[[Text]]a reusable process
HexagonA{{Text}}preparation
ParallelogramA[/Text/]input or output
AsymmetricA>Text]a flag or note

A node with no brackets, such as A, is a rectangle showing its ID.

Arrows and labels

Mermaid
flowchart LR
  A --> B
  C --- D
  E -.-> F
  G ==> H
  I --o J
  K --x L
LinkMeaning
-->arrow
---line without an arrow
-.->dotted arrow
==>thick arrow
--ocircle end
--xcross end
<-->arrow at both ends

Add a label to any link in one of two ways:

Mermaid
flowchart LR
  A -->|Approved| B
  A -- Rejected --> C

Make 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:

Mermaid
flowchart LR
  Draft --> Review --> Publish
  Review --> Edit & Legal

A --> 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:

Mermaid
flowchart TB
  subgraph client [Browser]
    UI[Web app] --> Cache[(Local cache)]
  end
  subgraph server [API]
    Auth[Auth service] --> DB[(Database)]
  end
  UI --> Auth

client 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:

Mermaid
flowchart LR
  A[Request] --> B[Check] --> C[Done]
  classDef ok fill:#e6f4ea,stroke:#2e7d32
  classDef warn fill:#fdecea,stroke:#c62828
  class C ok
  B:::warn

classDef 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. end closes subgraphs, so a node whose text is a lowercase end breaks the diagram. Write End or A["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---oB draws a circle end, not a link to oB. Add a space, A --- oB, or rename the node.
  • A missing first line. Without flowchart or graph, 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.