KevsRobots Learning Platform
By Kevin McAleer, 5 Minutes
In the Flowcharts in Depth lesson we coloured individual nodes with style and classDef. That’s great for highlighting one or two things. But what if you want to change the look of the whole diagram, to match your website or your project’s colours? That’s what themes are for.
Mermaid comes with five themes:
| Theme | Looks like |
|---|---|
default |
The standard look you’ve seen in most of this course |
neutral |
Black, white and grey: great for printing |
dark |
Light lines on a dark background |
forest |
Shades of green |
base |
A plain theme designed to be customised with your own colours |
The modern way to set a theme is a small config block at the very top of the diagram, between two lines of three dashes:
---
config:
theme: forest
---
flowchart LR
sensor[Read sensor] --> decide{Too close?}
decide -->|Yes| stop[Stop]
decide -->|No| go[Drive]
Here’s what it draws:
---
config:
theme: forest
---
flowchart LR
sensor[Read sensor] --> decide{Too close?}
decide -->|Yes| stop[Stop]
decide -->|No| go[Drive]
That block is written in YAML, the same format used at the top of Jekyll pages and Obsidian notes. Indentation matters: theme must be indented under config.
You can also add a title in the same block, and it’ll be shown above the diagram:
---
title: Obstacle check
config:
theme: neutral
---
flowchart LR
sensor[Read sensor] --> decide{Too close?}
---
title: Obstacle check
config:
theme: neutral
---
flowchart LR
sensor[Read sensor] --> decide{Too close?}
You’ll see lots of examples online that use an init directive instead, a special comment on the first line:
%%{init: {"theme": "dark"}}%%
sequenceDiagram
Pico->>Sensor: Read
Sensor-->>Pico: 42
%%{init: {"theme": "dark"}}%%
sequenceDiagram
Pico->>Sensor: Read
Sensor-->>Pico: 42
It does the same job. Directives still work, but the config block is the newer, recommended way, and it’s easier to read. If an app you’re using is running an older version of Mermaid and ignores the config block, try a directive instead.
To use your own colours, pick the base theme and set some theme variables:
---
config:
theme: base
themeVariables:
primaryColor: "#ffd166"
primaryTextColor: "#1d1d1d"
primaryBorderColor: "#ef476f"
lineColor: "#118ab2"
fontFamily: "Courier New, monospace"
---
flowchart LR
pico[Pico W] --> driver[Motor driver] --> wheels[Wheels]
pico --> leds[LED eyes]
Here’s what it draws:
---
config:
theme: base
themeVariables:
primaryColor: "#ffd166"
primaryTextColor: "#1d1d1d"
primaryBorderColor: "#ef476f"
lineColor: "#118ab2"
fontFamily: "Courier New, monospace"
---
flowchart LR
pico[Pico W] --> driver[Motor driver] --> wheels[Wheels]
pico --> leds[LED eyes]
The most useful variables are:
primaryColor - the fill colour of nodesprimaryTextColor - the text inside nodesprimaryBorderColor - the border of nodeslineColor - arrows and linesfontFamily - the fontMermaid works out lots of other colours from these, such as shading and contrasting text.
themeVariables only work properly with the
basetheme. The other themes set their own colours and mostly ignore yours. If your colours aren’t showing, check you’ve settheme: base.
Always put colour codes in quotes, like "#ffd166". In YAML, # starts a comment, so without quotes your colour disappears!
Each diagram type has its own settings too. For example, you can make flowchart lines straight and angular instead of curved:
---
config:
flowchart:
curve: linear
---
flowchart TD
a[Start] --> b{Choose}
b --> c[Left]
b --> d[Right]
---
config:
flowchart:
curve: linear
---
flowchart TD
a[Start] --> b{Choose}
b --> c[Left]
b --> d[Right]
Newer versions of Mermaid (version 11 and later) also add a look: handDrawn setting that makes diagrams look like sketches. It’s fun, but not every app supports it yet, so we won’t use it on this site.
base theme and themeVariables to make a diagram in your favourite colours.theme: base when setting themeVariablesWhy: The other themes override most of the variables with their own colours
primaryColor: "#ffd166"Why: # starts a YAML comment, so everything after it is ignored
--- line is the very first line of the diagram, with nothing before itQuick quiz 1 of 3
Which theme should you use with your own themeVariables?
base is designed to be customised; the other themes mostly ignore your variables.Quick quiz 2 of 3
Why must colour codes like #ffd166 be in quotes in the config block?
#ffd166 as a comment and the setting is empty.Quick quiz 3 of 3
Where must the config block go?
--- lines has to be the first thing in the diagram.
You can use the arrows ← → on your keyboard to navigate between lessons.
Comments