Mermaid Class Diagram Syntax & Examples
Reference for Mermaid class diagram syntax — class declarations, fields, methods, visibility modifiers, inheritance, composition, and multiplicity. Copy-paste examples.
Syntax reference, layout guidance, and ready-to-open examples for this diagram type.
A syntax reference for Mermaid's classDiagram grammar — read this when you need the exact notation for a relationship arrow, visibility modifier, or annotation, then try the result in the editor.
Mermaid class diagram constructs, one per row
| Construct | Syntax | Notes |
|---|---|---|
| Diagram keyword | classDiagram | Must be the first line, on its own. |
| Class with members | class Animal { +String name } | Members go inside the braces, one per line. A class can also be declared bare as class Animal. |
| Visibility | + public - private # protected ~ package | The prefix character is stripped from the label and drives the rendered marker. |
| Method | +isMammal() bool | Parentheses are what distinguish a method from a field; the trailing word is the return type. |
| Annotation | <<interface>> | Written on its own line inside the braces. <<abstract>>, <<enum>> and any custom stereotype work the same way. |
| Generics | class Box~T~ | Tildes stand in for angle brackets, which would otherwise be read as markup. |
| Inheritance / realization | Animal <|-- Duck | Solid is inheritance; the dotted forms <|.. and ..|> are realization. Reversing the arrow (Duck --|> Animal) points it the other way. |
| Composition / aggregation | Product *-- LineItem | *-- is composition (filled diamond), o-- is aggregation (hollow diamond). |
| Dependency / association | Source ..> Target | Dotted is a dependency; the solid --> is a plain association. |
| Cardinality and label | Owner "1" --> "*" Dog : owns | Quoted multiplicities sit next to each end. "1", "*", "0..1", "1..*" and ranges like "2..4" all render. |
Syntax reviewed
View Mermaid sourcePlain-text diagram syntax — copy or edit directly.
1classDiagram2 class Animal {3 +String name4 +int age5 +makeSound() void6 }7 class Dog {8 +String breed9 +bark() void10 }11 class Owner {12 +String name13 }14 Animal <|-- Dog15 Owner "1" --> "*" Dog : ownsWhat is a Mermaid class diagram
A Mermaid class diagram captures types, their fields and methods, and the relationships between them — the standard UML class-diagram view in a text-based form. It is the right shape when the question is what-are-the-types-and-how-do-they-relate: domain model reviews, module boundaries, API contract walkthroughs, onboarding documentation that introduces a codebase. If the question is what-order-do-things-happen-in, use a sequence diagram instead; if it is what-states-can-an-entity-be-in, use a state diagram. Class diagrams describe structure, not behaviour or time.
Declaring a class — fields & methods
A class block starts with class Name { ... }. Inside, each line is a field (+name: type) or method (+method(arg: type) returnType). The leading symbol is a visibility modifier: + public, - private, # protected, ~ package-private. Field syntax accepts both name: type and the C-style type name forms. Methods always have parentheses, even when empty. Mermaid is lenient about whitespace inside the braces — you can write each member on its own line for readability or pack several into one line.
class Animal { +String name }— single field+method(arg: type) returnType— public method with typed arg- Visibility modifiers:
+public,-private,#protected,~package-private
Relationships — inheritance, composition, association
Mermaid supports the full UML relationship vocabulary. Animal <|-- Dog reads as Dog inherits from Animal (the arrowhead <| points at the parent). Composite *-- Part is composition (the diamond * is filled, meaning the part is owned by the composite and dies with it). Aggregate o-- Member is aggregation (open diamond, weaker ownership). Source --> Target is a directional association (a uses-a relationship). Source ..> Target is a dashed dependency (lighter coupling). Interface ..|> Implementation is realization (a class implements an interface). Each arrow direction encodes a specific semantic — the reader recognises the relationship type from the arrow alone before reading any label.
<|--— inheritance (Class inherits from parent)*--— composition (owned, dies-with parent)o--— aggregation (referenced, lives independently)-->— association (general uses-a)..>— dependency (loose coupling)..|>— realization (implements an interface)
Multiplicity — one-to-many, many-to-many
Multiplicity is expressed as quoted strings on the relationship line: Owner "1" --> "*" Dog. The two strings appear at each end of the line and tell readers the cardinality at that end. Common values: "1" exactly one, "0..1" zero or one, "*" zero or more, "1..*" one or more, or any specific range like "2..4". Labels can be added after the arrow with a colon: Owner "1" --> "*" Dog : owns. The combination of cardinality at both ends plus a verb label communicates the relationship completely.
Annotations & stereotypes
Mermaid supports class annotations using the <<...>> syntax: class Database { <<interface>> }. Common stereotypes include <<interface>>, <<abstract>>, <<enum>>, <<service>>, and custom ones. The annotation appears above the class name in the rendered diagram and helps readers categorise classes without inspecting their members. For enumerations specifically, you can list values directly inside the class block: class Status { <<enum>> PENDING APPROVED REJECTED }. These annotations are particularly valuable in larger diagrams where the role of each class is otherwise not obvious.
Common patterns & gotchas
Three patterns appear constantly. Domain models combine class blocks with inheritance and composition — Product *-- LineItem style. Service architectures use <<interface>> annotations with realization arrows. API contracts use abstract classes for the request/response base types. The most common gotcha: forgetting that Mermaid is case-sensitive on class names — class User { } and a later reference to user are two different classes. Second gotcha: methods without parentheses are parsed as fields, so +save void (without ()) becomes a field, not a method. Always include () on methods.
- Class names are case-sensitive —
Useranduserare different classes - Methods always need
()— without parens they parse as fields - Whitespace inside class blocks is flexible; one member per line reads best