Venkatsai Maragoni · Technical writer

Documentation

Mermaid Diagrams in GitHub Pages—User Guide

A comprehensive guide to using Mermaid diagrams in your GitHub Pages portfolio site.

Table of contents

  1. What is Mermaid?
  2. Prerequisites
  3. Installing Dependencies
  4. Installing Mermaid
  5. Configuring Mermaid
  6. Creating Your First Mermaid Diagram
  7. Diagram Types Reference
  8. Best Practices
  9. FAQs

What is Mermaid?

Mermaid is a JavaScript-based diagramming and charting tool that renders Markdown-inspired text definitions into diagrams dynamically in the browser. It allows you to create flowcharts, sequence diagrams, Gantt charts, class diagrams, and more—all using simple, human-readable text syntax.

Place holder for SS

Why use Mermaid?

  • Text-based: diagrams are defined in plain text, making them easy to version-control with Git.
  • No external tools needed: no need for Visio, Draw.io, or image editors—diagrams live alongside your code.
  • Dynamic rendering: diagrams are rendered client-side in the browser, so they’re always up to date.
  • Wide support: natively supported in GitHub Markdown, GitLab, Notion, Confluence, and many other platforms.
  • Lightweight: no heavy dependencies—just a single JavaScript library.

Prerequisites

Before you begin, ensure you have the following installed on your machine:

Tool Minimum Version Purpose
Node.js v16+ JavaScript runtime
npm v8+ Package manager (bundled with Node.js)
Git v2.30+ Version control
A code editor — VS Code recommended

Verify installations

node --version
# Expected output: v16.x.x or higher

npm --version
# Expected output: 8.x.x or higher

git --version
# Expected output: git version 2.30.x or higher

Installing dependencies

Step 1: initialize your GitHub Pages project

If you don’t already have a GitHub Pages site, create one:

# Create a new directory for your portfolio
mkdir my-portfolio
cd my-portfolio

# Initialize a Git repository
git init

# Initialize a Node.js project
npm init -y

If you’re using a static site generator like Jekyll (GitHub Pages default) or Hugo, install it first.

For Jekyll (Ruby-based—GitHub Pages default):

# Install Ruby (if not already installed)
# macOS:
brew install ruby

# Ubuntu/Debian:
sudo apt-get install ruby-full

# Install Jekyll and Bundler
gem install jekyll bundler

# Create a new Jekyll site
jekyll new my-portfolio
cd my-portfolio

For a plain HTML/JS site (no static site generator):

# Create basic project structure
mkdir -p assets/js assets/css
touch index.html

Step 3: install a local development server

# Option A: Using npm
npm install --save-dev live-server

# Option B: Using Python (if available)
# python3 -m http.server 8000

Installing Mermaid

There are multiple ways to add Mermaid to your GitHub Pages site. Choose the method that best fits your setup.

Add the Mermaid script directly to your HTML file via a CDN. No npm install required.

<!-- Add this in the <head> or before the closing </body> tag -->
<script type="module">
  import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs';
  mermaid.initialize({ startOnLoad: true });
</script>

Method 2: npm install (for Node.js projects)

# Install Mermaid as a dependency
npm install mermaid

# Or as a dev dependency
npm install --save-dev mermaid

Then import it in your JavaScript:

// ES Module
import mermaid from 'mermaid';

// CommonJS
const mermaid = require('mermaid');

Method 3: Jekyll plugin (for Jekyll-based GitHub Pages)

Add the following to your Gemfile:

gem "jekyll-mermaid"

Then run:

bundle install

Add the plugin to your _config.yml:

plugins:
  - jekyll-mermaid

Method 4: native GitHub Markdown support

GitHub natively renders Mermaid in .md files. Simply use fenced code blocks with the mermaid language identifier:

```mermaid
graph TD
    A[Start] --> B[End]
```

Note: This works in GitHub-rendered Markdown (READMEs, issues, PRs) but does not automatically work in GitHub Pages HTML output. For GitHub Pages, use Method 1 or 2.


Configuring Mermaid

After installing Mermaid, you can customize its behavior and appearance.

Basic configuration

<script type="module">
  import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs';

  mermaid.initialize({
    // Core settings
    startOnLoad: true,           // Auto-render diagrams on page load
    theme: 'default',            // Theme: 'default', 'dark', 'forest', 'neutral', 'base'
    securityLevel: 'loose',      // 'strict', 'loose', 'antiscript', 'sandbox'
    logLevel: 'error',           // 'trace', 'debug', 'info', 'warn', 'error', 'fatal'

    // Flowchart-specific settings
    flowchart: {
      useMaxWidth: true,         // Diagrams scale to container width
      htmlLabels: true,          // Allow HTML in labels
      curve: 'basis',            // Line curve style: 'basis', 'linear', 'cardinal'
      padding: 15                // Padding around the diagram
    },

    // Sequence diagram settings
    sequence: {
      diagramMarginX: 50,
      diagramMarginY: 10,
      actorMargin: 50,
      width: 150,
      height: 65,
      boxMargin: 10,
      useMaxWidth: true,
      showSequenceNumbers: false
    },

    // Gantt chart settings
    gantt: {
      titleTopMargin: 25,
      barHeight: 20,
      barGap: 4,
      topPadding: 50,
      sidePadding: 75
    }
  });
</script>

Theme configuration

Mermaid comes with five built-in themes:

Theme Description
default Light theme with blue accents
dark Dark background with light text
forest Green-toned nature theme
neutral Grayscale, minimal theme
base Bare theme for full customization

Using a built-in theme:

mermaid.initialize({
  theme: 'dark'
});

Custom theme with CSS variables (using base theme):

mermaid.initialize({
  theme: 'base',
  themeVariables: {
    primaryColor: '#4a90d9',
    primaryTextColor: '#ffffff',
    primaryBorderColor: '#3a7bc8',
    lineColor: '#5a5a5a',
    secondaryColor: '#f4f4f4',
    tertiaryColor: '#e8e8e8',
    fontFamily: 'Inter, sans-serif',
    fontSize: '14px'
  }
});

Full GitHub Pages HTML template

Here’s a complete, ready-to-use HTML template:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>My Portfolio</title>
  <style>
    body {
      font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
      max-width: 900px;
      margin: 0 auto;
      padding: 2rem;
      background-color: #f9f9f9;
      color: #333;
    }
    .mermaid {
      background-color: #ffffff;
      border-radius: 8px;
      padding: 1rem;
      box-shadow: 0 2px 4px rgba(0,0,0,0.1);
      margin: 1.5rem 0;
    }
    h1, h2, h3 { color: #2c3e50; }
  </style>
</head>
<body>

  <h1>My Portfolio</h1>

  <!-- Your Mermaid diagrams go here -->
  <div class="mermaid">
    graph TD
      A[Start] --> B[Process]
      B --> C[End]
  </div>

  <!-- Mermaid JS -->
  <script type="module">
    import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs';
    mermaid.initialize({
      startOnLoad: true,
      theme: 'default',
      flowchart: { useMaxWidth: true, htmlLabels: true }
    });
  </script>

</body>
</html>

Creating your first Mermaid diagram

Step 1: add a diagram container

In your HTML file, add a <div> (or <pre>) with the class mermaid:

<div class="mermaid">
  graph TD
    A[Visit Portfolio] --> B{Interested?}
    B -->|Yes| C[Contact Me]
    B -->|No| D[Browse More]
    C --> E[Collaboration Begins!]
    D --> A
</div>

Step 2: preview locally

# Using live-server
npx live-server

# Or using Python
python3 -m http.server 8000

Open your browser to http://localhost:8080 (or 8000 for Python) and you should see your diagram rendered.

Step 3: deploy to GitHub Pages

git add .
git commit -m "Add Mermaid diagrams to portfolio"
git push origin main

Enable GitHub Pages in your repository settings:

  1. Go to Settings → Pages
  2. Under Source, select your branch (for example, main) and folder (for example, / (root))
  3. Click Save

Your site is then live at https://<username>.github.io/<repo-name>/.

Example diagrams

Flowchart:

graph LR
    A[HTML] --> B[CSS]
    B --> C[JavaScript]
    C --> D[React]
    D --> E[Portfolio]

Sequence Diagram:

sequenceDiagram
    participant User
    participant Portfolio
    participant GitHub

    User->>Portfolio: Visit site
    Portfolio->>GitHub: Fetch data via API
    GitHub-->>Portfolio: Return repos
    Portfolio-->>User: Display projects

Gantt Chart:

gantt
    title Portfolio Development Timeline
    dateFormat  YYYY-MM-DD
    section Design
    Wireframes           :done,    des1, 2026-01-01, 7d
    UI Mockups           :done,    des2, after des1, 5d
    section Development
    HTML/CSS Setup       :done,    dev1, after des2, 3d
    JavaScript Logic     :active,  dev2, after dev1, 7d
    Mermaid Integration  :         dev3, after dev2, 3d
    section Deployment
    Testing              :         dep1, after dev3, 3d
    Go Live              :milestone, after dep1, 0d

Pie Chart:

pie title Skills Distribution
    "JavaScript" : 35
    "Python" : 25
    "HTML/CSS" : 20
    "SQL" : 10
    "DevOps" : 10

Class Diagram:

classDiagram
    class Portfolio {
        +String name
        +String[] projects
        +render()
        +deploy()
    }
    class Project {
        +String title
        +String description
        +String techStack
        +display()
    }
    Portfolio "1" --> "*" Project : contains

Diagram types reference

Diagram Type Syntax Keyword Use Case
Flowchart graph or flowchart Process flows, decision trees
Sequence Diagram sequenceDiagram API interactions, user flows
Class Diagram classDiagram Object-oriented design
State Diagram stateDiagram-v2 State machines, lifecycle
Entity Relationship erDiagram Database schemas
Gantt Chart gantt Project timelines
Pie Chart pie Data distribution
Git Graph gitgraph Branch/merge visualization
Mindmap mindmap Brainstorming, concept maps
Timeline timeline Chronological events
Quadrant Chart quadrantChart Priority matrices
Sankey Diagram sankey-beta Flow/resource distribution

Best practices

1. Keep diagrams simple and focused

✅ DO: One concept per diagram
❌ DON'T: Cram everything into a single massive diagram

Break complex systems into multiple smaller diagrams rather than one overwhelming chart. Each diagram should communicate a single idea clearly.

2. Use meaningful node IDs and labels

✅ DO:
graph TD
    userLogin[User Login] --> authCheck{Authentication Check}
    authCheck -->|Valid| dashboard[Dashboard]

❌ DON'T:
graph TD
    A[User Login] --> B{Authentication Check}
    B -->|Valid| C[Dashboard]

Descriptive IDs make your diagram source code self-documenting and easier to maintain.

3. Choose the right diagram type

  • Flowcharts → Decision logic, workflows
  • Sequence diagrams → API calls, multi-party interactions
  • Class diagrams → Code architecture
  • Gantt charts → Project planning
  • ER diagrams → Database design

4. Use consistent styling

Apply a single theme across all diagrams on your site for visual consistency. Use the themeVariables configuration to match your portfolio’s color scheme.

5. Add direction intentionally

  • graph TD—Top to Down (vertical flow)
  • graph LR—Left to Right (horizontal flow)
  • graph BT—Bottom to Top
  • graph RL—Right to Left

Choose the direction that best represents the logical flow of your content.

6. Use subgraphs for grouping

graph TD
    subgraph Frontend
        A[React] --> B[Redux]
    end
    subgraph Backend
        C[Node.js] --> D[Express]
    end
    B --> C

7. Version control your diagrams

Since Mermaid diagrams are text-based, they diff cleanly in Git. Commit diagram changes alongside code changes for a complete history.

8. Test before deploying

Always preview diagrams locally before pushing to GitHub Pages. A syntax error stops the diagram from rendering, and visitors don’t see an error message.

Use the Mermaid Live Editor to test and debug diagrams quickly.

9. Add fallback content

For accessibility and SEO, consider adding a <noscript> fallback or alt text description:

<div class="mermaid" aria-label="Flowchart showing the deployment pipeline from code commit to production">
  graph LR
    A[Commit] --> B[Build] --> C[Test] --> D[Deploy]
</div>
<noscript>
  <p>Diagram: Deployment pipeline — Commit → Build → Test → Deploy</p>
</noscript>

10. Pin the Mermaid version

Avoid unexpected breaking changes by pinning to a specific version:

<!-- Pinned to a specific version -->
<script type="module">
  import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@11.4.1/dist/mermaid.esm.min.mjs';
</script>

FAQs

Why doesn’t a Mermaid diagram render on GitHub Pages?

A: GitHub Pages serves static HTML. Unlike GitHub Markdown (which natively renders Mermaid), GitHub Pages requires you to include the Mermaid JavaScript library explicitly. Add the CDN script tag to your HTML file (see Installing Mermaid—Method 1).


Does Mermaid work with Jekyll-based GitHub Pages?

A: Yes. You have two options:

  1. Use the jekyll-mermaid plugin (see Method 3).
  2. Add the CDN script to your Jekyll layout template (for example, _layouts/default.html).

Note: GitHub Pages only supports a limited set of Jekyll plugins. If using a custom plugin, you may need to build locally and push the _site folder, or use GitHub Actions for the build.


How do you make diagrams responsive on mobile?

A: Set useMaxWidth: true in your Mermaid configuration:

mermaid.initialize({
  flowchart: { useMaxWidth: true },
  sequence: { useMaxWidth: true }
});

Also add CSS to ensure the container scales:

.mermaid {
  max-width: 100%;
  overflow-x: auto;
}

Does Mermaid support dark mode?

A: Yes. Use the dark theme:

mermaid.initialize({ theme: 'dark' });

For dynamic dark mode toggling based on user preference:

const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
mermaid.initialize({
  theme: prefersDark ? 'dark' : 'default'
});

How do you debug a diagram syntax error?

A: Use the Mermaid Live Editor to paste your diagram code and see real-time error messages. Common issues include:

  • Missing arrow syntax (--> not ->)
  • Unmatched brackets or parentheses
  • Special characters in labels (wrap in quotes: A["Label with (parens)"])
  • Indentation issues (Mermaid is whitespace-sensitive in some contexts)

Is there a maximum diagram size?

A: There’s no hard limit, but very large diagrams (100+ nodes) can become slow to render and difficult to read. If your diagram is getting too large:

  • Break it into multiple smaller diagrams
  • Use subgraphs to organize sections
  • Consider linking between pages for different diagram views

Can Mermaid diagrams be exported as images?

A: Yes, several options:

  1. Mermaid Live Editor—Export as SVG or PNG from mermaid.live
  2. Mermaid CLI—Install @mermaid-js/mermaid-cli and use:

    npm install -g @mermaid-js/mermaid-cli
    mmdc -i input.mmd -o output.svg
    
  3. Browser—Right-click the rendered SVG and save as image

A: Yes. Mermaid supports click events and hyperlinks:

graph TD
    A[My GitHub] --> B[My Portfolio]
    click A "https://github.com/username" "Visit GitHub" _blank
    click B "https://username.github.io" "Visit Portfolio" _blank

How do you use Mermaid with Markdown files on GitHub Pages?

A: If you’re using a Markdown-based static site generator (Jekyll, Hugo, etc.), you can include Mermaid blocks in your .md files. Ensure your layout template includes the Mermaid script, then use:

<div class="mermaid">
graph TD
    A --> B --> C
</div>

Some generators also support fenced code blocks with post-processing plugins.


Does Mermaid support real-time or dynamic data?

A: Mermaid renders static diagrams from text definitions. It does not natively support real-time data binding. However, you can dynamically generate Mermaid syntax with JavaScript and call mermaid.run() to re-render:

const diagramDiv = document.getElementById('dynamic-diagram');
diagramDiv.textContent = `graph TD\n  A[Count: ${count}] --> B[Done]`;
diagramDiv.removeAttribute('data-processed');
await mermaid.run({ nodes: [diagramDiv] });

Additional resources


Last updated: July 2026

Last updated: