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 --> EBasic Syntax:
graph TDdefines a top-down graph (TD = Top Down)A[Text]creates a rectangular nodeB{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 ResponseBasic Syntax:
sequenceDiagramdefines a sequence diagramparticipant Namedefines a participantA->>B: Textcreates a solid arrow for synchronous messagesA-->>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 <|-- CatBasic Syntax:
classDiagramdefines a class diagramclass ClassName {}defines a class with properties/methods+indicates public properties/methods-indicates private properties/methodsA <|-- 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 stateA --> Bcreates a transition from A to BA --> B: Textcreates a labeled transition
Entity Relationship
erDiagram
USER ||--o{ ORDER : places
ORDER ||--|{ ITEM : contains
USER ||--o{ REVIEW : writes
ITEM ||--o{ REVIEW : receivesBasic 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, 3dBasic Syntax:
ganttdefines a Gantt charttitle Titlesets the chart titledateFormat Formatsets the date formatsection Namedefines a project sectionTask Name: ID, Start Date, Duration/End Datedefines a task, its start date, and its duration or end datedone/activeindicates task statusafter IDindicates after another task
Pie Chart
pie title Time Distribution
"Coding" : 40
"Debugging" : 25
"Learning" : 20
"Coffee Break" : 15Basic Syntax:
piedefines a pie charttitle 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 DocumentationKeep 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