Skip to main content

Mermaid class diagram cheat sheet

Class diagrams describe static types, members and relationships. Choose a sequence diagram for runtime messages, or a flowchart for decisions and process steps.

Build this in Mermaid Editor →

Quick start

Methods have parentheses; attributes do not. Place a method return type after a space.

Mermaid
classDiagram
  class Document {
    +String title
    -bool published
    +publish() bool
  }
  class Reader
  Reader --> Document : reads

Loading…

Syntax reference

Syntax reference
SyntaxMeaning
classDiagram / class Id / class Id["Label"]Declare a diagram, class id and optional display label.
class Id { members } / Id : memberAdd attributes and methods inside braces or with colon notation.
+ / - / # / ~Public, private, protected and package/internal visibility.
+method(arg) ReturnType / method()* / member$Return type follows a space; * marks abstract methods and $ marks static members.
List~T~ / Box~List~T~~Tildes delimit generic parameters; comma-separated generic arguments are not supported.
<|-- / *-- / o-- / --> / -- / ..> / ..|> / ..Inheritance, composition, aggregation, association, solid link, dependency, realization, dotted link.
A "1" --> "0..*" B : labelQuoted multiplicities appear at relation endpoints; a colon adds a relation label.
<<interface>> / <<abstract>> / <<service>> / <<enumeration>>Annotate a class with a stereotype.
namespace Name { class A }Group classes; dotted and nested namespaces are supported in 11.15.
note "Text" / note for A "Text"Add a general note or attach one to a class.
direction LR / RL / TB / BTSet the layout direction.
style A fill:#d3e5ff / classDef / cssClass / :::Style a class directly or apply a reusable style class.
click A href "URL" / click A call callback()Interactive links and callbacks require suitable host security settings.
%% / accTitle: / accDescr:Comments and accessible descriptions.

Visibility, methods and generics

Visibility is a leading symbol. A method is recognized by parentheses; its return type follows the closing parenthesis. Generic brackets use tildes, and generic arguments containing a comma remain unsupported.

Mermaid
classDiagram
  class Repository~T~ {
    -List~T~ items
    #int count
    ~String scope
    +find(id) T
    +create() T*
    +int total$
  }

Loading…

Eight relationship forms and multiplicity

Read the endpoint marker: a triangle indicates inheritance or realization; diamonds indicate ownership. Cardinalities are text in quotes, not validated database constraints. Reverse arrow orientation when the relationship needs the marker on the other endpoint.

Mermaid
classDiagram
  Base <|-- Derived
  Whole *-- Part
  Group o-- Member
  Reader --> Document
  A -- B
  Client ..> Service
  Contract <|.. Implementation
  X .. Y
  Reader "1" --> "0..*" Document : reads

Loading…

Stereotypes, namespaces and notes

Namespaces group related classes; they are presentation clusters rather than programming language scope validation. Notes can describe a whole diagram or a single class. Keep stereotypes close to the class they qualify.

Mermaid
classDiagram
  direction LR
  namespace Publishing {
    class Store {
      <<interface>>
      +save() bool
    }
    class Archive
  }
  Store <|.. Archive
  note for Store "Persistence boundary"
  note "Static structure"

Loading…

Reusable styles and direct styles

ClassDef defines a visual class; cssClass applies it to a model class. The shorthand ::: applies it while declaring the class. Direct style overrides only the named element.

Mermaid
classDiagram
  classDef important fill:#d3e5ff,stroke:#0070f3,color:#171717
  class Guide:::important
  class Archive
  cssClass "Archive" important
  style Guide fill:#d3e5ff,stroke:#0070f3,stroke-width:2px
  Guide --> Archive

Loading…

Namespace layout

Hierarchical namespaces render parent clusters by default. Set hierarchicalNamespaces to false for compact, fully qualified namespace labels. This is a layout choice and does not rename class ids.

Mermaid
---
config:
  class:
    hierarchicalNamespaces: false
---
classDiagram
  namespace Docs.Public {
    class Guide
  }
  namespace Docs.Internal {
    class Draft
  }
  Draft ..> Guide

Loading…

Common mistakes

Forgetting a closing brace

Close the member block before adding relationships.

Invalid syntax — corrected example below

classDiagram
  class Guide {
    +String title

Treating generics like ordinary angle brackets

Use List~T~. Avoid comma-separated generic parameters; they are not supported by this notation.

Limitations

  • This notation does not validate a type system or generate executable classes.
  • Comma-separated generic arguments are unsupported.
  • Automatic layout determines exact node positions; relationships are documentation, not enforced constraints.

Frequently Asked Questions

How do I write a method return type?
Write the method with parentheses, then a space and the return type, for example +find(id) Document.
How do I show composition?
Use *-- with the filled diamond at the owning class. Aggregation uses o-- with an unfilled diamond.
Can I group classes?
Yes. Put class declarations inside a namespace block. Nested and dotted namespaces are supported in Mermaid 11.15.