Mermaid Syntax Guide and Examples

Quickly learn how to create various diagrams using Mermaid syntax

Mermaid syntax quick start

Choose a diagram below, copy its plain-text source, and select Try in Editor to preview it. For flowcharts, graph TD and flowchart TD both create a top-down layout; graph LR runs from left to right. Use node IDs to connect steps and quotes around labels containing punctuation. Paste the source without Markdown code fences.

Flowchart

graph TD
  A[Start Project] --> B{Have Requirements?}
  B -->|Yes| C[Start Coding]
  B -->|No| D[Gather Requirements]
  D --> B
  C --> E{Tests Pass?}
  E -->|Yes| F[Deploy!]
  E -->|No| G[Fix Bugs]
  G --> E

Basic Syntax:

  • graph TDdefines a top-down graph (TD = Top Down)
  • A[Text]creates a rectangular node
  • B{Text}creates a diamond node (condition)
  • -->creates an arrow connection
  • -->|Text|creates a labeled arrow

Sequence Diagram

sequenceDiagram
  participant U as User
  participant S as System
  participant D as Database
  
  U->>S: Submit Request
  S->>D: Query Data
  D-->>S: Return Results
  S-->>U: Display Response

Basic Syntax:

  • sequenceDiagramdefines a sequence diagram
  • participant Namedefines a participant
  • A->>B: Textcreates a solid arrow for synchronous messages
  • A-->>B: Textcreates a dashed arrow for return/asynchronous messages

Class Diagram

classDiagram
  class Animal {
    +String name
    +int age
    +makeSound()
  }
  class Dog {
    +String breed
    +bark()
    +fetch()
  }
  class Cat {
    +String color
    +meow()
    +sleep()
  }
  Animal <|-- Dog
  Animal <|-- Cat

Basic Syntax:

  • classDiagramdefines a class diagram
  • class ClassName {}defines a class with properties/methods
  • +indicates public properties/methods
  • -indicates private properties/methods
  • A <|-- Bindicates B inherits from A (B is a subclass of A)

State Diagram

stateDiagram-v2
  [*] --> Idle
  Idle --> Processing: Start Task
  Processing --> Success: Complete
  Processing --> Error: Failed
  Error --> Processing: Retry
  Success --> [*]

Basic Syntax:

  • stateDiagram-v2defines a state diagram
  • [*]represents the start or end state
  • A --> Bcreates a transition from A to B
  • A --> B: Textcreates a labeled transition

Entity Relationship

erDiagram
  USER ||--o{ ORDER : places
  ORDER ||--|{ ITEM : contains
  USER ||--o{ REVIEW : writes
  ITEM ||--o{ REVIEW : receives

Basic Syntax:

  • erDiagramdefines an entity relationship diagram
  • ||--o{represents a one-to-many relationship
  • ||--|{represents a one-to-many (mandatory) relationship
  • }|..|{represents a many-to-many relationship (dashed line)
  • : Textdefines the relationship description

Gantt Chart

gantt
  title Project Timeline
  dateFormat YYYY-MM-DD
  section Planning
  Requirements :done, req, 2024-01-01, 7d
  Design :active, des, after req, 10d
  section Development
  Coding :dev, after des, 14d
  Testing :test, after dev, 7d
  section Launch
  Deploy :deploy, after test, 3d

Basic Syntax:

  • ganttdefines a Gantt chart
  • title Titlesets the chart title
  • dateFormat Formatsets the date format
  • section Namedefines a project section
  • Task Name: ID, Start Date, Duration/End Datedefines a task, its start date, and its duration or end date
  • done/activeindicates task status
  • after IDindicates after another task

Pie Chart

pie title Time Distribution
  "Coding" : 40
  "Debugging" : 25
  "Learning" : 20
  "Coffee Break" : 15

Basic Syntax:

  • piedefines a pie chart
  • title Titlesets the pie chart title
  • "Label" : Valuedefines a slice of the pie chart

Additional Resources

This page provides basic Mermaid syntax guidelines. For more advanced features and detailed syntax, please refer to:

Mermaid Official Documentation

Keep editing your Mermaid diagram

Choose an example above, edit its code with a live preview, then export PNG or SVG. No registration required.

Start Tutorial