Diagrams clarify ideas that paragraphs of text never will. If you write documentation, technical notes, or project plans in markdown, adding a flowchart, sequence diagram, or Gantt chart right inside your text files saves hours of switching between tools. Mermaid is the most popular diagramming language that lives directly in your markdown files, and learning how to use it means your diagrams stay version-controlled, easy to edit, and always in sync with your written content.
What is a Mermaid diagram, and how does it work inside markdown?
Mermaid is a JavaScript-based diagramming tool that uses a simple text-based syntax to render charts and diagrams. Instead of dragging shapes around in a visual editor, you write short, readable code that describes your diagram. When rendered by a compatible platform or preview tool that code becomes a clean visual diagram.
The key advantage is that Mermaid blocks live right inside your markdown files. You wrap the diagram code in a fenced code block with the mermaid language identifier, and any tool that supports Mermaid will render it automatically. There is no need for separate image files, external editors, or design software.
How do you add a Mermaid diagram to a markdown file?
The syntax is straightforward. You create a standard markdown code fence and label it as mermaid instead of a programming language like Python or JavaScript. Here is the basic structure:
Step 1: Open a code fence with three backticks and the word mermaid.
Step 2: Write your diagram definition using Mermaid syntax between the fences.
Step 3: Close the code fence with three backticks.
A simple flowchart example looks like this:
```mermaid
graph TD
A[Start] --> B{Is it working?}
B -->|Yes| C[Great]
B -->|No| D[Fix it]
D --> B
```
When rendered, this produces a flowchart with a decision diamond, two outcome paths, and a loop. The graph TD line tells Mermaid to draw the chart flowing from top to bottom. Changing it to graph LR flips the direction to left to right.
What types of diagrams can you create with Mermaid?
Mermaid supports a wide range of diagram types. The most commonly used ones include:
- Flowcharts for processes, decision trees, and workflows
- Sequence diagrams for showing interactions between actors or systems over time
- Class diagrams for object-oriented design and data models
- State diagrams for modeling state machines and transitions
- Gantt charts for project timelines and scheduling
- Entity-relationship diagrams for database schema design
- Pie charts for proportional data visualization
- Git graphs for visualizing branch and commit history
Each diagram type has its own keyword that starts the definition inside the mermaid code block. For example, sequenceDiagram begins a sequence diagram, while gantt starts a Gantt chart. If you want to dig deeper into sequence diagrams specifically, our sequence diagram reference guide covers the full syntax with examples.
Which tools and platforms render Mermaid diagrams in markdown?
Not every markdown viewer supports Mermaid out of the box. You need a tool or platform that knows how to parse and render mermaid code fences. Here are the most popular options:
- GitHub renders Mermaid diagrams natively in markdown files, issues, and pull requests
- GitLab supports Mermaid rendering in wikis and markdown files
- VS Code with the "Markdown Preview Mermaid Support" extension, you can preview diagrams while editing
- Notion supports Mermaid blocks for inline diagramming
- Obsidian renders Mermaid diagrams in live preview and reading modes
- Hugo, MkDocs, Docusaurus many static site generators have built-in or plugin-based Mermaid support
GitHub's native support has made Mermaid especially popular for open-source documentation. You can write a diagram directly in a README file, and anyone viewing it on GitHub will see the rendered chart without installing anything.
What does a practical Mermaid diagram look like in a real document?
Let's walk through a common real-world example: documenting an API authentication flow. Here is how you might write it inside a markdown documentation file:
```mermaid
sequenceDiagram
participant Client
participant Server
participant Database
Client->>Server: POST /login with credentials
Server->>Database: Query user record
Database-->>Server: Return user data
Server-->>Client: Return JWT token
Client->>Server: GET /api/data with JWT
Server-->>Client: Return requested data
```
This renders as a clean sequence diagram showing the communication between a client, server, and database. The arrows indicate request direction, and the syntax reads almost like plain English.
For project management documents, a Gantt chart works well inside markdown too. If you are planning sprints or tracking deliverables, our Gantt chart syntax guide walks through the full structure with scheduling examples. You can also find a broader overview of how these diagrams fit together in our Mermaid diagram syntax walkthrough.
What are the most common mistakes people make with Mermaid in markdown?
Mermaid syntax is forgiving in some areas but strict in others. These are the errors that trip people up most often:
- Using the wrong language identifier. The code fence must say
mermaidexactly. WritingMermaidorflowchartas the language label will not work in most renderers. - Indentation issues. Mermaid cares about indentation in certain contexts, especially inside Gantt charts and subgraph blocks. Mixing tabs and spaces can cause silent rendering failures.
- Special characters in node labels. If your label text contains parentheses, brackets, or quotes, you need to wrap the text carefully in the correct bracket style. For example,
A["Label with (parentheses)"]works, butA(Label with (parentheses))will break. - Forgetting arrow syntax for sequence diagrams. Solid arrows use
-->> or ->> while dashed arrows use-->>. Getting these wrong changes or breaks the message lines. - Not checking the rendered output. Always preview your diagram. A diagram that looks correct in text form might render with overlapping nodes or missing connections.
How can you make your Mermaid diagrams easier to read and maintain?
These habits make a real difference when you are working with Mermaid diagrams in documentation that multiple people edit:
- Use descriptive node IDs and labels. Write
A[User submits form]instead ofA[Step 1]. When someone reads the raw markdown months later, they should understand the diagram without seeing the rendered version. - Keep diagrams focused on one idea. If your flowchart has more than 15-20 nodes, consider splitting it into separate diagrams. Overcrowded diagrams become harder to read than the text they replace.
- Add comments above the code block. A sentence or two in markdown above the diagram explaining what it shows and why helps context for readers who cannot render Mermaid (like people reading raw files in email or plain text editors).
- Use subgraphs for grouping. The
subgraphkeyword lets you visually cluster related nodes together, which is useful for showing systems, modules, or phases. - Version control your diagrams. Since Mermaid lives in text files, you get full git history for free. Write meaningful commit messages when you change a diagram so reviewers can understand what changed.
How do you debug a Mermaid diagram that will not render?
When a diagram fails silently, try these steps:
- Paste the code into the Mermaid Live Editor. This tool gives you real-time rendering and will show syntax errors with specific line numbers.
- Check for unmatched brackets and parentheses. Every opening bracket needs a closing one. A single missing character can prevent the entire diagram from rendering.
- Simplify and rebuild. Remove half the diagram and see if the remainder renders. Add sections back one at a time until you find the line causing the problem.
- Check your platform's Mermaid version. Newer diagram types like
mindmaportimelinerequire recent Mermaid versions. GitHub and other platforms update periodically, but they may lag behind the latest release.
Quick checklist: creating your first Mermaid diagram in markdown
- Write a fenced code block with
mermaidas the language identifier - Start with a diagram type keyword:
graph,sequenceDiagram,gantt, etc. - Define your nodes and connections using the correct arrow syntax
- Use descriptive labels so the raw text is readable too
- Preview the rendered output before committing
- Test in the Mermaid Live Editor if anything breaks
- Check that your target platform (GitHub, VS Code, Obsidian) supports the diagram type you are using
- Keep each diagram focused split complex flows into multiple smaller diagrams
Start with a simple flowchart for your next documentation update. Once you see how little effort it takes to maintain diagrams as text, you will find yourself reaching for Mermaid every time a process or system interaction needs visual explanation.
Mermaid Gantt Chart Syntax Explained: Complete Guide to Creating Gantt Diagrams
Mermaid Sequence Diagram Syntax Reference Guide
Mermaid Diagram Syntax Cheat Sheet: Complete Guide & Quick Reference
Best Diagram Code Editors for Software Architects in 2025
How to Write Diagram Codes in Markdown Syntax
Diagram Code Editor Comparison for System Engineers