Mermaid guide

Mermaid Class Diagram Syntax: Classes, Members, Relationships, and Generics

The class diagram is Mermaid's UML structure view: classes with fields and methods, connected by typed relationships such as inheritance, composition, and association. It reads well for domain models and for explaining how a module's types fit together.

A class is declared with class Name { ... } or built up implicitly by adding members with Name : +field. Relationship lines use a small set of arrow tokens, each with a UML meaning, and can carry cardinality strings on either end.

  • object-oriented design
  • domain models
  • API surface documentation
  • refactoring discussions

Example

Mermaid Class Diagram example

owns

1

many

Diagram

+String id

+String code

+String r2_key

+Int view_count

+save()

+share() : String

User

+String id

+String email

+String plan

+getDiagrams() : Diagram[]

classDiagram
    class Diagram {
        +String id
        +String code
        +String r2_key
        +Int view_count
        +save()
        +share() String
    }
    class User {
        +String id
        +String email
        +String plan
        +getDiagrams() Diagram[]
    }
    User "1" --> "many" Diagram : owns

Syntax

Classes and members

classDiagram
    class Order {
        +String id
        -Money total
        #List~Item~ items
        +addItem(Item item) void
        +total() Money
    }
Classes and members — Mermaid class

Order

+String id

-Money total

#List<Item> items

+addItem(Item item) : void

+total() : Money

Prefixes: + public, - private, # protected, ~ package. Generics use tildes: List~Item~ renders as List<Item>.

Try this snippet →

Relationships

classDiagram
    Animal <|-- Dog : inheritance
    Car *-- Engine : composition
    Team o-- Player : aggregation
    Client --> Server : association
    Client ..> Logger : dependency
    Shape <|.. Circle : realization
Relationships — Mermaid class

inheritance

composition

aggregation

association

dependency

realization

Animal

Dog

Car

Engine

Team

Player

Client

Server

Logger

Shape

Circle

The arrowhead points at the parent, whole, or target. Read <|-- as "is-a", *-- as "owns", o-- as "has".

Try this snippet →

Cardinality

classDiagram
    Customer "1" --> "0..*" Order : places
    Order "1" *-- "1..*" LineItem
Cardinality — Mermaid class

places

1

1

0..*

1..*

Customer

Order

LineItem

Quoted strings on either side of the arrow; use 1, 0..1, 1..*, *, or n.

Try this snippet →

Annotations

classDiagram
    class Repository {
        <<interface>>
        +find(id) Entity
    }
    class Status {
        <<enumeration>>
        ACTIVE
        ARCHIVED
    }
Annotations — Mermaid class

«interface»

Repository

+find(id) : Entity

«enumeration»

Status

ACTIVE

ARCHIVED

Stereotypes <<interface>>, <<abstract>>, <<service>>, <<enumeration>> render above the class name.

Try this snippet →

Common mistakes

These are the parse errors that come up most, including in code written by ChatGPT and Claude. Paste any failing diagram into the editor and it marks the offending line.

  • Angle brackets for generics: List<Item> items.

    Fix: Mermaid uses tildes: List~Item~ items. Angle brackets collide with the arrow tokens.

  • Method parameters with spaces and no parentheses on the return type: +save(Order order): void.

    Fix: Return type goes after the closing parenthesis with a space, no colon: +save(Order order) void.

  • Relationship arrow drawn in the wrong direction, e.g. Dog <|-- Animal.

    Fix: The open triangle sits on the parent: Animal <|-- Dog. Read the arrow as "Animal is extended by Dog".

Real-world examples

Complete, copy-ready class diagrams for common situations. Each one opens in the editor with a click.

Browse all examples →

FAQ

Can I namespace classes?

Yes: wrap them in namespace Billing { class Invoice ... }. Classes inside render in a labelled box, and you can still draw relationships across namespaces.

How do I link a class to its source file?

link Order "https://github.com/org/repo/blob/main/order.ts" makes the class name clickable in renderers that allow links. GitHub's renderer strips them.