Diagrams with Mermaid

Reviewed & published by Brayan K

By the end of this lesson you'll turn a plain fenced code block into a real diagram — flowcharts, sequence diagrams, Gantt charts, and pie charts — all written as text that GitHub and many docs tools render automatically.

Part of the free Markdown course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

What You'll Learn

💡 Real-World Analogy

Mermaid is like describing a map over the phone. Instead of drawing boxes and arrows by hand, you say "Start box, then an arrow to a decision diamond, then a Yes arrow to the dashboard." Mermaid takes that spoken-style description in text and draws the picture for you. Because it's just text, your diagrams live inside the Markdown, get tracked in version control, and update with a simple edit — no graphics editor required.

1. The Mermaid Code Fence

A Mermaid diagram lives in a fenced code block whose info string is mermaid. Inside the fence you write the diagram in Mermaid's text syntax. A renderer that understands Mermaid (GitHub, GitLab, and many documentation tools) replaces the code block with an actual drawing; a plain Markdown viewer just shows the source text.

\`\`\`mermaid
graph TD
  A[Start] --> B{Logged in?}
  B -->|Yes| C[Show dashboard]
  B -->|No| D[Show login page]
\`\`\`

2. Flowcharts: Nodes and Edges

A flowchart begins with graph and a direction: TD (top-down) or LR (left-to-right). You then declare nodes — an id plus label text — and connect them with edges written as -->. The node id (like A) is internal; the bracket style sets the shape: A[Box] is a rectangle, A(Round) is rounded, and A{Decision} is a diamond. Add text between pipes to label an arrow: A -->|Yes| B.

Node shapes at a glance

🎯 Your Turn #1: a two-box flowchart

Fill in the blanks so the program prints a valid two-node flowchart, then run it. Pick TD or LR for the direction.

// 🎯 YOUR TURN — design a tiny flowchart.
// Fill in each ___ then press "Try it Yourself" to print the source.

const direction = "___";   // 👉 TD for top-down, or LR for left-to-right
const firstNode = "___";   // 👉 a label for the start box, e.g. Start
const nextNode  = "___";   // 👉 a label for the next box, e.g. Finish

console.log("graph " + direction);
console.log("  A[" + firstNode + "] --> B[" + nextNode + "]");

// ✅ Expected printed source (example):
//    graph TD
//      A[Start] --> B[Finish]
//
// Wrap it in a mermaid fence on GitHub to see two boxes and an arrow.

3. Sequence Diagrams

A sequence diagram shows messages passed between participants over time. Start with sequenceDiagram, declare each participant, then write messages with arrows: -->> for a solid call and -->> with a dashed style for a reply. Time flows downward, so the order of your lines is the order of the conversation.

\`\`\`mermaid
sequenceDiagram
  participant U as User
  participant S as Server
  U->>S: GET /home
  S-->>U: 200 OK (HTML)
\`\`\`

User ——— GET /home ———▶ Server

User ◀- - 200 OK (HTML) - - Server

Time runs top to bottom; each line is one message.

Here's a worked example that prints real Mermaid source for a flowchart. Run it, read each printed line, then paste a real mermaid fence into a GitHub README to watch the boxes and arrows appear.

## Open a mermaid code fence
(three backticks)mermaid

## A top-down flowchart
graph TD
  A[Start] --> B{Ready?}
  B -->|Yes| C[Go]
  B -->|No| A

## Close the fence
(three backticks)

<!-- ✅ Expected result, rendered:
     h2 -> text: Open a mermaid code fence
     h2 -> count: 3
     p  -> count: 3
-->

🎯 Your Turn #2: a decision with branches

Fill in the blanks so the program prints a flowchart with a diamond decision and two labelled arrows. Remember: {braces} make the diamond, |Yes| and |No| label the arrows.

// 🎯 YOUR TURN — add a DECISION with two labelled branches.
// A diamond uses {curly braces}; label each arrow between |pipes|.

const question = "___";   // 👉 a yes/no question, e.g. Ready?
const yesPath  = "___";   // 👉 where Yes leads, e.g. Continue
const noPath   = "___";   // 👉 where No leads, e.g. Stop

console.log("graph TD");
console.log("  A{" + question + "}");
console.log("  A -->|Yes| B[" + yesPath + "]");
console.log("  A -->|No| C[" + noPath + "]");

// ✅ Expected printed source (example):
//    graph TD
//      A{Ready?}
//      A -->|Yes| B[Continue]
//      A -->|No| C[Stop]
//
// The {braces} render as a diamond; |Yes| and |No| label the arrows.

4. Gantt, Class, and Pie Charts

Mermaid does more than flowcharts. A Gantt chart (keyword gantt) lays out dated tasks on a timeline. A pie chart (keyword pie) draws labelled slices from numbers. There are also class diagrams (classDiagram) for object structure and several others. Each diagram type has its own opening keyword and small syntax, but they all live in the same mermaid fence.

\`\`\`mermaid
gantt
  title Project Plan
  dateFormat YYYY-MM-DD
  section Build
  Design   :a1, 2026-06-01, 7d
  Develop  :a2, after a1, 14d
\`\`\`
\`\`\`mermaid
pie title Languages
  "JavaScript" : 45
  "Python" : 30
  "Other" : 25
\`\`\`

Common Errors (and the fix)

📋 Quick Reference — Mermaid

ConceptSyntax
Flowchart startgraph TD (or LR)
Box nodeA[Label]
Decision nodeA{Label}
EdgeA --> B
Labelled edgeA -->|Yes| B
SequencesequenceDiagram
Gantt / Piegantt / pie

Mini-Challenge: A Login Flow

No blanks this time — just a brief and an outline. Use console.log to print the source for a flowchart with a start box, a decision diamond, and Yes/No arrows to two outcome boxes. Run it, then wrap it in a mermaid fence on GitHub.

// 🎯 MINI-CHALLENGE: print a login-flow diagram source.
// Build (with console.log) the SOURCE for a flowchart that includes:
//   1. A graph direction line (graph TD)
//   2. A start box, e.g. A[Visit site]
//   3. A decision diamond, e.g. B{Logged in?}
//   4. A "Yes" arrow to a dashboard box and a "No" arrow to a login box
//
// ✅ Expected printed source (shape):
//    graph TD
//      A[Visit site] --> B{Logged in?}
//      B -->|Yes| C[Dashboard]
//      B -->|No| D[Login]
//
// Then wrap it in a mermaid fence to render it on GitHub.

// your code here

Pro Tips

Frequently Asked Questions

Q: Does Mermaid work in every Markdown viewer?

No. It renders where it's supported — GitHub, GitLab, Obsidian, and docs tools with a Mermaid plugin. Plain parsers show the fenced code as text.

Q: Is Mermaid part of the Markdown spec?

No. It's an extension layered on top of fenced code blocks. The Markdown itself just sees a code block labelled mermaid; a separate library does the drawing.

Q: Can I style the colours of a diagram?

Yes. Mermaid supports themes and per-node styling with classDef and style directives, though the basics in this lesson need none of that.

Q: How do I export a diagram as an image?

The Mermaid Live Editor and the Mermaid CLI can export PNG or SVG, which is handy for slides or sites that don't render Mermaid live.

Lesson Complete! 🎉

Where to go next: diagrams handle pictures; now handle equations. Continue to Math with LaTeX to write fractions, Greek letters, and integrals inside Markdown.

Practice quiz

How do you embed a Mermaid diagram in Markdown?

  • In a fenced code block labelled mermaid
  • With an image tag
  • Inside a YAML block
  • With double brackets

Answer: In a fenced code block labelled mermaid. You write a fenced code block whose info string is mermaid; the renderer turns its contents into a diagram.

Which keyword starts a top-down flowchart?

  • diagram down
  • chart top
  • graph TD
  • flow vertical

Answer: graph TD. graph TD (top-down) begins a flowchart; LR gives left-to-right.

What does TD mean in graph TD?

  • Two dimensional
  • Top Down direction
  • Tree Data
  • Text Display

Answer: Top Down direction. TD sets the layout direction to top-down; other options include LR, RL, and BT.

How is an edge (arrow) between two nodes written in a flowchart?

  • A ~~ B
  • A == B
  • A :: B
  • A --> B

Answer: A --> B. Two nodes are connected with -->, for example A --> B.

How do you give a node square-bracket label text, like a box named Start?

  • A{Start}
  • A[Start]
  • A<Start>
  • A(Start)

Answer: A[Start]. Square brackets A[Start] create a rectangular node with the text Start.

Which Mermaid diagram type shows messages exchanged over time between participants?

  • sequenceDiagram
  • pie
  • gantt
  • classDiagram

Answer: sequenceDiagram. sequenceDiagram models participants and the ordered messages passed between them.

Which keyword begins a Mermaid Gantt chart?

  • schedule
  • gantt
  • calendar
  • timeline

Answer: gantt. A Gantt chart starts with the gantt keyword, followed by a title and dated tasks.

Where does Mermaid commonly render automatically?

  • Image-only galleries
  • Only in PDFs
  • Plain text editors with no preview
  • GitHub and GitLab Markdown

Answer: GitHub and GitLab Markdown. GitHub and GitLab render mermaid code blocks natively, as do many documentation tools.

What does an arrow label like A -->|Yes| B do?

  • Makes the arrow dashed
  • Colours the node yellow
  • Puts the text Yes on the connecting arrow
  • Deletes node B

Answer: Puts the text Yes on the connecting arrow. Text between pipes after the arrow labels that edge; here the arrow reads Yes.

Which shape syntax creates a decision diamond in a flowchart?

  • A((round))
  • A>flag]
  • A[box]
  • A{question}

Answer: A{question}. Curly braces A{question} produce a diamond, the conventional shape for a decision.

Continue this course