Unsaved
Ready
Loading Monaco Editor...
Preview
Styling
Class Diagram
All diagram types

Mermaid Class Diagram Syntax & UML Examples

Class diagrams describe types: their fields, their methods, and how they relate. They are a sketch of the object model, not a call trace.

Examples

Copy an example, or open it in the live editor with this diagram already loaded.

Inheritance hierarchy

A base account and two roles. The hollow triangle points at the parent class.

classDiagram
    class Account {
        +String email
        +String role
        +login()
    }
    class Admin {
        +suspend(Account)
    }
    class Member {
        +listOrders()
    }
    Account <|-- Admin
    Account <|-- Member

Composition and cardinality

An order owns its line items. The quoted numbers are the multiplicity on each end of the association.

classDiagram
    class Order {
        +String id
        +int total
        +addItem(sku)
    }
    class Line {
        +int quantity
        +int price
    }
    class Product {
        +String sku
        +String name
    }
    Order "1" *-- "1..*" Line : contains
    Line "*" --> "1" Product : refers to

Interface and implementation

A dashed triangle is realization: the class implements the interface. The stereotype marks the interface.

classDiagram
    class Repository {
        <<interface>>
        +find(id)
        +save(entity)
    }
    class SqlRepository {
        -Connection db
        +find(id)
        +save(entity)
    }
    Repository <|.. SqlRepository

What is a Class Diagram?

A Mermaid class diagram is a UML class diagram written as text. A class block lists attributes and methods. Visibility markers are + public, - private, and # protected. Relationships are arrows: <|-- for inheritance, *-- for composition, o-- for aggregation, and --> for a plain association. Cardinality, when you need it, sits in quotes on the arrow. Use a class diagram to agree on the object model before writing the code, or to explain a module to someone reading it later.

When to use it

Use a class diagram for domain models, service interfaces, and inheritance hierarchies. Use an ER diagram when the boxes are database tables with keys. Use a sequence diagram when you care about the order of method calls rather than the types that own them.

Edit this class diagram in the live editor, read the Mermaid cheat sheet, or compare it with ER Diagram, Sequence Diagram and Flowchart.

Common Use Cases

  • •Software design - Plan class hierarchies and relationships
  • •Database modeling - Map entities to object models
  • •API design - Define data transfer objects and services
  • •Documentation - Communicate system architecture to teams
  • •Code generation - Use diagrams as blueprints for implementation

Syntax Quick Reference

ElementSyntaxDescription
Classclass Name { }Define a class
Attribute+String nameClass property (+ public, - private)
Method+method()Class function
Inheritance<|--Extends relationship
Composition*--Contains relationship
Association-->Uses relationship

Syntax tips

  • •class Name { } declares the box. Attributes and methods each get their own line inside the braces.
  • •+ is public, - is private, # is protected. They are documentation; Mermaid does not enforce them.
  • •<|-- means the class on the right extends the class on the left. The triangle is drawn on the parent.
  • •*-- is composition (the part does not outlive the whole). o-- is aggregation. --> is a looser association.
  • •Put multiplicity in quotes: Order "1" *-- "*" Line. A label after a colon names the relationship.

Common mistakes

Pointing the inheritance arrow at the child class.

Write Parent <|-- Child. The triangle stays attached to Parent.

Modeling tables, primary keys, and foreign keys as classes.

That is an ER diagram. Class diagrams describe objects and behavior; ER diagrams describe rows and keys.

Putting spaces in a class name.

Use a single token such as OrderLine. Class names in this syntax cannot contain spaces.

Export Options

Class diagrams are often pasted into design docs. SVG keeps the text selectable and sharp. Free PNG exports include a watermark; Pro removes it.

Got Mermaid code from ChatGPT? Paste it here to render and export instantly.

Get the Free Mermaid Syntax Cheat Sheet

Free Mermaid Syntax Cheat Sheet

All 21 diagram types in one PDF reference.

Frequently Asked Questions

Ready to create your own class diagram?

Open the editor with this example pre-loaded and start customizing.

Class Diagram templates

Related Diagram Types

All diagram types