Blog

Mermaid Diagrams in Markdown: Syntax and Examples

Write flowcharts, sequence diagrams and ER diagrams in Markdown with Mermaid. The syntax for each diagram type, plus the errors that break rendering.

By Markdown Viewer5 min read

A diagram that lives in a PNG goes stale the first time the code changes. Mermaid solves that by keeping diagrams as text: a fenced code block in the same Markdown file as the documentation, rendered by whatever is reading it.

GitHub renders Mermaid in READMEs and issues. So do GitLab, Obsidian, Notion and most modern Markdown tools. This is the syntax for the diagram types you will actually use, and the parser errors that account for most broken diagrams.

How Mermaid works in Markdown

Use a fenced code block with the language tag mermaid:

```mermaid
flowchart LR
  A[Draft] --> B[Review]
  B --> C[Publish]
```

That is the whole integration. No image files, no export step, no binary in the repository — and a diff on the diagram is a readable text diff.

If a viewer shows the fence as code instead of drawing it, the viewer does not support Mermaid. The Mermaid editor renders it; GitHub renders it in Markdown files but not in every context.

Flowcharts

The most-used type. Direction comes first: TB/TD top-down, LR left-right, RL, BT.

flowchart TD
  A[User submits form] --> B{Valid?}
  B -- Yes --> C[Save record]
  B -- No --> D[Show errors]
  D --> A
  C --> E[Send confirmation]

Node shapes:

Syntax Shape
A[Text] Rectangle
A(Text) Rounded rectangle
A([Text]) Stadium
A{Text} Diamond (decision)
A[(Text)] Cylinder (database)
A[[Text]] Subroutine
A((Text)) Circle

Link types:

A --> B      solid arrow
A --- B      solid line
A -.-> B     dotted arrow
A ==> B      thick arrow
A -->|label| B

Keep labels short. A flowchart whose nodes contain three sentences is a document pretending to be a diagram.

Sequence diagrams

sequenceDiagram
  participant U as User
  participant A as API
  participant D as Database
  U->>A: POST /orders
  A->>D: INSERT order
  D-->>A: order id
  A-->>U: 201 Created
  • ->> request, -->> response (dashed)
  • participant Name as Label fixes the order of columns — without it, participants appear in first-use order
  • Note over A,D: text adds a note spanning participants
  • alt / else / end expresses branches; loop / end repeats

Class and state diagrams

classDiagram
  class Document {
    +String title
    +save() void
    +render() String
  }
  class MarkdownDocument {
    +parse() AST
  }
  Document <|-- MarkdownDocument

<|-- inheritance, *-- composition, o-- aggregation, --> association, ..> dependency.

stateDiagram-v2
  [*] --> Draft
  Draft --> Review: submit
  Review --> Draft: request changes
  Review --> Published: approve
  Published --> [*]

[*] is the start or end state, and the text after : labels the transition.

Entity-relationship diagrams

erDiagram
  USER ||--o{ DOCUMENT : owns
  DOCUMENT ||--|{ VERSION : has
  USER {
    string id PK
    string email
  }
  DOCUMENT {
    string id PK
    string title
    date created_at
  }

Cardinality reads left to right: ||--o{ is “exactly one to zero or more”. PK, FK and UK mark keys.

Timeline, Gantt and pie charts

gantt
  title Release plan
  dateFormat YYYY-MM-DD
  section Build
  Parser        :a1, 2026-11-01, 10d
  Renderer      :a2, after a1, 7d
  section Ship
  Docs          :b1, 2026-11-12, 5d
pie title Documents by format
  "Markdown" : 62
  "HTML" : 18
  "PDF" : 20

Gantt needs a dateFormat and real dates; relative durations (after a1, 7d) are easier to maintain than fixed dates, which go stale as soon as a milestone slips.

Styling and orientation

Mermaid applies a default theme. Per-diagram styling is possible with classDef:

classDef warn fill:#fef3c7,stroke:#b45309,color:#78350f
class D warn

Use it sparingly. A diagram with six custom colours communicates less than one with two, and inline styles do not adapt to dark mode — which matters because GitHub renders Mermaid in both themes.

The syntax errors that break diagrams

Mermaid’s parser fails on the whole block rather than the offending line, so one character can blank the diagram. In order of frequency:

  1. Parentheses or brackets inside a node label. A[Send email (async)] fails because ( starts a new shape. Wrap the label in quotes: A["Send email (async)"].
  2. Unquoted special characters. Colons, semicolons and inline code in labels are the usual culprits; quoting the label fixes all of them.
  3. A missing end. subgraph, alt, loop and opt all require one.
  4. graph mixed with flowchart syntax. Both are accepted, but node definitions from one style inside the other are not.
  5. Reserved words as node IDs. end, graph, class and style as identifiers cause confusing errors; rename to endNode.
  6. Leading whitespace before the diagram type. The first non-blank line must be flowchart, sequenceDiagram, classDiagram and so on.

When a diagram will not render, comment out half of it and re-render to find the line; the error message rarely points at it directly.

Mermaid on GitHub: what actually renders

  • README.md and other .md files: yes, including private repositories.
  • Issues, pull requests and discussions: yes.
  • Wiki pages: yes.
  • Markdown rendered inside comments in code review: yes.
  • Gists: no.

GitHub renders with a specific Mermaid version, so a feature added in a newer release may fail there while working in your local editor. If a diagram works locally but not on GitHub, check whether you used a recently added diagram type or directive.

Editing and previewing diagrams

Mermaid is unforgiving to write blind. Preview while you type: the Mermaid editor renders a diagram live next to its source, in light and dark themes, and the Markdown viewer renders Mermaid inside a full document so you can check it in context.

For the Markdown syntax around the fence — headings, lists, tables, maths — the cheat sheet covers the rest of the format.

Frequently asked questions

Is Mermaid supported by standard Markdown?

No. It is an extension implemented by the renderer. CommonMark and plain Markdown show the block as code. GitHub, GitLab, Obsidian and most modern tools render it.

Can I use Mermaid in a PDF?

Only if the converter renders it before printing. A browser-based Markdown-to-PDF conversion does, because the diagram is an SVG by the time the print engine runs. A plain text pipeline will not.

Should diagrams be committed as .mmd files?

Either works. Keeping the diagram inline in the document is better for docs that change with the diagram; a separate .mmd file is better when several documents share one diagram. Inline is the common choice.

Why does my diagram show as a grey box?

The renderer loaded but failed to parse the definition. Open the browser console: Mermaid logs a parse error with the token it choked on, which is far more useful than the box.

Try it in the browser

Open a .md file, edit it with live preview, and export to PDF, HTML or DOCX — all on your device, with no sign-up and no upload.

Open the Markdown viewer