Flowcharts: classDef styling

Flowcharts: classDef styling

puml.online

简介 / Introduction

Once a flowchart has more than a couple of branches, the “happy path” and the “failure path” start to look identical, and readers have to read every label to tell them apart. Coloring the nodes is the fastest fix. Mermaid offers a declarative classDef syntax; PlantUML combines a <style> block with skinparam. The result is the same picture, but the syntax is not. This article renders the same “validate parameters” flow in both engines.

PlantUML 版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@startuml
<style>
activity {
BackgroundColor #F5F5F5
BorderColor #333333
FontColor #222222
ArrowColor #333333
}
</style>

start
:校验参数;
if (参数合法?) then (是)
:写入缓存;<<#DDF3E0>>
:返回结果;<<#DDF3E0>>
else (否)
:记录错误;<<#FBE0E0>>
endif
stop
@enduml

Key syntax notes:

  • <style> block — a declarative style sheet. activity is the selector; element, node, class and others work too.
  • BackgroundColor / BorderColor / FontColor / ArrowColor — the property names the style sheet accepts, with hex color values.
  • ;<<#DDF3E0>> — append <<#RRGGBB>> at the end of an action statement to color that one node (it must come after the ;; putting it before triggers a “syntax is deprecated” warning).
  • To color a whole element class at once, skinparam activityBackgroundColor #F5F5F5 also works, but it applies to every activity in the diagram.

PlantUML has no “define a named style, then attach it” mechanism like classDef. Styling goes either through the global <style> block or through global skinparam values; per-node overrides are done with the <<#COLOR>> suffix.

Mermaid 版

1
2
3
4
5
6
7
8
9
flowchart TD
V[校验参数] --> Q{参数合法?}
Q -->|是| C[写入缓存]
Q -->|否| E[记录错误]
C --> R[返回结果]
classDef ok fill:#DDF3E0,stroke:#333333,stroke-width:1px,color:#222222
classDef err fill:#FBE0E0,stroke:#333333,stroke-width:1px,color:#222222
class C,R ok
class E err

Key syntax notes:

  • classDef <name> <properties> — defines a named style; properties are space-separated and values are comma-separated.
  • fill / stroke — background color and border color.
  • color — text color; stroke-width — border thickness.
  • class <node list> <style name> — attaches a style to nodes. class C,R ok gives both C and R the ok style.
  • style C fill:#DDF3E0 colors a single node directly, but classDef is less repetitive when the same style is reused.

对比 / Side-by-side

Meaning PlantUML Mermaid
Declare a style sheet <style> activity { BackgroundColor #DDF3E0 } </style> classDef ok fill:#DDF3E0
Apply a style to elements none; use the <<#DDF3E0>> suffix per node class C,R ok
Element type selector activity / element / node / class none; a classDef applies to any node
Global uniform palette skinparam activityBackgroundColor #F5F5F5 classDef plus a batched class
Color property names BackgroundColor / BorderColor / FontColor fill / stroke / color
Stroke thickness extra properties inside the <style> block stroke-width:1px inline in classDef

小结 / Wrap-up

Mermaid’s classDef is a “name it once, reference it everywhere” declarative model, which suits defining several semantic palettes and reusing them across a diagram. PlantUML goes with “global style sheet plus per-node overrides”: the <style> block sets the baseline and the <<#RRGGBB>> suffix highlights individual nodes. Both produce identical SVG output in the end, so the choice comes down to which mental model you prefer — Mermaid feels like CSS, PlantUML feels like a config file.

  • Title: Flowcharts: classDef styling
  • Author: puml.online
  • Created at : 2026-11-10 01:00:00
  • Updated at : 2026-11-26 01:07:01
  • Link: https://puml.online/blog/flowchart-styled-classes-en/
  • License: This work is licensed under CC BY-NC-SA 4.0.