I am hosting a Jekyll Blog on Github and write my posts with Markdown. When I am adding images, I do it the following way:
![name of the image](http://link.com/image.jpg)
This then shows the image in the text.
However, how can I tell Markdown to add a caption which is presented below or above the image?
There are two semantically correct solutions to this question:
1. Using a plugin
I've tried a couple of plugins doing this and my favourite is
jekyll-figure
.1.1. Install
jekyll-figure
One way to install
jekyll-figure
is to addgem "jekyll-figure"
to your Gemfile in your plugins group.Then run
bundle install
from your terminal window.1.2. Use
jekyll-figure
Simply wrap your markdown in
{% figure %}
and{% endfigure %}
tags.You caption goes in the opening
{% figure %}
tag, and you can even style it with markdown!Example:
1.3. Style it
Now that your images and captions are semantically correct, you can apply CSS as you wish to:
figure
(for both image and caption)figure img
(for image only)figcaption
(for caption only)2. Creating a custom include
You'll need to create an
image.html
file in your_includes
folder, and include it using Liquid in Markdown.2.1. Create _includes/image.html
Create the
image.html
document in your _includes folder :2.2. In Markdown, include an image using Liquid
An image in
/assets/images
with a caption:An (external) image using an absolute URL: (change
src=""
tosrcabs=""
)A clickable image: (add
url=""
argument)An image without a caption:
2.3. Style it
Now that your images and captions are semantically correct, you can apply CSS as you wish to:
figure
(for both image and caption)figure img
(for image only)figcaption
(for caption only)Andrew's @andrew-wei answer works great. You can also chain a few together, depending on what you are trying to do. This, for example, gets you an image with alt, title and caption with a line break and bold and italics in different parts of the caption:
With the following
<img>
markdown:That is the basic caption use. Not necessary to use an extra plugin.
I know this is an old question but I thought I'd still share my method of adding image captions. You won't be able to use the
caption
orfigcaption
tags, but this would be a simple alternative without using any plugins.In your markdown, you can wrap your caption with the emphasis tag and put it directly underneath the image without inserting a new line like so:
This would generate the following HTML:
Then in your CSS you can style it using the following selector without interfering with other
em
tags on the page:Note that you must not have a blank line between the image and the caption because that would instead generate:
You can also use whatever tag you want other than
em
. Just make sure there is a tag, otherwise you won't be able to style it.