Mermaid flowcharts

Write boxes and arrows as plain text in your Markdown. This guide covers the flowchart syntax, with examples you can load into the converter and download as a PDF.

Markdown
Preview

Your first flowchart

A flowchart is a code block named mermaid whose first line is flowchart followed by a direction. Every line after that describes nodes and the arrows between them. A node is created the first time you mention it, so there's no separate step for declaring boxes.

```mermaid
flowchart LR
  Idea --> Draft --> Publish
```

Each word here is both the node's ID and its label. Once labels get longer than a word, give the node a short ID and put the label in brackets: D[Write the first draft]. After that you only need to type D to refer to it, which keeps the lines that draw arrows short and easy to read.

```mermaid
flowchart LR
  I[Have an idea] --> D[Write the first draft]
  D --> R[Ask a friend to read it]
  R --> P[Publish]
```

Indenting the lines is optional. It makes the source easier to scan, and Mermaid ignores it.

Direction

The word after flowchart sets which way the chart flows. Mermaid places the nodes for you; the direction is the main control you have over the layout.

You typeThe chart flows
flowchart TD or flowchart TBTop to bottom
flowchart BTBottom to top
flowchart LRLeft to right
flowchart RLRight to left

Left to right reads naturally for short processes of three to six steps. Top to bottom suits anything longer, and anything with many branches, because a page has more room going down than across. You may also see graph instead of flowchart in older examples. It works the same way, but flowchart is the current name.

Node shapes

The brackets around a label choose the shape. Flowcharts have a loose convention: rounded ends for the start and finish, plain boxes for steps, diamonds for questions and cylinders for stored data.

You typeShapeOften used for
A[Text]RectangleA step
A(Text)Rounded rectangleA softer step
A([Text])StadiumStart or end
A{Text}DiamondA decision
A[(Text)]CylinderA database
A((Text))CircleA connector or event
A[[Text]]Box with side barsA subroutine
A{{Text}}HexagonPreparation
A[/Text/]ParallelogramInput or output
```mermaid
flowchart TD
  Start([Start]) --> Input[/Read the form/]
  Input --> Valid{All fields filled in?}
  Valid -- No --> Input
  Valid -- Yes --> Save[(Save to the database)]
  Save --> Finish([Finish])
```

Arrows and labels

Lines come in a few weights and styles. Most diagrams only need the plain arrow, plus a dotted one for anything optional or happening in the background.

You typeYou get
A --> BArrow
A --- BLine without an arrowhead
A -.-> BDotted arrow
A ==> BThick arrow
A <--> BArrowheads at both ends
A --o B / A --x BCircle or cross at the end

To label a line, put the text in the middle of it, as in A -- Yes --> B, or after it between pipes, as in A -->|Yes| B. Both give the same result; the pipe form is easier to spot when a line is long. Labels work on dotted and thick lines too: A -. later .-> B and A == now ==> B.

```mermaid
flowchart LR
  Order[New order] --> Paid{Paid?}
  Paid -->|Yes| Pack[Pack the box]
  Paid -->|No| Remind[Send a reminder]
  Remind -. after 3 days .-> Cancel[Cancel the order]
  Pack == same day ==> Ship[Ship it]
```

Adding dashes makes a line longer, which pushes the next node further away: A ----> B spans more ranks than A --> B. It's a handy way to line up a branch that rejoins the main path later.

Chains and branches

You can write several steps on one line, as the first example did. An ampersand connects several nodes at once, which saves repeating the same arrow when a step fans out or several steps lead to the same place.

```mermaid
flowchart TD
  Brief[Write the brief] --> Design & Copy & Legal
  Design[Design] --> Review
  Copy[Copywriting] --> Review
  Legal[Legal check] --> Review
  Review{Approved?} -- No --> Brief
  Review -- Yes --> Launch([Launch])
```

A node's label only needs to be given once, the first time you write it with brackets. Later mentions can use the bare ID, as Review does here. If you give two different labels to the same ID, the last one wins.

Subgraphs

A subgraph draws a labeled box around a group of nodes, which is useful for showing who is responsible for each part of a process, or which system each step runs on. Start it with subgraph, an ID and a title in brackets, and close it with end. Arrows can cross from one subgraph to another, and can point at a whole subgraph by its ID.

```mermaid
flowchart LR
  subgraph customer [Customer]
    Browse[Browse the shop] --> Buy[Check out]
  end
  subgraph warehouse [Warehouse]
    direction TB
    Pick[Pick the items] --> Box[Pack the box]
  end
  customer --> warehouse
  warehouse --> Courier([Courier collects])
```

A direction line inside a subgraph changes the flow within that box only. Mermaid ignores it when a node inside the subgraph is linked directly to a node outside, so if a subgraph's direction seems to have no effect, check for an arrow that crosses its edge.

Colors

To highlight particular nodes, define a class with classDef and attach it to nodes with :::. The properties are written like CSS: fill for the background, stroke for the border and color for the text.

```mermaid
flowchart LR
  classDef risk fill:#fde2e1,stroke:#b42318,color:#7a1610
  classDef safe fill:#dcfae6,stroke:#067647,color:#054f31
  Plan[Plan the change] --> Test[Test on staging]:::safe
  Test --> Deploy[Deploy on Friday evening]:::risk
```

Colors you set this way appear in every md2pdf.me style, including the greyscale ones. Use them sparingly: a diagram with one highlighted node draws the eye to it, while a diagram where every node has its own color is harder to read than one with none. If the PDF may be printed in black and white, choose fills that differ in lightness as well as in hue.

Text that breaks a flowchart

When a flowchart has a mistake, the preview shows its code with the error above it, including the line Mermaid stopped at. These are the usual causes:

Lines starting with %% are comments. Mermaid skips them, which makes them useful for notes to yourself and for switching off part of a diagram while you look for a mistake.

```mermaid
flowchart TD
  %% Labels with punctuation go in quotes
  Cart["Basket (3 items)"] --> Pay{"Pay now?"}
  Pay -- Yes --> Done([End of checkout])
  Pay -- No --> Cart
```

Flowcharts in a PDF

md2pdf.me draws each flowchart as a vector graphic, so the boxes and text stay sharp however far you zoom into the PDF, and a diagram is never split across two pages. A flowchart that is wider than the page's text is shrunk to fit, and so is one taller than most of a page. With a wide left-to-right chart that can leave the text small, so switch it to TD, which lets it be drawn larger.

A flowchart's colors come from the style you choose. Classic, Academic and Résumé draw it in shades of grey for printing, Modern and Report in their blue accent, Worksheet in its own blue and purple, and GitHub in the same colors as github.com. The themed styles use their own colors: oxblood for Gothic, rust and navy for Sandwake, forest green for Elvish, teal and magenta for Cyberpunk, black and oxblood on newsprint grey for Newspaper, and ink and vermilion for Fiendish. You can switch styles in the converter above and the preview redraws the diagram straight away.

For a long process, consider several smaller flowcharts with a heading and a sentence or two before each one. They're easier to follow than a single chart covering a whole page, and the text between them can explain what each part is for.

Other diagram types

Back to the Mermaid cheat sheet