Mermaid Syntax Reference
Quick reference for generating valid Mermaid diagram code.
Flowchart
graph TD
A[Start] --> B{Decision}
B -->|Yes| C[Action 1]
B -->|No| D[Action 2]
C --> E[End]
D --> EDirection
TD/TB- Top to bottomBT- Bottom to topLR- Left to rightRL- Right to left
Node Shapes
A[Text]- RectangleA(Text)- Rounded rectangleA([Text])- Stadium/pillA[[Text]]- SubroutineA[(Text)]- Cylinder (database)A((Text))- CircleA>Text]- AsymmetricA{Text}- Diamond (decision)A{{Text}}- HexagonA[/Text/]- ParallelogramA[\Text\]- Parallelogram altA[/Text\]- TrapezoidA[\Text/]- Trapezoid alt
Edge Styles
A --> B- ArrowA --- B- LineA -.-> B- Dotted arrowA ==> B- Thick arrowA -->|text| B- Arrow with label (preferred)A ---|text| B- Line with label (preferred)
Important: Always use pipe syntax -->|label| for edge labels. The space-dash syntax -- label --> can cause incomplete renders.
Subgraphs
graph TD
subgraph Group1 [Label]
A --> B
end
subgraph Group2
C --> D
end
B --> CSequence Diagram
sequenceDiagram
participant A as Alice
participant B as Bob
A->>B: Hello
B-->>A: Hi there
A->>+B: Start process
B-->>-A: DoneArrow Types
->>- Solid arrow-->>- Dashed arrow-x- Solid with x--x- Dashed with x-)- Solid open arrow--)- Dashed open arrow
Activations
+after arrow activates participant-after arrow deactivates participant
Notes and Boxes
sequenceDiagram
Note over A,B: Shared note
Note right of A: Side note
rect rgb(200, 220, 255)
A->>B: In a box
endLoops and Conditionals
sequenceDiagram
loop Every minute
A->>B: Ping
end
alt Success
B-->>A: Pong
else Failure
B-->>A: Error
end
opt Optional
A->>B: Extra step
endState Diagram
stateDiagram-v2
[*] --> Idle
Idle --> Processing : start
Processing --> Done : complete
Processing --> Error : fail
Error --> Idle : reset
Done --> [*]Composite States
stateDiagram-v2
state Active {
[*] --> Running
Running --> Paused : pause
Paused --> Running : resume
}
Idle --> Active : activate
Active --> Idle : deactivateNotes
stateDiagram-v2
State1 : Description here
note right of State1
Additional info
end noteClass Diagram
classDiagram
class Animal {
+String name
+int age
+makeSound() void
}
class Dog {
+bark() void
}
Animal <|-- Dog : extendsRelationships
<|--- Inheritance*--- Compositiono--- Aggregation-->- Association--- Link (solid)..>- Dependency..|>- Realisation..- Link (dashed)
Cardinality
classDiagram
Customer "1" --> "*" Order
Order "1" --> "1..*" LineItemVisibility
+Public-Private#Protected~Package/Internal
Entity-Relationship Diagram
erDiagram
CUSTOMER ||--o{ ORDER : places
ORDER ||--|{ LINE-ITEM : contains
PRODUCT }|..|{ LINE-ITEM : "ordered in"Relationship Types
||- Exactly one|{- One or moreo{- Zero or moreo|- Zero or one
Identifying vs Non-identifying
--- Identifying (solid)..- Non-identifying (dashed)
Attributes
erDiagram
CUSTOMER {
string id PK
string name
string email UK
}
ORDER {
int id PK
string customer_id FK
date created_at
}Styling
CSS Classes
graph TD
A:::highlight --> B
classDef highlight fill:#f96,stroke:#333Inline Styles
graph TD
A --> B
style A fill:#bbf,stroke:#333Tips
- Escape special characters: Use quotes for labels with special chars:
A["Label with (parens)"] - Multi-line labels: Use
<br/>for line breaks - Comments: Use
%%for comments that won't render - IDs vs Labels: Node IDs should be simple, labels can be complex:
node1["Complex Label Here"]