5.1 Styling & Integrations

  1. Themes: default, forest, dark.

  2. Inline styles with style.

  3. 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 forest for 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

While 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?

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:

  • NodeID is the ID you used when you defined the node.

  • CSS-Properties are properties like fill (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 Process node 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

Property
What it does

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

βœ”οΈ 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 style lets 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:

  • success style colors the Process step green.

  • warning style colors the Validate step 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 called important with:

    • Red background (#f66),

    • Dark border,

    • White text.

  • class Step1,Step2 important applies that style to both Step1 and Step2.


βœ… When to Use classDef

βœ”οΈ Use classDef when you want consistent styling across multiple nodes. βœ”οΈ Use it for categories β€” for example:

  • warning for error states

  • success for positive outcomes

  • highlight for 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.

  • classDef overrides parts of the theme for a group.

  • style can still tweak individual nodes for unique highlights.


βœ… More Complex Example

Here’s a diagram with multiple classes:

  • success style colors the Process step green.

  • warning style colors the Validate step yellow.


In short:

πŸ“Œ classDef + class = reusable styling for multiple nodes β€” perfect for keeping your Mermaid diagrams clean, consistent, and easy to maintain.


Last updated