Basics
- The first line declares the diagram type, e.g.
flowchart LR,sequenceDiagram,erDiagram. - Comments start with
%%. - Wrap labels with special characters in double quotes:
A["Hello (world)"]. - Flowchart directions:
TB/TD(top-down),BT,LR,RL. - Markdown in labels:
A["`**bold** and _italic_`"].
Flowchart
Node shapes
| Syntax | Shape |
|---|---|
A[Text] | Rectangle |
A(Text) | Rounded rectangle |
A([Text]) | Stadium / pill |
A[[Text]] | Subroutine |
A[(Text)] | Cylinder (database) |
A((Text)) | Circle |
A(((Text))) | Double circle |
A>Text] | Asymmetric flag |
A{Text} | Rhombus (decision) |
A{{Text}} | Hexagon |
A[/Text/] | Parallelogram |
A[\Text\] | Parallelogram (alt) |
A[/Text\] | Trapezoid |
A[\Text/] | Trapezoid (alt) |
A@{ shape: doc, label: "Report" } | Named shape syntax (v11+) |
Links and arrows
| Syntax | Link |
|---|---|
A --> B | Arrow |
A --- B | Line without arrow |
A -.-> B | Dotted arrow |
A ==> B | Thick arrow |
A --text--> B | Arrow with label |
A -->|text| B | Arrow with label (alt) |
A --o B | Circle end |
A --x B | Cross end |
A <--> B | Bidirectional |
A ---> B | Longer link (more rank) |
A ~~~ B | Invisible link (layout only) |
A --> B & C | Chain to several nodes |
Subgraphs
flowchart LR
subgraph API["API layer"]
direction TB
G[Gateway] --> S[Service]
end
U[User] --> APIMore in the flowchart guide.
Sequence diagram
| Syntax | Meaning |
|---|---|
A->>B: msg | Solid line with arrowhead (sync call) |
A-->>B: msg | Dotted line with arrowhead (reply) |
A->B: msg | Solid line, no arrowhead |
A-)B: msg | Async message |
A-xB: msg | Message with cross (lost/failed) |
activate A / deactivate A | Activation bar (or use +/- after the arrow) |
Note right of A: text | Note (left of / right of / over A,B) |
loop / alt / else / opt / par / and / critical / break ... end | Control blocks |
autonumber | Number every message |
box Aqua Group ... end | Group participants |
rect rgb(240,240,255) ... end | Highlight a region |
See the sequence diagram reference and tutorial.
Class diagram
| Syntax | Relationship |
|---|---|
A <|-- B | Inheritance |
A *-- B | Composition |
A o-- B | Aggregation |
A --> B | Association |
A ..> B | Dependency |
A ..|> B | Realization |
A "1" --> "*" B | Cardinality |
+public -private #protected ~package | Member visibility |
<<interface>> A | Annotation |
ER diagram cardinality
| Syntax | Meaning |
|---|---|
|o--o| | Zero or one to zero or one |
||--|| | Exactly one to exactly one |
}o--o{ | Zero or more to zero or more |
}|--|{ | One or more to one or more |
||--o{ | One to zero-or-more (classic 1:N) |
-- vs .. | Identifying (solid) vs non-identifying (dashed) |
string name PK "comment" | Attribute with key and comment |
Generate one from a schema with the SQL to ER diagram guide.
Styling
classDef, style and linkStyle
flowchart LR
A[Start] --> B[Process] --> C[Done]
classDef ok fill:#dcfce7,stroke:#16a34a,color:#14532d
classDef warn fill:#fef9c3,stroke:#ca8a04
class C ok
B:::warn
style A stroke-dasharray: 5 5
linkStyle 0 stroke:#0d9488,stroke-width:2pxRead Mermaid themes and styling for more.
Config & themes
Use a YAML frontmatter block at the very top of the diagram to set a title, theme, look and per-diagram options. Built-in themes: default, neutral, dark, forest and base (customisable with themeVariables).
---
title: My diagram
config:
theme: forest
look: handDrawn
flowchart:
curve: linear
---
flowchart LR
A --> BAll diagram types
| Diagram | Starts with | Key syntax |
|---|---|---|
| Flowchart | flowchart TD | flowchart TD · A[Text] · A(Text) / A([Text]) |
| Sequence Diagram | sequenceDiagram | sequenceDiagram · participant A as Alice · actor U |
| Class Diagram | classDiagram | classDiagram · class Name { +type field \n +method() ret } · + - # ~ |
| State Diagram | stateDiagram-v2 | stateDiagram-v2 · [*] --> A · A --> B : event |
| Entity Relationship Diagram | erDiagram | erDiagram · A ||--o{ B : label · |o / o| |
| Gantt Chart | gantt | gantt · title Text · dateFormat YYYY-MM-DD |
| Pie Chart | pie showData | pie · pie showData · title Text |
| Mindmap | mindmap | mindmap · Indentation · root((Text)) |
| Timeline | timeline | timeline · title Text · Period : Event |
| Git Graph | gitGraph | gitGraph · commit / commit id: "x" · commit type: HIGHLIGHT | REVERSE |
| User Journey | journey | journey · title Text · section Name |
| Quadrant Chart | quadrantChart | quadrantChart · x-axis Left --> Right · y-axis Bottom --> Top |
| Sankey Diagram | sankey | sankey · source,target,value · "Name, with comma",target,5 |
| XY Chart (Bar & Line) | xychart | xychart · xychart horizontal · title "Text" |
| Requirement Diagram | requirementDiagram | requirementDiagram · requirement name { id: … text: … risk: … verifymethod: … } · functionalRequirement / performanceRequirement / interfaceRequirement / physicalRequirement / designConstraint |
| C4 Diagram | C4Context | C4Context / C4Container / C4Component / C4Dynamic / C4Deployment · Person(alias, "Label", "Description") · System(alias, …) / System_Ext(alias, …) |
| Block Diagram | block | block (or block-beta) · columns 3 · a["Label"] |
| Packet Diagram | packet | packet (or packet-beta) · title Text · 0-15: "Field" |
| Kanban Board | kanban | kanban · id[Column title] · id[Card text] |
| Architecture Diagram | architecture-beta | architecture-beta · group id(icon)[Label] · service id(icon)[Label] in group |
| Radar Chart | radar-beta | radar-beta · axis id["Label"], … · curve id["Label"]{1, 2, 3} |
| Treemap | treemap-beta | treemap-beta · "Section" · "Leaf": 42 |
Cheat sheet FAQ
How do I add a comment in Mermaid?
Start a line with %%. Everything after it on that line is ignored by the parser.
How do I change the Mermaid theme?
Add a frontmatter block with config: theme: forest (or default, neutral, dark, base) at the top, or pick a theme in the MermaidViewer toolbar.
How do I use special characters in labels?
Wrap the label in double quotes, e.g. A["Price (USD): $10"]. Use #quot; for a literal double quote and entity codes like #9829; for symbols.
Why does my diagram break on the word end?
In flowcharts the lowercase word end closes a subgraph. Capitalise it (End) or wrap it in quotes.