2.2 Common Flowchart Elements
Subgraphs (groups).
Conditions and decision nodes.
Styling nodes with
styleorclassDef.
1. Subgraphs (Groups)
When your flowchart grows beyond a few steps, it can become harder to read at a glance. Thatβs where subgraphs, also called groups, come in. A subgraph lets you visually group related nodes together, helping readers understand which steps belong to the same part of a process.
β
What is a Subgraph?
A subgraph is a container that wraps multiple nodes inside a labeled box. It doesnβt change how nodes connect β it just groups them for clarity.
Use subgraphs when:
You want to show phases in a process (e.g., Input, Processing, Output).
You have modules or components that belong together.
You need to separate responsibilities in a big flowchart.
β
How to Write a Subgraph
Mermaid uses a simple subgraph keyword:
subgraph SubgraphName [Optional Label]
Node1
Node2
endsubgraphstarts the group.Everything indented under it is inside the subgraph.
endcloses the group.The square brackets
[ ]add a visible label to the subgraph box.
β
Example
Hereβs a flowchart with two subgraphs:
What this does:
The
Backend Processsubgraph groups Validation, Process Data, and Save to DB.The
Frontend Processsubgraph shows User Form and how it links to Validation.This makes it clear which parts of the flow happen in the frontend vs. backend.
β
Tips for Using Subgraphs
Use clear, short labels so your groups donβt clutter the diagram.
Keep related nodes together inside the group block.
You can nest subgraphs inside bigger diagrams β but avoid going too deep. Too many levels can make things harder to read.
In short:
π Subgraphs are your tool for grouping related steps. They keep big diagrams tidy, organized, and easier to explain β especially when your flow has distinct parts that belong together.
Hereβs a clear, complete draft for βConditions and Decision Nodesβ β perfect for Mermaid for Beginners *Chapter 2.2: Common Flowchart Elements:
2. Conditions and Decision Nodes
In any real process or system, there are moments where you need to make a choice: Is this valid?, Did the user approve?, Should we retry? In a flowchart, these choices are shown using conditions and decision nodes.
β
What is a Decision Node?
A decision node represents a point where your process can branch based on a condition.
Visually, decision nodes are usually shown as diamonds in flowcharts.
In Mermaid, you create a diamond shape using curly braces { } around the label.
β
How to Write a Decision Node
Hereβs the basic syntax:
How this works:
B{Is it valid?}defines a decision node with the question Is it valid?.The curly braces
{ }tell Mermaid to draw this as a diamond.From the decision node, the flow branches using labeled arrows:
Yesgoes toProceed,Nogoes toStop.
β
Adding Conditions with Labels
The condition itself is shown as a label on the arrow:
--|Yes|-->means: If Yes, go here.This makes your diagram easy to read β viewers immediately see how the process splits.
β
Multiple Conditions
Decision nodes donβt have to be limited to two outcomes. You can branch into multiple paths if needed:
β
Combining with Loops
Decision nodes often connect naturally to loops. For example, you can send an invalid option back to an earlier step:
If the input is valid, move forward.
If not, loop back to Start for a retry.
β
Tips for Clear Decisions
Keep your condition question short and clear β like Approved? or Retry?
Always label the outgoing arrows so readers see what each path means.
Use
{ }only for decision nodes β it keeps the diagram consistent and easy to read.
In short:
π Decision nodes help your flowchart show choices and conditions. π Use
{ }for diamonds and labeled arrows for clear branches. This makes your diagrams easy to follow and closer to how real processes work.
3. Styling Nodes with style or classDef
style or classDefWhen you create flowcharts with Mermaid, your diagrams will look clear and clean by default β but sometimes youβll want to highlight important steps, group similar nodes, or make certain parts stand out visually.
Thatβs where Mermaidβs styling options come in: the style and classDef keywords.
β
Why Style Nodes?
Styling nodes helps you:
Highlight critical steps (like errors or approvals).
Show related nodes with the same color or border.
Make diagrams more readable and professional.
Mermaid offers two main ways to customize nodes:
Inline
styleβ style a single node directly.Reusable
classDefβ define a style once and apply it to multiple nodes.
β
Using style for a Single Node
style for a Single NodeThe style keyword lets you apply CSS-like styles to one node at a time.
Syntax:
Example:
What this does:
Fills node
Bwith a light purple (#f9f).Gives it a dark border (
#333).Sets the border width to
2px.Changes the text color inside to black (
#000).
This is perfect when you just need to emphasize one specific step.
β
Using classDef for Reusable Styles
classDef for Reusable StylesWhen you want many nodes to share the same look, classDef is better.
It works in two parts:
classDefdefines the style once.classapplies it to one or more nodes.
Example:
What this does:
classDef importantcreates a style calledimportantwith an orange fill, dark border, and black text.class B,C importantapplies theimportantstyle to nodesBandC.
If you have many steps that need the same highlight, classDef saves time and keeps your diagram consistent.
β
Common Styling Options
Here are a few CSS-like properties you can use:
fill
Changes the nodeβs background color
stroke
Changes the border color
stroke-width
Sets how thick the border is
color
Changes the text color inside the node
stroke-dasharray
Makes borders dashed instead of solid
β
Tips for Styling
Keep your colors accessible: use good contrast for readability.
Donβt overdo styling β a few colors are usually enough.
Combine styling with subgraphs or labels to make complex diagrams easy to follow.
In short:
π Use
stylefor quick, one-off highlights. π UseclassDeffor clean, reusable styles across multiple nodes. This keeps your Mermaid diagrams clear, professional, and easy to update.
Last updated