Chapter 25 | data
, time
, code
, var
, samp
, and kbd
elements
Continuing with the text-level semantics elements, in this chapter we cover data
, time
, code
, var
, samp
, and kbd
.
data
Not to be confused with the data
attribute, the data
element represents, well, data. The data
element has a required value
attribute that represents a machine-readable representation of its contents. For example:
|
|
Another use for the data
element is when used in conjunction with JavaScript to, for example, ease sorting of a data table.
|
|
In the above table, the data
element is used to represent the total number of cookies sold by each child. This makes implementing a sorting function for the “Cookies sold” column much simpler than having to parse the numbers from the text content, calculate the sum, and then apply a sort.
Another use case is to combine the data
element with microdata.
|
|
time
Whereas the data
element is a generic container for representing human and machine-readable content, the time
element is specifically meant to represent dates and time values. While the value
attribute of the data
element is required, the datetime
attribute of the time
element is not. While this is valid, the most common use case for the time
element is to define both a human and machine-readable format of the date, time, or date and time.
|
|
The above is entirely valid but, it would probably be more user friendly to represent the information as follows:
|
|
With the above you have both a human-readable representation and a machine-readable format you can parse with JavaScript, for example:
|
|
Live Codepen - Combining the time
element’s datetime
attribute with JavaScript
See the live example on Codepen.io
To learn what constitutes a valid date and time format that can be used with the element it is best to refer to the official documentation here.
code
We briefly touched on this element earlier when discussing the pre
element. As its name suggests, this element is used to markup computer code. It can be used both inline or to markup an entire section of code.
For example inline it can be used as follows:
|
|
Or to markup an entire code example(in these instances it is best to couple it with the pre
element so that the browser respects intended whitespace):
|
|
Live Codepen - Combining the pre and code elements
See the live example on Codepen.io
NOTE: As you will notice from the live examples, the browser will use a monospaced font for content marked up with the
code
element.
var
Again, this is not to be confused with the JavaScript var
keyword but, it is used to markup variables. This does not have to be a variable in a programming sense but, can also be used to markup mathematical variables as well as terms used as placeholders in content.
|
|
Or alternatively in a programming context:
|
|
Live Codepen - Using the var
element
See the live example on Codepen.io
samp
Not to be confused with the African food of the same name 😀 (love me some samp and beans #nomnom), the samp
element represents the output from another program or computing system.
|
|
The above shows the output of a Git command ran in a Terminal application and combined with the pre
element is a perfect use case for the samp
element.
Live Codepen - Using the samp
element
See the live example on Codepen.io
kbd
Where the samp
element was for computer output, the kbd
is meant to markup user input, commonly keyboard input.
|
|
You can also nest the kbd
element inside a samp
element. In this case, it represents the input as it was echoed by the system.
|
|
When a kbd
element contains a samp
element it represents input based on system output.
|
|
Live Codepen - Using the kbd
element
See the live example on Codepen.io
In the above example, the entire line cat renovate.json | grep "packageRules"
represents user input, but the result(output) of cat renovate.json
is piped(|
) to the grep "packageRules"
as input. The last line is then the output of executing the entire string of user input. 🙃 That is all to say that you can go pretty crazy and detailed with the combination of all of these elements but, it is often not needed. Always be mindful to use the appropriate elements, and aim for the simplest solution that effectively conveys the message both semantically and visually.
That then concludes our exploration of representing various types of data using text-level semantics.