We’ve discussed a lot of new elements here at HTML5Doctor, but the
article element has somehow escaped the microscope… until now!
article is one of the new sectioning elements. It is often confused with
div but don’t worry we’ll explain the difference between them.
What the spec says
Thankfully, the spec is short and sweet:
articleelement represents a component of a page that consists of a self-contained composition in a document, page, application, or site and that is intended to be independently distributable or reusable, e.g. in syndication. This could be a forum post, a magazine or newspaper article, a blog entry, a user-submitted comment, an interactive widget or gadget, or any other independent item of content.
In addition to its content, an
<article> element typically has a heading (often in a header element), and sometimes a footer. The easiest way to conceptualise
<article> is to think of its use in a weblog, as mentioned in the spec’s examples “a blog entry” and “user-submitted comments.” Here at HTML5 Doctor, we wrap each weblog entry inside an
<article> element. We also use
<article> on ‘static’ content pages, like the About and Contact pages, as
<article> can be used for “any other independent item of content.” The tricky part is, what exactly is an independent item of content?
The smell test for going independent
An independent piece of content, one suitable for putting in an
<article> element, is content that makes sense on its own. This yardstick is up to your interpretation, but an easy smell test is would this make sense in an RSS feed? Of course weblog articles and static pages would make sense in a feed reader, and some sites have weblog comment feeds. On the other hand, a feed with each paragraph of this article as a separate post wouldn’t be very useful. The key point here is that the content has to make sense independent of its context, i.e. when all the surrounding content is stripped away.
<article> in use
We only have a title and some content, but it’s enough to make sense on its own (assume there’s a lot more content about apples ;-)
<article> <h1>Apple</h1> <p>The <b>apple</b> is the pomaceous fruit of the apple tree...</p> ... </article>
A published date leads us to add a
<header>, and there’s also content that would be suitable in a
<article> <header> <h1>Apple</h1> <p>Published: <time pubdate="pubdate">2009-10-09</time></p> </header> <p>The <b>apple</b> is the pomaceous fruit of the apple tree...</p> ... <footer> <p><small>Creative Commons Attribution-ShareAlike License</small></p> </footer> </article>
<article> with comments as nested
This example shows a weblog entry with comments. Each comment can be marked up as an
<article> within the containing
<article> <header> <h1>Apple</h1> <p>Published: <time pubdate datetime="2009-10-09">9th October, 2009</time></p> </header> <p>The <b>apple</b> is the pomaceous fruit of the apple tree...</p> ... <section> <h2>Comments</h2> <article> <header> <h3>Posted by: Apple Lover</h3> <p><time pubdate datetime="2009-10-10T19:10-08:00">~1 hour ago</time></p> </header> <p>I love apples, my favourite kind are Granny Smiths</p> </article> <article> <header> <h3>Posted by: Oranges are king</h3> <p><time pubdate datetime="2009-10-10T19:15-08:00">~1 hour ago</time></p> </header> <p>Urgh, apples!? you should write about ORANGES instead!!1!</p> </article> </section> </article>
You can use the
<section> element to split the article into logical groups of content with headings:
<article> <h1>Apple varieties</h1> <p>The apple is the pomaceous fruit of the apple tree...</p> <section> <h2>Red Delicious</h2> <p>These bright red apples are the most common found in many supermarkets...</p> </section> <section> <h2>Granny Smith</h2> <p>These juicy, green apples make a great filling for apple pies...</p> </section> </article>
Where appropriate a
<section> element can contain
<article> elements. We already saw this in the comment section example above, and other common examples are the homepage or category pages of a weblog:
<section> <h1>Articles on: Fruit</h1> <article> <h2>Apple</h2> <p>The apple is the pomaceous fruit of the apple tree...</p> </article> <article> <h2>Orange</h2> <p>The orange is a hybrid of ancient cultivated origin, possibly between pomelo and tangerine...</p> </article> <article> <h2>Banana</h2> <p>Bananas come in a variety of sizes and colors when ripe, including yellow, purple, and red...</p> </article> </section>
<article> for a widget
The specification also mentions that an interactive widget can also be an
<article>. The example below shows how the markup might look for an embedded widget from somewhere like Widgetbox.
<article> <h1>My Fruit Spinner</h1> <object> <param name="allowFullScreen" value="true"> <embed src="#" width="600" height="395"></embed> </object> </article>
You may have noticed the
pubdate attribute in these examples.
pubdate is an optional boolean attribute that may be added to one
time element within the
<article>. If present it indicates that the
<time> element is the date the
<article> was published. It can be written in several ways, the most popular being:
You could think of these as HTML and XHTML-style — the end result is the same so use the style you like. Note that
pubdate applies only to the parent
<article> element, or to the document as a whole.
The difference between
There’s been a lot of confusion over the difference (or perceived lack of a difference) between the
<section> elements in HTML5. The
<article> element is a specialised kind of
<section>; it has a more specific semantic meaning than
<section> in that it is an independent, self-contained block of related content. We could use
<section>, but using
<article> gives more semantic meaning to the content.
<section> is only a block of related content, and
<div> is only a block of content. Also as mentioned above the
pubdate attribute doesn’t apply to
<section>. To decide which of these three elements is appropriate, choose the first suitable option:
- Would the content would make sense on its own in a feed reader? If so use
- Is the content related? If so use
- Finally if there’s no semantic relationship use
Dr Bruce has written HTML5
<section>s, what’s the difference?, so we recommend reading that if you are still fuzzy on when to use
Hopefully this post has given you some insight into the correct use of the
<article> element. Do you have any other examples that you can share for using
<article> in your HTML5 markup?
I’m also keen to hear your thoughts on the confusion between the
<section> elements. Do you think the distinction between the two is clear?