Figures As Universal Containers
When I see figures, this is what I think about and what I see the most often, maybe with [srcset and sizes](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/srcset) attributes to handle responsive images. According to the [HTML specification](https://html.spec.whatwg.org/multipage/grouping-content.html#the-figure-element) (emphasis mine): > The figure element represents some flow content, optionally with a caption, that is self-contained (like a complete sentence) and is typically referenced as a single unit from the main flow of the document. > > ... > > **The element can thus be used to annotate illustrations, diagrams, photos, code listings, etc.** In this post, we'll explore how to use the figure element with different types of content. We will also discuss how to add captions, how to number groups of images, the accessibility implications of doing it, and how to reference them in the main document text. ## Annotating Elements In A Figure Using the `figure` element to wrap content is not always required but, when used with the `figcaption` element you can annotate what the element inside is about in addition to what you would do in the main body of the document. Using captions (with the `figcaption` element) also allows authors to reference the figure in other parts of the document. We'll discuss this in more detail later in the post. These examples have minimal styling ### Picture Elements The first example uses the [picture](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/picture) element. To the user it wouldn't appear any different but under the hood, it would display the best format from those provided by the developer. ### Code Listings With Syntax Highlight When working with code listings I always like to use [Prism.js](https://prismjs.com/) for syntax highlighting. ### Table Tables have always been difficult for me to create. Figures don't add complexity, they just remind me how hard it is to style them properly. We could annotate the table using the [caption](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/caption) element. However, we should keep in mind two things: * If we include a `caption` element it must be the first child of its parent table element * When we nest a `table` inside a `figure`, we should caption using figcaption inside the figure rather than caption the table directly. ### Video With Video Element The [video](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video) element is similar to what we saw in the picture element, except that the structure is different. ### Videos With YouTube Embeds We can embed streaming videos inside figures. These are different than using the `video` elements shown in the previous section but the result would be identical if the video was also available for download. ### Lists There are three types of lists that I usually work with. Ordered, unordered and definition lists. All lists should work the same as far as being inside figures go, the display will be different. The first example below shows and unordered or bulleted list. The next example shows a definition list (`dl` and its `dt` and `dd` children). ## Captioning figures So far we've covered a variety of elements that can be placed inside the `figure` element. Now we'll look at different strategies for captioning figures using the `figcaption` element. According to the HTML Living Standard: > The first figcaption element child of the element, if any, represents the caption of the figure element's contents. If there is no child figcaption element, then there is no caption. We can control whether a figure has captions witht he `figcaption` element. If we omit it then there will be no caption shown to the user. ### Numbering Figures The `figcaption` element on its own will not number the figures. We have two options to get `{type of figure} {number}: ` before the text of the caption and each of these options has advantages and disadvantages. The first way is to manually include the reference in the caption for each type of figure. Using images as examples, it coulld look like this: ```html