Chapter 20 | The paragraph, hr
, pre
and blockquote
elements
In this chapter we start looking at the grouping content elements, p
(paragraph), hr
, pre
and blockquote
.
p(paragraph) and div elements
We start off with the two grouping content elements that are the most general purpose elements of the bunch. While they can both be used in a variety of similar ways, it is important to always use the element that makes the most semantic sense.
For example, both the following examples are valid:
|
|
Quote from catipsum.com
However semantically, you would choose to use the paragraph element over the div
element as it is more appropriate. The reason I am discussing these two together is that there are valid instances when you could use a paragraph element, but where a div
would be more appropriate.
NOTE: Another aspect the two elements have in common is that they have no implicit ARIA role but, can be used with any valid ARIA role. Always remember though, just because you can, does not mean that you should. Always, always choose the most appropriate HTML element for the job at hand. Only reach for these custom uses when you absolutely have no other choice.
For example, the following is valid HTML:
|
|
The more appropriate markup would be this though:
|
|
The div
element is a general block level grouping element whilst the paragraph element is more appropriate for paragraphs of text. There are also some situations where a paragraph element might seem appropriate but, would be invalid HTML, for example:
|
|
The above would be invalid because list elements cannot be children of a paragraph element. While it is a rather odd example, you could substitute a div
in place of the paragraph element and have valid HTML:
|
|
With a little bit of CSS:
|
|
you could have the above rendered as:
Live Codepen - A little odd, but technically valid, sentence
A strange use case to be sure, but now you know how to approach it should it ever come up in real life. 🙃
hr(Thematic break)
The hr
, or thematic break element, has an implicit ARIA role of separator
and as such represents a thematic break in content, for example:
|
|
NOTE: While this element will still render as a horizontal rule in visual browsers, it is now defined in semantic terms and should not be used for purely visual purposes. A better candidate would be CSS. Also, note that all previous attributes of the thematic break(as it is now known) have been deprecated or are non-standard. Any styling should be done using CSS.
pre
If you have seen code examples online in blog posts, or books such as this one, you have already come into contact with the pre
element.
NOTE: Using the
pre
element by itself to markup code examples is not the whole story though. When doing so, you would couple it with thecode
element. More on that in a future chapter.
The pre
element is a way to markup preformatted text. In HTML if you write the following:
|
|
The end result would be visually rendered as:
Live Codepen - Whitespace behaviour with a paragraph tag
As you can see, the only thing that will be honored is single space characters, all other additional whitespaces will be collapsed. If you wanted to visually preserve the whitespace, you would mark it up using the pre
element.
|
|
Now it will be rendered exactly as typed:
Live Codepen - Whitespace behaviour with the pre
tag
This is then also how you would markup things such as ASCII Art:
|
|
- Borrowed from https://www.asciiart.eu/
blockquote
As the name suggests this element is used to markup content that is quoted from another source:
|
|
Without any additional CSS, the blockquote
element will be indented from its surrounding text but, will not automatically include quotation marks. This can be included in the text or added stylistically via CSS. While not required, it is valid to use other HTML elements such as the paragraph tag inside the blockquote
element.
While not directly exposed to users, you can include a URL to where the original content is quoted from using the cite
attribute:
|
|
While a user agent could enable users to follow a citation link, it is not required and not implemented as such. Another interesting side note is that the specification specifically indicates that attribution for the quotation should be placed outside of the blockquote
element, for example:
|
|
That is then also the most common way to expose the URL specified in the cite
attribute.
That is all for our first taste of the grouping elements of HTML. In the next chapter we will continue by looking at all things list-related.
Related Reading
- No related reading for this chapter. Have another 🍪