Mermaid.js Cheat Sheet

Quick reference for all Mermaid diagram types. Click "Copy" on any code block.

Flowcharts

Direction

graph TD   %% Top to Bottom
graph LR   %% Left to Right
graph BT   %% Bottom to Top
graph RL   %% Right to Left

Node Shapes

flowchart TD
    A[Rectangle]
    B(Rounded)
    C([Stadium])
    D[[Subroutine]]
    E[(Cylinder/DB)]
    F((Circle))
    G{Diamond}
    H{{Hexagon}}
    I>Flag]
    J[/Parallelogram/]
    K[\Parallelogram Alt\]
    L[/Trapezoid\]

Edge Types

flowchart LR
    A --> B           %% Arrow
    A --- B           %% Line
    A -.-> B          %% Dotted arrow
    A ==> B           %% Thick arrow
    A --o B           %% Circle end
    A --x B           %% Cross end
    A -->|label| B    %% Labeled arrow

Subgraphs

flowchart TB
    subgraph "Group Name"
        A --> B
    end
    subgraph Another
        direction LR
        C --> D
    end

Styling

flowchart TD
    A:::myClass --> B
    classDef myClass fill:#f9f,stroke:#333,color:black
    style B fill:#bbf,stroke:#33f

Sequence Diagrams

Basics

sequenceDiagram
    participant A as Alice
    actor U as User
    A->>B: Solid arrow (sync)
    B-->>A: Dotted arrow (response)
    A-)B: Open arrow (async)
    A-xB: Cross (lost message)

Activation

sequenceDiagram
    A->>+B: Request (activate)
    B-->>-A: Response (deactivate)

Blocks

sequenceDiagram
    autonumber

    loop Every 5s
        A->>B: Ping
    end

    alt Success
        B-->>A: 200 OK
    else Failure
        B-->>A: 500 Error
    end

    opt Optional step
        A->>B: Maybe
    end

    par Parallel
        A->>B: Request 1
    and
        A->>C: Request 2
    end

Notes

sequenceDiagram
    Note over A,B: Spanning note
    Note right of B: Side note
    rect rgb(200, 220, 255)
        A->>B: Highlighted section
    end

Gantt Charts

gantt
    title Project Timeline
    dateFormat YYYY-MM-DD
    axisFormat %b %d
    excludes weekends

    section Phase 1
    Task 1          :done, t1, 2025-01-01, 7d
    Task 2          :active, t2, after t1, 5d
    Milestone       :milestone, m1, after t2, 0d

    section Phase 2
    Task 3          :t3, after m1, 10d
    Critical task   :crit, t4, after t3, 3d

Task States

  • done — completed
  • active — in progress
  • crit — critical path
  • Combine: crit, done

Class Diagrams

Classes

classDiagram
    class ClassName {
        +String publicAttr
        -int privateAttr
        #bool protectedAttr
        ~float internalAttr
        +publicMethod() void
        -privateMethod(param) String
        +staticMethod()$ int
        +abstractMethod()* void
    }

Relationships

classDiagram
    A <|-- B    : Inheritance
    C *-- D     : Composition
    E o-- F     : Aggregation
    G --> H     : Association
    I ..> J     : Dependency
    K ..|> L    : Realization
    M "1" --> "*" N : Cardinality

Annotations

classDiagram
    class MyInterface {
        <<interface>>
    }
    class MyAbstract {
        <<abstract>>
    }
    class MyEnum {
        <<enumeration>>
        VALUE_A
        VALUE_B
    }

State Diagrams

stateDiagram-v2
    direction LR
    [*] --> Active
    Active --> Inactive : disable
    Inactive --> Active : enable
    Active --> [*] : delete

    state Active {
        [*] --> Running
        Running --> Paused : pause
        Paused --> Running : resume
    }

Special States

stateDiagram-v2
    state check <<choice>>
    state fork_state <<fork>>
    state join_state <<join>>

    note right of Active
        This is a note
    end note

ER Diagrams

erDiagram
    CUSTOMER {
        int id PK
        string name
        string email UK
    }
    ORDER {
        int id PK
        int customer_id FK
        date ordered_at
    }
    CUSTOMER ||--o{ ORDER : places

Cardinality

  • || — exactly one
  • o| — zero or one
  • }| — one or more
  • }o — zero or more
  • -- solid (identifying) / .. dashed (non-identifying)

Pie Charts

pie title Distribution
    "Category A" : 40
    "Category B" : 30
    "Category C" : 20
    "Other" : 10

Add showData after pie to show raw values.

Mind Maps

mindmap
    root((Central Topic))
        Branch 1
            Sub-topic A
            Sub-topic B
        Branch 2
            Sub-topic C
                Detail 1
                Detail 2
        Branch 3

Git Graphs

gitGraph
    commit
    commit
    branch develop
    checkout develop
    commit
    commit
    checkout main
    merge develop
    commit

Timeline

timeline
    title History
    2020 : Founded
         : MVP launched
    2021 : Series A
         : Reached 10k users
    2022 : Series B
         : International expansion

Themes & Config

%%{init: {'theme': 'dark'}}%%
graph TD
    A --> B

Available themes: default, dark, forest, neutral, base

Tips

  • Use %% for comments
  • Wrap special characters in quotes: A["Text (parens)"]
  • Use <br/> for line breaks in labels