Mermaid reference

Mermaid subgraphs

A subgraph draws a labelled frame around a group of nodes: a team, a stage of a pipeline, a cloud region. This page covers the syntax, nesting, per-group direction, and the layout rule that surprises almost everyone.

Updated

A pipeline: two stages, each laid out top to bottom Edit and export
flowchart LR
  push([Push to main]) --> build
  subgraph build [Build]
    direction TB
    b1[Install] --> b2[Compile] --> b3[Run tests]
  end
  subgraph ship [Ship]
    direction TB
    s1[Package] --> s2[Deploy]
  end
  build --> ship
  ship --> live([Live])
Nested subgraphs Edit and export
flowchart TB
  user((User)) --> cdn
  subgraph cloud [Cloud]
    cdn[CDN]
    subgraph eu [EU region]
      api[API] --> db[(Postgres)]
    end
  end
  cdn --> api

Syntax

subgraph id [Title shown on the frame]
  nodes and links
end
  • The id is what you link to; the title in brackets is what readers see. Without brackets, the id is used as the title.
  • Quote the title if it contains parentheses or other symbols: subgraph eu ["Team (EU)"].
  • end must be alone on its line. A node can't be called end in lowercase for that reason.
  • A node belongs to the first subgraph it appears in. Declare it inside the group, then link it from anywhere.

Linking to a whole group

Use the subgraph id like a node id: push --> build points at the Build frame, and build --> ship links the two frames. It is the clearest way to show stages without wiring every inner node.

A direction per subgraph, and when it's ignored

Put direction TB (or LR, BT, RL) as the first line inside a subgraph to lay its content out differently from the rest of the chart. In the pipeline example, the chart flows left to right while each stage reads top to bottom.

Mermaid documents one catch: if any node inside the subgraph is linked to a node outside it, the subgraph's own direction is ignored and it follows the parent direction. That's why the example links the frames (build --> ship) rather than b3 --> s1.

You linkSubgraph direction
Subgraph id to subgraph id (build --> ship)Respected
Outside node to subgraph id (push --> build)Respected
Inner node to outside node (b3 --> live)Ignored: the parent direction wins

Styling a subgraph

Give one frame its own colors
flowchart LR
  subgraph eu [EU region]
    a[API] --> b[(DB)]
  end
  style eu fill:#eef6ea,stroke:#2e7d32

style takes the subgraph id. For the same look on several frames, define a classDef and apply it with class eu,us myClass.

Describe it in words instead

Paste notes about a process, a team or an exchange between systems. Arrowly draws the flowchart, org chart or sequence diagram and gives you Mermaid code that always parses. 3 free diagrams a day, no signup.

Draw from my notes

Questions

Can a node be in two subgraphs?

No. Mermaid places each node in one group. Create two nodes with the same label if the step really happens in both places.

Why is my subgraph drawn in the wrong order?

Mermaid's layout follows the links, not the order you write things. Add a link between the groups (or an invisible one with ~~~) to force the order.

Can I collapse a subgraph?

Not in a static render. For a big chart, draw an overview with one node per group, and a detailed chart per group.