🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Content & MDX••By Komal Nakrani

Astro Image Captions: figure and figcaption in Markdown

Add Astro image captions: a Figure component with the Image component for MDX, or a small rehype plugin that turns Markdown image titles into figure and figcaption.

#Astro#Image Captions#Markdown#Images

For Astro image captions, use a <figure> with a <figcaption>. In MDX, write a small Figure component that wraps the Image component from astro:assets. In plain Markdown, a short rehype plugin can turn the image title in ![alt](./photo.jpg "Caption") into a caption automatically, so writers keep the standard Markdown syntax.

Key Takeaways

  • Markdown has no caption syntax; the image title becomes a title attribute (a tooltip), not visible text.
  • <figure> plus <figcaption> is the semantic HTML for a captioned image.
  • Alt text and captions do different jobs: alt describes the image, the caption adds context.
  • Local images in Markdown are still optimized by Astro when wrapped by a plugin.

Option 1: a Figure component for MDX

---
// src/components/Figure.astro
import { Image } from 'astro:assets';
import type { ImageMetadata } from 'astro';

interface Props {
  src: ImageMetadata;
  alt: string;
  caption: string;
  credit?: string;
}
const { src, alt, caption, credit } = Astro.props;
---
<figure class="figure">
  <Image src={src} alt={alt} widths={[640, 960, 1280]} sizes="(min-width: 768px) 720px, 100vw" />
  <figcaption>
    {caption}
    {credit && <span class="credit">Photo: {credit}</span>}
  </figcaption>
</figure>
{/* src/content/blog/studio-tour.mdx */}
import Figure from '../../components/Figure.astro';
import desk from './images/desk.jpg';

<Figure src={desk} alt="Oak desk with two monitors by a window" caption="The main desk, rebuilt in 2026." credit="A. Photographer" />

Option 2: captions from Markdown titles

Install the remark pipeline and write a rehype plugin:

npm install @astrojs/markdown-remark unist-util-visit
// src/plugins/rehype-figure.mjs
import { visit } from 'unist-util-visit';

export default function rehypeFigure() {
  return (tree) => {
    visit(tree, 'element', (node, index, parent) => {
      if (node.tagName !== 'p' || !parent || index === undefined) return;
      const kids = node.children.filter((c) => !(c.type === 'text' && !c.value.trim()));
      const img = kids[0];
      if (kids.length !== 1 || img.type !== 'element' || img.tagName !== 'img') return;
      const caption = img.properties?.title;
      if (!caption) return;
      delete img.properties.title;
      parent.children[index] = {
        type: 'element',
        tagName: 'figure',
        properties: {},
        children: [img, { type: 'element', tagName: 'figcaption', properties: {}, children: [{ type: 'text', value: String(caption) }] }],
      };
    });
  };
}
// astro.config.mjs
import { defineConfig } from 'astro/config';
import { unified } from '@astrojs/markdown-remark';
import rehypeFigure from './src/plugins/rehype-figure.mjs';

export default defineConfig({
  markdown: {
    processor: unified({ rehypePlugins: [rehypeFigure] }),
  },
});

Now any image that sits alone in its paragraph and has a title gets a caption:

![Oak desk with two monitors by a window](./images/desk.jpg "The main desk, rebuilt in 2026.")

Images without a title, or inline with other text, are left untouched. Our custom remark plugin tip explains plugin structure, and the remark and rehype plugins guide covers the processor.

Style the caption

/* src/styles/global.css */
.figure, figure {
  margin: 2rem 0;
}
figure img {
  width: 100%;
  height: auto;
  border-radius: 0.5rem;
}
figcaption {
  margin-top: 0.5rem;
  font-size: 0.875rem;
  color: #475569;
}
figcaption .credit {
  display: block;
  font-size: 0.8em;
}

Alt text versus caption

Do not repeat the caption as alt text. A screen reader reads both, so duplicating them doubles the noise. Alt says what is in the image; the caption says why it matters or who made it. Our Astro Image component guide covers alt text rules, MDN’s figure reference covers the markup, and our blog and editorial themes include figure styles. The content docs show where Markdown images live.

FAQ

How do I add a caption to an image in Astro Markdown?

Markdown has no caption syntax. Use MDX with a Figure component, or add a rehype plugin that turns the image title into a <figcaption>, as shown above.

Does the Astro Image component support captions?

<Image> renders only the <img>. Wrap it in <figure> and add a <figcaption> yourself, or build a small component that does both.

Are Markdown images still optimized when wrapped in a figure?

Yes. The plugin only moves the image node into a figure. Astro still processes local images referenced in Markdown during the build.