Skip to content
Mernyx
Open menu

Mermaid Class Diagram Cheat Sheet

Types, their members, and how they relate.

Every picture on this page was drawn byMernyx, the native Mermaid editor for macOS. Not by a browser running Mermaid.js — the same renderer the app uses for its canvas.

Defining a class

Members are prefixed with a visibility marker: + public, - private.

classDiagram
  class Animal {
    +String name
    +int age
    +isMammal() bool
  }
A class box named Animal listing three members: name, age and an isMammal method.Expand diagram

Relation types

Six relations, each with its own arrowhead. The left two are UML classics.

classDiagram
  Animal <|-- Duck
  Car *-- Wheel
  Team o-- Member
  User --> Order
  Order ..> Invoice
Five class boxes joined by different arrowheads: a hollow triangle for inheritance, a filled diamond for composition, a hollow diamond for aggregation, a plain arrow and a dashed arrow.Expand diagram

Labelling a relation

A colon after the relation is the verb between the two types.

classDiagram
  Keeper "1" --> "*" Animal : feeds
  Animal "1" --> "0..*" Leg : has
Two class boxes joined by an arrow labelled feeds, with a one beside the first box and a many beside the second.Expand diagram

Cardinality

Quoted numbers before each end say how many of that type are involved.

classDiagram
  Customer "1" --> "*" Order : places
  Order "1" *-- "1..*" LineItem : contains
Three class boxes: Customer to Order with a one and a many, Order to LineItem with a one and a one-to-many.Expand diagram

Annotations

Angle brackets inside the class body label its kind.

classDiagram
  class Shape {
    <<interface>>
    +draw() void
  }
  Circle ..|> Shape
A class box named Shape marked as an interface with a draw method, connected by a dashed triangle to another class box.Expand diagram

Direction

Same keyword as flowcharts. Default is top to bottom.

classDiagram
  direction LR
  Animal <|-- Duck
  Animal <|-- Fish
Two class boxes side by side, both connected to a third class box above them by hollow triangles.Expand diagram

Draw it in Mernyx.

Open a .mmd file, move a node on the canvas, and the source comes with it. Native on macOS, no browser involved.

Diagram preview