Skip to content

Using Mermaid in .astro Files

In Markdown and MDX you use a fenced ```mermaid block. In .astro components and pages, write the diagram source directly inside a <pre class="mermaid"> element. The client script picks up any pre.mermaid element on the page.

<pre class="mermaid">graph LR
A --> B --> C
</pre>

Set data-width, data-height, and data-align attributes — the same values the info-string options (w-NNN, h-NNN, align=...) produce in Markdown.

<pre class="mermaid" data-width="400" data-height="200" data-align="center">flowchart TD
A[Start] --> B[Process] --> C[End]
</pre>

Wrap the <pre> in a <figure class="mermaid-figure"> with a <figcaption> to mirror the title="..." option from Markdown.

<figure class="mermaid-figure" data-align="left">
<pre class="mermaid" data-width="360">flowchart LR
A[Frontend] --> B[API] --> C[Database]
</pre>
<figcaption>Request lifecycle</figcaption>
</figure>

A standalone .astro page demonstrating all of the above is deployed at /astro-diagram/.