DevToolsmith

Mermaid Class Diagram Syntax

A class diagram shows classes with their fields and methods and the relationships between them: inheritance, composition, aggregation and plain associations. Edit the example below and the diagram redraws as you type; every example on this page can be opened in the editor.

Runs 100% in your browser — nothing is uploaded

Mermaid code
Tip: drop a file here271 chars · 271 bytes · 16 lines
DiagramRendering…

Loading Mermaid…

What a Mermaid class diagram is for

A class diagram shows classes with their fields and methods and the relationships between them: inheritance, composition, aggregation and plain associations. Use it to document a domain model or the shape of an object-oriented codebase.

Class Diagram syntax

SyntaxMeaning
class User { … } Declare a class with members
+name: String or +String name Field; + public, - private, # protected, ~ package
+save() bool Method with return type
+find(id)$ / +area()* $ marks static, * marks abstract
class Box~T~ Generic type, written with tildes
A <|-- B Inheritance: B extends A
A *-- B Composition: B is part of A and dies with it
A o-- B Aggregation: A has B
A --> B Association
A ..> B Dependency
A ..|> B Realization: A implements B
A "1" --> "*" B : owns Cardinality and label
<<interface>> Shape Annotation, e.g. interface, abstract, enumeration
namespace Billing { … } Group classes

Basic example

classDiagram
    class Animal {
        +String name
        +int age
        +makeSound() void
    }
    class Dog {
        +String breed
        +fetch() void
    }
    class Cat {
        +bool indoor
        +purr() void
    }
    Animal <|-- Dog
    Animal <|-- Cat

Open this example in the Mermaid editor

More class diagram examples

Order domain model

Composition for line items (they don’t exist without the order), association with cardinality for the customer.

classDiagram
    class Customer {
        +UUID id
        +String email
        +placeOrder(cart) Order
    }
    class Order {
        +UUID id
        +OrderStatus status
        +total() Money
    }
    class LineItem {
        +String sku
        +int quantity
        +Money price
    }
    class OrderStatus {
        <<enumeration>>
        PENDING
        PAID
        SHIPPED
    }
    Customer "1" --> "*" Order : places
    Order *-- "1..*" LineItem
    Order --> OrderStatus

Open “Order domain model” in the editor

Interface and implementations

..|> shows that a class implements an interface; generics use tildes.

classDiagram
    class Repository~T~ {
        <<interface>>
        +find(id) T
        +save(entity T) void
    }
    class SqlUserRepository {
        -DataSource ds
        +find(id) User
        +save(entity User) void
    }
    class InMemoryUserRepository {
        -Map~UUID, User~ store
        +find(id) User
        +save(entity User) void
    }
    SqlUserRepository ..|> Repository
    InMemoryUserRepository ..|> Repository

Open “Interface and implementations” in the editor

Common mistakes

  • Generics use tildes, not angle brackets: List~String~, because < and > are part of the arrow syntax.
  • The arrowhead goes on the parent side: Animal <|-- Dog reads “Dog inherits from Animal”.
  • Members can also be added one per line outside the block: User : +String email.

Use it in Markdown

```mermaid
classDiagram
    class Animal {
        +String name
        +int age
        +makeSound() void
    }
    class Dog {
        +String breed
        +fetch() void
    }
    class Cat {
        +bool indoor
        +purr() void
    }
    Animal <|-- Dog
    Animal <|-- Cat
```

GitHub, GitLab, Azure DevOps, Obsidian, Notion and docs generators such as Docusaurus and MkDocs (with a plugin) draw fenced mermaid blocks. Check it locally in the Markdown preview.

Other Mermaid diagrams

Sources

Spotted a mistake? Email hello@devtoolsmith.com and we’ll fix it.

Frequently asked questions

How do I start a Mermaid class diagram?

Begin the diagram with the keyword classDiagram on its own line, then add one statement per line. In Markdown, wrap the whole diagram in a fenced code block with the language mermaid so GitHub, GitLab and most docs sites draw it.

How do I export a Mermaid class diagram as an image?

Paste the diagram into the editor on this page and use SVG or PNG above the preview. SVG stays sharp at any size and can be edited further; PNG is exported at twice the screen resolution for slides and documents. Nothing is uploaded.