Mermaid Class Diagram

UML class diagrams written as text, rendered in Markdown

A Mermaid class diagram describes classes, their fields and methods, and how they relate — UML-style, but written as text. Keep it in a README or a design doc and it renders wherever Mermaid does: GitHub, GitLab, Obsidian and MDViewer.

Classes, Attributes and Methods

Start with classDiagram. Define a class with a block of members; methods have parentheses, and a return type can follow them:

Mermaid ```mermaid classDiagram class Animal { +String name +int age +makeSound() void } class Dog { +String breed +fetch() void } Animal <|-- Dog ```
Mermaid class diagram: Dog inherits from Animal

The + in front of a member is its visibility:

  • + public, - private, # protected, ~ package/internal
  • Add $ after a method for static and * for abstract: +create()$

Relationships

Arrows between class names describe how classes relate. Text after the colon labels the line, and quoted values add cardinality:

Mermaid ```mermaid classDiagram Customer "1" --> "*" Order : places Order *-- LineItem : contains Order o-- Coupon PaymentMethod <|.. CardPayment Order ..> Invoice : creates ```
Mermaid class diagram showing association, composition, aggregation, realization and dependency
ArrowMeaning
<|--Inheritance (extends)
*--Composition (part cannot exist alone)
o--Aggregation (part can exist alone)
-->Association
..>Dependency
..|>Realization (implements an interface)
"1" --> "*"Cardinality: one to many

Interfaces, Annotations and Generics

Mark a class with an annotation such as <<interface>> or <<abstract>>. Generic types use tildes instead of angle brackets: Repository~T~.

Mermaid ```mermaid classDiagram class Repository~T~ { <<interface>> +findById(id) T +save(T item) void } class UserRepository { -Database db +findById(id) User +save(User item) void } Repository <|.. UserRepository ```
Mermaid class diagram: UserRepository implements the generic Repository interface

Tips

  • Show only the members that matter for the point you are making — a diagram is not the source code.
  • Change the layout with direction LR on the line after classDiagram.
  • For database tables and foreign keys, an ER diagram is the better fit.

More Mermaid guides: What is Mermaid, flowcharts, sequence diagrams, timelines, Gantt charts, class diagrams, ER diagrams and pie charts. To open .mmd files on a Mac, see how to open Mermaid files.

FAQ

How do I draw a class diagram in Markdown?

Add a fenced code block with the language mermaid, start it with classDiagram, define classes with class Name { … } and connect them with arrows such as Animal <|-- Dog.

How do I show inheritance in a Mermaid class diagram?

Use Parent <|-- Child. For a class that implements an interface, use the dotted version: Interface <|.. Class.

How do I write generics in a Mermaid class diagram?

Use tildes around the type: class List~T~ or +getItems() List~Item~. Mermaid renders them as angle brackets.

What is the difference between composition and aggregation in Mermaid?

Composition (*--, filled diamond) means the part cannot exist without the whole. Aggregation (o--, hollow diamond) means it can.

View It with MDViewer

MDViewer renders Mermaid diagrams inside any .md file on your Mac — click a diagram to zoom and pan. Paste an example from this guide into a file and open it, or try it without installing in the free online viewer.

Download MDViewer

Lite is free for personal use; Pro is $9.99 one-time. Requires macOS 13.0 or later, Intel and Apple Silicon.