Mermaid JS coloring is a powerful feature that allows developers and designers to transform standard flowcharts and diagrams into visually engaging representations of data. By applying custom color schemes, you can align diagrams with corporate branding, improve accessibility, and guide the user’s eye through complex workflows. This capability moves Mermaid beyond a simple diagramming tool, positioning it as a dynamic visualization asset for modern web applications.
Understanding Theming vs. Inline Styling
The foundation of Mermaid JS coloring lies in understanding the difference between global theming and per-element styling. Users can either apply a broad theme to change the overall look of a diagram or target specific nodes and links with custom colors. This flexibility ensures that whether you are creating a high-level overview or a detailed technical flowchart, you have the control needed to achieve the exact visual outcome you desire.
Configuration Through The Theme Variables
To implement Mermaid JS coloring effectively, you must modify the theme variables within your configuration. These variables act as a palette, defining the default colors for backgrounds, text, primary and secondary nodes, and error states. By adjusting these values in your `mermaid.initialize()` script or in your site’s CSS, you can establish a consistent design language that persists across all diagrams on your project.

| Variable | Purpose | Example Value |
|---|---|---|
| primaryColor | Base for main nodes | #3498db |
| primaryTextColor | Font color on primary nodes | #ffffff |
| lineColor | Connector lines | #95a5a6 |
| tertiaryColor | Alternating node shades | #ecf0f1 |
Class-Based Overrides For Precision
While theme variables provide a solid baseline, Mermaid JS coloring also supports class-based overrides for granular control. By adding specific CSS classes to your nodes directly within the Mermaid syntax, you can assign unique colors to critical paths or exceptions. This method is invaluable for highlighting error states, emphasizing decision points, or separating distinct phases of a process without rewriting the entire theme.
Syntax Highlighting Integration
For developers documenting code architecture or sequence diagrams, Mermaid JS coloring intersects with syntax highlighting. You can define color schemes for different parts of your code blocks, such as keywords, strings, and comments, ensuring that the technical accuracy of the diagram is matched by its readability. This integration is particularly useful for engineering teams looking to maintain a unified aesthetic between their source code and their documentation visuals.
Practical Implementation Strategies
When rolling out Mermaid JS coloring across a large project, it is wise to start with a style guide. Define a set of rules for which colors represent which statuses or departments, and stick to them rigorously. Consistency is key to preventing visual clutter; too many disparate colors can make a diagram feel chaotic rather than insightful. Use color to reduce complexity, not to add to it.

Accessibility Considerations
A common pitfall in diagram design is neglecting accessibility. Relying solely on color to convey information can alienate users with color vision deficiencies. Effective Mermaid JS coloring always incorporates secondary cues such as patterns, shapes, or text labels to communicate meaning. Always test your diagrams against contrast checkers to ensure that text remains legible and that the color contrast meets WCAG standards for usability.