Add image captions in Jekyll without a plugin
Markdown images do not provide a built-in way to add captions. In Jekyll, a small include solves that without adding a plugin, so it also works on GitHub Pages.
Create _includes/image.html:
<figure class="image">
<img src="{{ include.url }}" alt="{{ include.description }}">
<figcaption>{{ include.description }}</figcaption>
</figure>
The <figure> and <figcaption> elements keep the image and its caption connected semantically.
Use the include from a Markdown post like this:
{% include image.html url="/images/bird.jpg" description="Bird" %}
The description value is used for both the alternative text and the visible caption. That is fine when both should say the same thing. If you need a more detailed alt description, add a separate alt parameter to the include instead of forcing one piece of text to do two jobs.
Enjoyed reading this?
If you found this article helpful, consider supporting my writing. Every little bit helps keep this blog going.
Related articles
- The HTTP Status Codes I Actually Check
A practical guide to common HTTP response codes, redirects, client and server errors, and the headers that explain them.
- What Makes a Website a Progressive Web App?
A practical look at service workers, web app manifests, installation, and the cases where a Progressive Web App is worth building.
- What a Static Site Generator Actually Does
A static site generator combines content and templates before deployment, producing HTML files that a web server can deliver directly.