A class diagram maps classes as boxes with three compartments β€” name, attributes, methods β€” connected by typed arrows. In Mermaid you write class Vehicle { +String name +start() void } and connect with Vehicle <|-- Car for inheritance, *-- for composition, o-- for aggregation. Visibility markers (+ public, - private, # protected) prefix each member. This maker renders the MIT-licensed Mermaid 11 library in your browser and exports clean SVG or PNG.
Loading renderer…
Ad

Class Diagram Syntax Reference

SyntaxWhat it draws
classDiagramRequired first line
class Order { ... }Box with member compartments
+String emailPublic attribute (member syntax: visibility, name, optional type)
-hashPassword(pwd)Private method
#rowCount() intProtected method with return type
<<interface>>Stereotype label line inside the class body
<<abstract>> / <<enumeration>>Other common stereotypes
A <|-- BInheritance β€” B extends A (triangle at A)
A <|.. BRealization β€” B implements interface A
Whole *-- PartComposition β€” part dies with whole
Whole o-- PartAggregation β€” part can outlive whole
A -- B / A --> BAssociation / directed association
A ..> B : usesDependency β€” A temporarily uses B
A "1" --> "0..*" BMultiplicity labels on the ends
note for Order "text"A note attached to one class

The direction you write a relationship matters: in Vehicle <|-- Car the triangle points at Vehicle, the parent. Mermaid is picky about spaces inside braces β€” one member per line, no blank lines inside the block.

Which Relationship Arrow Do I Need?

Most mistakes in class diagrams are the wrong arrow, not the wrong syntax. Ask the question in the second column, and if the answer is yes, that's your arrow.

RelationshipAsk yourselfMermaidIn code it usually looks like
InheritanceIs B a kind of A?A <|-- Bclass B extends A
RealizationDoes B promise A's methods without inheriting code?A <|.. Bclass B implements A
CompositionDoes Part die when Whole is deleted?Whole *-- PartWhole creates Part in its constructor and never shares it
AggregationDoes Whole hold Parts that live on without it?Whole o-- PartA list of objects passed in from outside
AssociationDoes A keep a long-lived reference to B?A --> BA field of type B
DependencyDoes A only use B briefly?A ..> BB as a method parameter or local variable

When composition and aggregation both seem right, pick association. It's never wrong, just less specific, and reviewers argue far less about a plain arrow than a diamond.

Member Modifiers and Generics

WriteMeansExample
+ - # ~Public, private, protected, package/internal~int retryCount
* after a methodAbstract method, shown in italics+area() double*
$ after a memberStatic, shown underlined+create() Order$
~Type~Generic type, drawn with angle bracketsclass Box~T~ or +List~String~ tags
direction LRLay the diagram out left to right (TB is the default)Second line of the default diagram
"1" --> "0..*"Multiplicity. Common values: 1, 0..1, 1..*, *, n, 0..n, 1..nCustomer "1" --> "0..*" Order

Generics use tildes because angle brackets clash with Mermaid's own syntax. Nested generics like List~List~int~~ work, but a generic with a comma inside, such as Map~K, V~, isn't supported yet according to the Mermaid docs.

Exporting and Keeping Your Diagram

The two download buttons give you different files, and it's worth knowing which to use before you paste one into a document.

SVGPNG
File nameclass-diagram.svgclass-diagram.png
ResolutionVector, sharp at any zoom2Γ— the on-screen diagram size
BackgroundNone of its ownSolid dark (#0f1117), matching the preview
Best forWikis, READMEs, design docs with a dark themeSlides, chat messages, tickets
Watch out forLight text from the dark theme can fade on a white pageText can't be edited afterwards

The editor doesn't save anything between visits. Reload the page and it goes back to the payment example. That's deliberate, since your code never leaves the browser, but it means the Copy code button is the real save button. Paste the code into your repo next to the classes it describes, and the diagram can be re-rendered whenever the design changes. That's the point of diagrams as code: the source is a few lines of text that diff cleanly in a pull request, unlike a binary image.

The renderer itself, Mermaid 11, loads from a public CDN the first time you open the page. If that request is blocked on a locked-down network, the status line says so. The same syntax works in GitHub and GitLab Markdown inside a ```mermaid code block, so a diagram you build here can live directly in a README. For data models, the ER diagram maker is the better fit, and the Mermaid diagram suite covers every other diagram type.

How the Class Diagram Maker Works

Class diagrams are the static view of an object-oriented design: what types exist, what they hold, what they can do, and how they relate. They don't show control flow (that's a sequence diagram) or stored data (that's an ER diagram). They're the diagram you sketch before writing the first class, and the one you keep beside the code as it grows.

How to use it

The editor on the left renders about 400 ms after you stop typing; the status line flags parse errors. Export SVG for documentation or PNG for a slide. Both are produced locally β€” your code never leaves the browser.

A worked example

The default diagram models a payment flow. Count before you draw: 6 classes, 5 relationships. PaymentMethod is an interface (the <<interface>> stereotype); CreditCard and BankAccount each realize it with <|.., so two dotted lines with hollow triangles point at that box. A third dotted line arrives there too: Order ..> PaymentMethod : pays with is a dependency, drawn with an open arrowhead instead of a triangle. Same line style, different meaning, and the arrowhead is how you tell them apart. Order *-- LineItem is composition β€” delete the order and its line items go with it. Customer "1" --> "0..*" Order says one customer can hold any number of orders, including none.

Six classes and five relationships fit on one screen; the same drawing in a canvas tool is twenty drag operations and an alignment pass. That's the usual experience with diagrams-as-code β€” structure is cheap to revise, so you revise it more often.

Frequently Asked Questions

How do I make a UML class diagram for free?

Open a file with classDiagram, declare classes with class Name { ... }, list attributes and methods inside the braces (visibility marker first: + public, - private, # protected), then connect classes with relationship arrows like Vehicle <|-- Car for inheritance. This maker renders live in your browser and exports SVG β€” no account, no watermark.

What do the +, -, and # symbols mean in a class diagram?

They are UML visibility markers. + means public (any class can call it), - means private (only the class itself), and # means protected (the class and its subclasses). ~ marks package-private. You write them at the start of each member line, as in +String name or -balance() float, and Mermaid shows them as a gutter character next to each member.

How do I show inheritance versus composition?

Inheritance is ClassA <|-- ClassB (a hollow triangle pointing at the parent). Composition, where the part dies with the whole, is Whole *-- Part. Aggregation, where parts survive, is Whole o-- Part. Plain association is --, dependency (uses-temporarily) is ..>, and realization of an interface is <|.. Implementer. The shape at the parent end and the line style carry the meaning.

How do I label a relationship with cardinality?

Put the multiplicity on the relationship line with quotes: Customer "1" --> "*" Order : places. Read it as one customer places zero-or-more orders. Common values are 1, 0..1, *, and 1..*, and you can put a label after a colon to name the relationship in plain English.

Can I mark a class as abstract or an interface?

Yes. Inside the class body, write <<abstract>> or <<interface>> on its own line and Mermaid adds the UML stereotype label. <<enumeration>> works the same way for enums, where you then list the allowed values as members. The stereotype appears as a label above the class name. To mark a single method as abstract, end it with an asterisk, as in draw()*, and Mermaid sets it in italics, following the UML convention.

Is my class diagram saved?

No. Nothing is stored in your browser or sent to a server, so reloading the page brings back the example diagram. Click Copy code and keep the Mermaid source somewhere, ideally in the same repository as the code it describes, then paste it back in to edit later.

How do I write generics like List<String> in a class diagram?

Wrap the type in tildes instead of angle brackets: List~String~. It works on class names too, as in class Box~T~, and Mermaid draws it with angle brackets. Nested generics are supported, but a generic with a comma, such as Map~K, V~, isn't yet.

Why does my exported PNG have a dark background?

The PNG export paints the same dark background as the preview, then draws the diagram at twice its on-screen size, so it stays readable on slides. If you need a diagram for a light document, use the SVG, which has no background of its own, and check the text contrast after pasting.