Class diagram
classDiagram
direction LR
namespace zoo {
class Animal~T~ {
<<abstract>>
+String name
-int count$
+speak()* String
}
}
Animal <|-- Dog
Keeper "1" --> "0..*" Animal : feeds
Duck ()-- Quacks
note for Dog "good boy"
style Dog fill:#fef3c7
timeline:
enter Dog slide-bottom, enter Animal-Dog fade
enter Keeper slide-left, enter Keeper-Animal fade
enter note:1 fade, highlight Dog outline
Header and direction
The header is classDiagram or classDiagram-v2. A header with no content is a valid empty diagram.
direction TB (the default), BT, LR and RL can appear anywhere on a line of their own; if written more than once, the last one wins. direction TD is an error: write direction TB. A trailing ; is fine (direction LR;).
Any other line that contains direction TB, BT, RL or LR (in a label, a member, a note, a relationship label, and so on) is an error, because Mermaid would read the whole line as a direction statement and drop the rest. Reword the text or move the direction to a line of its own.
accTitle: text and accDescr: text (or the braced accDescr { ... } block), in lower case, set the diagram’s accessible title and description.
A YAML front matter (--- … ---) at the very top and %%{init: ...}%% directives are read as Mermaid reads them, but their settings are not applied yet: each is reported as a warning and the diagram is drawn without it.
Only a whole line whose first non-blank characters are %% is a comment. A %% in the middle of a line is kept as text inside a member, a label or a note, and is an error after a statement (A <|-- B %% note); put comments on a line of their own.
Classes
Class names may contain only letters, digits and _.
| Syntax | Description |
|---|---|
class Animal |
Declares a class |
class Animal { ... } |
Declares a class with members. { ends the first line; } may be on a line of its own, end the last member’s line, or close the block on the same line (class A { +int x }, class A {}) |
class Order["Order Line"] |
A label drawn in place of the name |
Animal : +int age |
Adds members one at a time outside a block |
class Shelf~T~ |
Generic, drawn as Shelf<T>. The generic belongs to the class’s first mention: class A and later class A~T~ is an error, as is a different generic later. It cannot be nested (~List~int~~ is an error; write ~List#lt;int#gt;~) or hold a < that opens a tag (~List<int>~); entity codes such as #lt; are decoded, and <br> breaks a line |
A~T~ --> B |
A generic may also be written on a relationship’s end |
<<interface>> |
Written as the first line inside the block, or as <<interface>> Shape after the class is declared. The content can be any text; a second, different annotation on the same class is an error |
class Animal:::hot |
Applies a classDef (see Styles) |
- A class named in a relationship is declared automatically.
- The same class may be declared many times; its members are merged. A member written twice is drawn twice.
Members
| Syntax | Meaning |
|---|---|
Starts with +, -, #, ~ |
public, private, protected, package. A space after the marker is kept (+ x) |
Ends with * |
abstract: the * is not drawn and the whole line is italic. On a method it may also stand right after ) (+run()* String) |
Ends with $ |
static: the $ is not drawn and the whole line is underlined |
Contains (: a method |
+mate(Animal partner) Animal: what follows ) is the return type, drawn after : as +mate(Animal partner) : Animal |
| Anything else: an attribute | String name: the last word is the name, the words before it are the type |
~T~in a member’s type is also drawn as<T>, as inList~int~ ids; here a generic may be nested (List~List~int~~ ids).- A member’s name may start with a digit and hold
-,.or letters of any script. Entity codes and character references are decoded (+#lt; xdraws+< x). - A
;does not separate members:+int x; +int y, in a block or afterAnimal :, is an error. Write each member on a line of its own. - Inside a block, a line that is only
%%is drawn as the member%%, not read as a comment.
Relationships
classDiagram
Animal <|-- Dog
Car *-- Wheel
Team o-- Player
Order --> Item
Service ..> Logger
Plugin ..|> Extension
A -- B
C .. D
Customer "1" --> "*" Invoice : issues
| Syntax | Meaning |
|---|---|
<|-- |
Inheritance |
*-- |
Composition |
o-- |
Aggregation |
--> |
Association |
-- |
Link (solid) |
..> |
Dependency |
..|> |
Realization |
.. |
Link (dashed) |
- Every type can be written the other way round, such as
--|>,--*or<..; both ends can also carry a head at once, as in<|--|>. - There are two lines:
--solid and..dashed. The left end can be<|,<,*,oor(), the right end|>,>,*,oor(), in any combination. - Multiplicity: either end can take quoted text,
A "1" --> "0..*" B. - Label: add
: textat the end. It is drawn on the line, on a background the theme’s--siren-edge-label-fillpaints, and wraps by itself once it is wider than 150px, as in Mermaid;<br>breaks a row. A class name, a member and a note never wrap. - Lollipop interface:
Duck ()-- Quacks. The()end does not become a class; it is drawn as an interface circle.
Namespace
classDiagram
namespace shapes {
class Circle
class Square
}
namespace io {
class Reader
}
Circle --> Reader
namespace name { opens a block and } closes it; the block may also be written on one line (namespace io { class Reader }). Inside it go class declarations, notes and nested namespaces, and nothing else: a relationship, a style, a classDef, a direction or an inline member there is an error, and an empty namespace is one too.
- Namespaces nest, as
namespace N { namespace M { ... } }or as the dotted namenamespace N.M { ... }; both draw the frame N around the frame M. A space in a name (namespace N M {) is an error; writeNMorN_M. - A namespace opened more than once is one namespace: its blocks are merged into one frame.
- A note written inside a namespace is drawn inside its frame.
- A class placed in two different namespaces stays in the first one and reports an error.
Note
note "text": a standalone note.note for Dog "text": attached beside a class. If it names a class that does not exist, the note is still drawn, unattached, with a warning.
Styles
classDiagram
class Order
class Invoice
class Payment
style Order fill:#dbeafe,stroke:#2563eb
classDef money fill:#dcfce7,color:#166534
cssClass "Invoice,Payment" money
style class property:value,...classDef name property:value,...cssClass "A,B" name: the class list must be quoted.class Animal:::nameapplies a class on the declaration.- A
styleon a class beats aclassDefapplied to it, whichever is written first. - Applying a name no
classDefdefines is a warning, and the class is drawn unstyled. - While a timeline
highlightis on a class, the highlight’s border colour and width win over the author’sstroke. - Values follow the same rules as in flowcharts; see Flowchart styles.
Interaction
| Syntax | Effect |
|---|---|
click Dog href "url" "tooltip" |
Link |
link Dog "url" "tooltip" |
Link |
click Dog call fn("arg") "tooltip" |
Calls a callback |
callback Dog "fn" "tooltip" |
Calls a callback |
The tooltip text is shown as a tooltip and may be omitted. URLs are restricted the same way as in flowcharts.
Timeline targets
| Element | id | Example |
|---|---|---|
| Class | The class name (generics not included) | Animal (class Animal~T~) |
| Relationship | left-right (as written, left to right), with #2 for a repeat |
Animal-Dog, Keeper-Animal |
| namespace | namespace:N, numbered in the order the blocks open, outer before inner |
namespace:1 |
| note | note:N, in source order |
note:1 |
- The id of
Animal <|-- DogisAnimal-Dog: only left and right matter, not the arrow’s direction. - A namespace opened more than once keeps the id of its first block.
- A class’s members and a lollipop interface’s label cannot be targeted on their own.
Not yet supported
| Syntax | Current behavior |
|---|---|
class A name (applying a style class) |
Error; use cssClass "A" name or class A:::name instead |
| Backtick class names, non-ASCII class names | Error |
click ... href "url" "tooltip" _blank |
Error; class diagram links cannot set a link target |