<figure> tag was introduced with HTML5 quite some time ago, though you may still be unfamiliar with what it does, or how it can be used in lots of different situations. In this tutorial, you'll learn the difference between
<figure> and other comparable tags, and how you can begin using it to enhance the way you group items in your markup.
This is a beginner level tutorial, but will also serve as a useful refresher for others.
Typical Uses for
Let’s first see what the HTML5 Doctor has to say on the matter:
The figure element represents a unit of content, optionally with a caption, that is self-contained, that is typically referenced as a single unit from the main flow of the document, and that can be moved away from the main flow of the document without affecting the document’s meaning. -Richard Clark
figure element specifies self-contained content, such as illustrations and photos. This means that you'll always be using the
figure tag to house something else.
One common misconception is that
figure can only be used for images. In fact, it can be used to group any number of different things together, including, but not limited to, images, video, audio, and code blocks.
Defining Page Structure
figure is a sectioning root, which means that it effectively isolates its content from its ancestor's structure. Whatever's inside the figure doesn't contribute to the document outline outside it. It behaves as a structural element of the page and can therefore contain its own headings, beginning with an
<h1> and working down the hierarchy through
<h3> and so on, but this has no bearing on the hierarchy outside:
Elements such as this are often used to introduce external content to the document, like a quote for example.
<fieldset> is another example of a sectioning root element.
Here's a typical example of how the
figure tag can be used to display an image.
<figure> <img src="your-image.png" alt="Hello world! I am an image." /> </figure>
Now, you may be thinking, "Why bother wrapping my img tag in additional markup?" That's a fair question, but it begins to make sense once you realise that a
<figure> can contain more than one child element, such as a caption.
There's also a tag for a figure caption, or rather,
figcaption. Taking this into account, you'd be able to use it in the following manner.
<figure> <img src="your-image.png" alt="Hello world! I am an image." /> <figcaption>What's up? I am a caption!</figcaption> </figure>
Using just a series of
img elements to display images together works fine, though each individual image would standalone as its own block of code, completely unrelated to any others. In cases where images share a relationship of some kind
figure comes to the rescue. These three images are related and share a single summary in the
<figure> <img src="image1.png" alt="Image 1" /> <img src="image2.png" alt="Image 2" /> <img src="image3.png" alt="Image 3" /> <figcaption>Three different images, grouped together as a figure, and being captioned with figcaption.</figcaption> </figure>
<figure> for Code Blocks
figure element's talent for grouping commonly-themed elements doesn't end there. Perhaps you want to group some inline code, along with a caption, in a tutorial for example, then you could use
figcaption in the following way:
<figure> <p><code>Life is good, said HTML5 to XHTML.</code></p> <figcaption>In this code block, we see HTML5 speaking to XHTML.</figcaption> </figure>
figure can be used in this way with
samp, making it easy to group code blocks.
That's it! You've learned how to use the
figure element, along with
figcaption and other elements to group multiple items together. How do you use the
figure element at the moment? Will that change as a result of reading this quick tip? Tell us in the comments!
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.Update me weekly
Envato Tuts+ tutorials are translated into other languages by our community members—you can be involved too!Translate this post