5.1 Styling & Integrations
Themes: default, forest, dark.
Inline styles with
style.Class definitions for reusable styles.
1. Themes: Default, Forest, Dark
By default, Mermaid renders your diagrams with a clean, simple style β but sometimes youβll want to change how your diagrams look to better match your docs, slide decks, or website. Mermaid makes this easy with themes.
A theme changes your diagramβs:
Colors (background, nodes, lines)
Fonts and text color
Link styles and shapes
β
Why Use Themes?
Themes help you:
Match your diagrams to your brand or documentation style.
Make diagrams easier to read in light or dark mode.
Quickly switch between looks without rewriting your diagram code.
β
Common Mermaid Themes
Mermaid comes with a few popular built-in themes. The three youβll see most are:
π¨ 1οΈβ£ Default
The default theme is the clean, classic look you see when you first run Mermaid.
It uses a white background, light gray nodes, and black text.
Good for simple, clean documentation β especially on light pages.
π² 2οΈβ£ Forest
The forest theme gives your diagram a green accent palette β nodes, arrows, and connectors use greens and earthy tones.
This theme is great for slides, wikis, or docs that need a slightly softer, organic feel.
Many people choose
forestfor architecture diagrams or process flows that need a bit more visual style.
π 3οΈβ£ Dark
The dark theme flips your diagram for dark-mode environments.
It uses a dark background with lighter text and shapes.
Perfect if your site, slides, or notes use a dark theme β it avoids harsh contrast and keeps your diagrams easy on the eyes.
β
How to Set a Theme
When rendering your diagram, you can set a theme in your Mermaid configuration.
Example with inline config:
Or for dark mode:
β
Tips for Using Themes
βοΈ Pick a theme that matches your docs β for example, if your GitBook is dark, use dark.
βοΈ Try different themes in the Mermaid Live Editor to see which fits your style best.
βοΈ Use custom themes if you want to go further β Mermaid lets you tweak colors and styles using your own CSS variables too!
In short:
π Mermaidβs built-in themes β default, forest, and dark β help you keep your diagrams readable, professional, and consistent with your site or slides β without changing your diagram code at all.
2. Inline Styles with style
styleWhile Mermaidβs themes let you quickly change your diagramβs overall look, sometimes you need to highlight a single node, color a specific shape, or adjust one arrow without changing the whole diagram.
Thatβs where inline styles come in β using the style keyword.
β
What is style in Mermaid?
style in Mermaid?The style keyword lets you apply CSS-like custom styling directly to a specific node inside your diagram.
This is handy when you want to:
Highlight a critical step (like an error or warning).
Make a key node stand out.
Add emphasis without editing the global theme.
β
Basic Syntax
To style a node, use:
NodeIDis the ID you used when you defined the node.CSS-Propertiesare properties likefill(background),stroke(border),color(text),stroke-width(border thickness), etc.
β
Example
Hereβs a simple flowchart with an inline style applied:
What this does:
The
Processnode is given:fill:#f96β an orange background.stroke:#333β a dark gray border.stroke-width:2pxβ a thicker border.color:#fffβ white text inside.
β
Styling Multiple Nodes
You can repeat style for each node you want to customize:
β
Common Properties
fill
Background color of the node
stroke
Border color
stroke-width
Thickness of the border
color
Text color
stroke-dasharray
Makes borders dashed instead of solid
opacity
Makes the node partially transparent
β
When to Use Inline style
styleβοΈ Use it for one-off highlights β for example, showing a failure state in red.
βοΈ Combine it with your overall theme β style overrides the theme for that node only.
βοΈ Keep it minimal β too many custom styles can make your diagram cluttered.
In short:
π Inline
stylelets you override the look of one node at a time β perfect for quick highlights and emphasis without changing your whole diagram.
β
More Complex Example
Hereβs a diagram with multiple classes:
successstyle colors theProcessstep green.warningstyle colors theValidatestep yellow.
In short:
π
classDef+class= reusable styling for multiple nodes β perfect for keeping your Mermaid diagrams clean, consistent, and easy to maintain.
3. Class Definitions for Reusable Styles
Inline styles with style are great for quick highlights β but what if you want to apply the same style to multiple nodes without repeating the same CSS code every time?
Mermaid solves this with class definitions β using classDef and class.
β
What Are Class Definitions?
A class definition lets you:
Create a named style rule once (like a CSS class).
Apply that rule to as many nodes as you want.
Keep your diagram cleaner and easier to update.
If you change the style in the class definition, all nodes using that class update automatically β just like CSS on a website.
β
Basic Syntax
Mermaid uses two parts:
1οΈβ£ classDef β define the style
2οΈβ£ class β apply the style to specific nodes
Example:
β
How It Works
Example Flowchart:
What this does:
classDef important ...defines a class calledimportantwith:Red background (
#f66),Dark border,
White text.
class Step1,Step2 importantapplies that style to bothStep1andStep2.
β
When to Use classDef
classDefβοΈ Use classDef when you want consistent styling across multiple nodes.
βοΈ Use it for categories β for example:
warningfor error statessuccessfor positive outcomeshighlightfor focus steps
βοΈ It keeps your diagrams tidy and maintainable, especially if you reuse the same style in many places.
β
Combining with Themes
You can mix classDef with themes and inline style.
Themes set the base look.
classDefoverrides parts of the theme for a group.stylecan still tweak individual nodes for unique highlights.
β
More Complex Example
Hereβs a diagram with multiple classes:
successstyle colors theProcessstep green.warningstyle colors theValidatestep yellow.
In short:
π
classDef+class= reusable styling for multiple nodes β perfect for keeping your Mermaid diagrams clean, consistent, and easy to maintain.
Last updated