Mermaid Accessibility Best Practices

AGPL-3.0-or-later License - See LICENSE file for full text
Copyright (c) 2026 Mike Gifford

Normative reference for authoring, annotation, linting, and the “Generate prompt to improve this diagram” workflow.

Based on:


1. Required Metadata

Every Mermaid diagram must include:

%%accTitle Brief title (max 100 characters)
%%accDescr Detailed description explaining what the diagram shows and why

Title Requirements

Description Requirements

Example: Decision Tree

graph TD
    A[User Login] --> B{Valid Credentials?}
    B -->|Yes| C[Grant Access]
    B -->|No| D[Show Error]
    C --> E[Load Dashboard]
    D --> F[Retry Login]

%%accTitle User Authentication Flowchart
%%accDescr Describes a login process where credentials are validated. If valid, the user is granted access and the dashboard loads. If invalid, an error message is shown and the user can retry.

2. SVG Accessibility Requirements

Pattern 11 Implementation (Carie Fisher)

All output SVGs must implement Pattern 11:

<svg role="img" aria-labelledby="title-id desc-id">
  <title id="title-id">Diagram Title</title>
  <desc id="desc-id">Diagram Description</desc>
  <!-- diagram content -->
</svg>

Rationale: Pattern 11 is the most reliable pattern across different screen reader/browser combinations. While it may repeat content in some configurations, it never ignores accessibility elements.

Required Attributes

ID Generation Rules


3. Semantic Structure for Flowcharts

Following Léonie Watson’s patterns:

Root-Level Structure

<svg role="img" aria-labelledby="...">
  <title>...</title>
  <desc>...</desc>
  <g role="list">
    <!-- Each logical node -->
    <g role="listitem">
      <title>Node label</title>
      <!-- node content -->
    </g>
  </g>
</svg>

Node Requirements


4. Node Type Annotation (Future)

The tool should support optional annotations for node type inference:

%%a11y-node A type=question
%%a11y-node B type=statement
%%a11y-node C type=process
%%a11y-edge A->B ariaLabel="Yes, continue"

Node Types:


5. Validation Rules

Pre-Export Validation

Before exporting, the tool must verify:

  1. Metadata present — Both %%accTitle and %%accDescr exist
  2. Title length — ≤100 characters
  3. Description present — ≥10 characters, ≤500 recommended
  4. SVG well-formed — No parsing errors
  5. IDs unique — No duplicate IDs within SVG
  6. Role attributesrole="img" on root SVG
  7. ARIA labelledby — Both title and desc IDs referenced
  8. Contrast — WCAG 4.5:1 (text), 3:1 (non-text) in light AND dark modes

Warnings (Non-blocking)

Errors (Blocking Export)


6. Dark Mode Handling

Theming Requirements

Example SVG with Dark Mode Support

<svg role="img" xmlns="http://www.w3.org/2000/svg">
  <title>Data Flow</title>
  <desc>Shows data moving from input to output</desc>
  <style>
    @media (prefers-color-scheme: dark) {
      .line { stroke: #e0e0e0; }
      .text { fill: #ffffff; }
    }
  </style>
  <!-- diagram content -->
</svg>

7. Linting Checklist

Apply this checklist before accepting any diagram for export:


8. User Prompts for Improvement

When the tool detects ambiguity, it must prompt the user:

Decision Node Prompt

Is this a decision point (question)?
⚫ Yes, this is a decision (e.g., "Check if valid?")
⚫ No, this is an action/process (e.g., "Check password")

Edge Label Prompt

Add contextual label for "Yes" branch:
[Example: "Yes, create account"]

Description Clarification

Your description is very brief. Consider explaining:
- What triggers this diagram?
- What are the key decision points?
- What happens at the end?

9. Contrast Checking (WCAG 2.x and APCA)

WCAG Contrast Ratio

Calculate per WCAG formula:

(Lmax + 0.05) / (Lmin + 0.05)

Where L (luminance) is calculated from RGB:

L = 0.2126 * R + 0.7152 * G + 0.0722 * B
(with component gamma adjustments)

APCA (Advanced Perceptual Contrast Algorithm)

Note: APCA applies only to text, not decorative fills.

Validation Thresholds


10. Known Limitations

Document these limitations in diagram metadata or UI:

  1. Mermaid’s native a11y support is limited; this tool enhances it
  2. Very complex diagrams may need alternative representations
  3. Color-only differentiation (e.g., different colors for status) should include additional indicators
  4. Monochromatic diagrams cannot have contrast validated
  5. Animation support depends on Mermaid version

References


Last Updated: January 16, 2026
Version: 1.0
Status: Normative Reference