How to write a Mermaid flowchart:a direction, nodes and arrows.

A Mermaid flowchart is text in a code block: you name the nodes, connect them with arrows, and the diagram is drawn for you. Here is the syntax with copyable examples.

macOS 14 or later. Reading is free; editing and reviews are one payment.

Quick answer

Start a code block marked mermaid, write flowchart and a direction, then list the links between nodes. TD draws top to bottom and LR left to right. A box is [text], a decision is {text}, and --> is an arrow.

```mermaid
flowchart LR
    A[Write spec] --> B{Approved?}
    B -->|Yes| C[Build]
    B -->|No| A
```

·

Direction

The first line names the diagram type and the direction. flowchart and the older graph both work.

  • TD or TB: top to bottom
  • BT: bottom to top
  • LR: left to right
  • RL: right to left

Pick LR for a short process that fits the width of a page and TD for a longer one.

Node shapes

A node is an id, optionally followed by its text in brackets. The brackets pick the shape:

flowchart LR
    a[Rectangle]
    b(Rounded)
    c([Stadium])
    d[[Subroutine]]
    e[(Database)]
    f((Circle))
    g{Decision}
    h{{Hexagon}}
    i[/Parallelogram/]

A node with no brackets just shows its id. You can mention a node again later by its id alone, so the shape and the text are written once.

Subgraphs

A subgraph groups nodes in a labeled box. It starts with subgraph and ends with end:

flowchart TD
    subgraph client [Client]
        A[Browser] --> B[App]
    end
    subgraph server [Server]
        C[API] --> D[(Database)]
    end
    B --> C

The word in brackets is the title, and the word before it is the id you can link to. A subgraph can have its own direction LR line inside, though the direction is ignored if a node inside links to something outside.

A complete example

Putting it together, here is a small release process with a decision, a loop back and a grouped step:

flowchart TD
    A([Start]) --> B[Write the change]
    B --> C{Tests pass?}
    C -->|No| B
    C -->|Yes| D[Open a pull request]
    subgraph review [Review]
        D --> E{Approved?}
        E -->|No| B
    end
    E -->|Yes| F[(Merge)]

Read it from the top: the stadium shape marks the start, the diamonds are questions, labeled arrows are the answers, and the box around two nodes is the review. Naming each decision as a question and each arrow as its answer makes a flowchart readable without explanation.

Comments and styles

A line that starts with %% is a comment and is not drawn. To color nodes, define a class and apply it:

flowchart TD
    %% the happy path
    A[Start]:::good --> B[Done]:::good
    classDef good fill:#d4f4dd,stroke:#2d7a46

Keep styling light. Most flowcharts read better with fewer colors, and styles can look different in light and dark themes.

Common mistakes

  • The word end in lowercase as a node text breaks the diagram. Write End or quote the text.
  • A node id that starts with o or x right after a link, such as A---oB, is read as a circle or cross link. Add a space or capitalize it.
  • Special characters in a label. Brackets, parentheses and colons need double quotes: A["Save (draft)"].
  • No first line. Without flowchart TD nothing is drawn.
  • Too many nodes. A flowchart with thirty boxes is hard to read. Split it, or use subgraphs.

When a diagram fails, Mermaid shows an error message, and the text stays in your file to fix.

Drawing it

GitHub draws Mermaid blocks in Markdown files, and so does DotMD: offline, in the light or dark theme to match the page. In the editor, Insert → Diagram puts a working flowchart into the document and the preview redraws as you type. The same guide covers sequence, class, state, ER and Gantt diagrams. For the fence syntax itself, see code blocks in Markdown.

Questions about Mermaid flowcharts

How do I write a flowchart in Mermaid?

Start a mermaid code block with flowchart TD (or LR), then add links such as A[Start] --> B[End], one per line.

What is the difference between TD and LR in a Mermaid flowchart?

TD (top down, same as TB) draws the chart from top to bottom, and LR draws it from left to right.

How do I add text to an arrow in Mermaid?

Put it between pipes after the arrow, A -->|Yes| B, or between the dashes, A -- Yes --> B.

Why does my Mermaid flowchart show an error?

Common causes are a lowercase end in a label, a missing first line, or special characters in a label without double quotes.

More questions