Markeee Markeee - 17 days ago 6
HTML Question

Can we use <figcaption> on images without using <figure>

I am converting my website to use HTML5 semantic elements. I have lots of portfolio pages, containing lots of images. Each image has a caption, like this:

<img ... >
<p class="caption">... </p>


Can I replace the
p
tag and class with
figcaption
without having to wrap the image and the caption in a
figure
element?

Wrapping all the images and captions in a
figure
element will be a pain for me to do, and adds extra (unnecassary) code.

Using
figcaption
seems like semantically better than using a
p
tag with a class.

The images are not directly referenced by the main text on the page, so using
figure
isn't strictly necassary or needed (and as I've said I'm loath to spend the time doing this and feel it adds to code bloat).

This is obviously a very different question to: "Can a element contain only a without an image?"

Answer

You can, but it wouldn't be valid HTML. The specs say the parent must be a <figure>:

Permitted parent elements: A <figure> element; the <figcaption> element must be its first or last child.

Comments