HTML REFERENCE · 08
Other HTML
Comments, character references, disclosure elements, and global attributes help describe and manage ordinary HTML content. These features work without JavaScript.
Other HTML at a glance
| Element or attribute | What it means | When to use it |
|---|---|---|
<!-- comment --> |
A note in the HTML source that is not rendered as page text. | Use to explain a decision for someone reading the code. Comments are visible in source, so do not put secrets in them. Do not nest comments. |
< / > / & / " |
Character references for less-than, greater-than, ampersand, and a double quotation mark. | Use to write reserved characters as text. References begin with & and end with a semicolon, such as <. |
© / © |
Numeric and named references that both represent ©. | Use either reference when needed. Direct Unicode text such as © also works in a UTF-8 file. |
|
A nonbreaking space. | Use only when two nearby pieces of text should remain on the same line, such as a number and its unit. Use CSS for visual spacing. |
details / summary |
A disclosure widget and its visible toggle label. | Put summary first inside details. Use for optional explanations that readers can expand, as in this guide’s study answers. |
open |
A boolean attribute initially expanding details. | Use when the explanation should start visible. Omit open to start collapsed; open="false" still opens it. |
id |
An identifier unique within its document. | Use for a fragment target or an association with a label or heading. Do not reuse an ID on multiple elements. |
class |
A space-separated set of reusable class names. | Use to group elements for CSS styling. Many elements can share a class, and one element can have several classes. |
title |
Advisory text about an element. | Use only for supplementary information. Many desktop browsers show it on hover, but it is not reliably available on touch or to all assistive technology. |
lang / dir |
The language of content and its text direction. | Use lang for a language change and dir="rtl" for right-to-left content. Lang does not translate words, and dir does not change their language. |
hidden |
A global attribute marking content that is not currently relevant. | Use the plain hidden attribute to hide content from normal rendering and normally from assistive technology. It is not a privacy or security feature. |
data-* |
Custom attributes for application-specific data. | Use a meaningful name such as data-route="ridge" when other tools need a value. It does not create visible text or built-in behavior. |
Writing a source comment
The browser leaves the comment out of rendered text. Someone viewing the HTML source can still read it. The paragraph remains visible.
Code
<!-- This introduction is shared by the route notes. -->
<p>Welcome to the route notes.</p>
Output
Welcome to the route notes.
Displaying reserved characters
To display <section> as text rather than create an element, encode the angle brackets. Encode the ampersand when it is meant as ordinary text. The named and numeric copyright references below produce the same character.
Code
<p>Write <section> to begin a section.</p>
<p>Maps & route notes</p>
<p>"Ridge Loop" is the route name.</p>
<p>© Trail Notes / © Trail Notes</p>
<p>Distance: 4 km</p>
Output
Write <section> to begin a section.
Maps & route notes
"Ridge Loop" is the route name.
© Trail Notes / © Trail Notes
Distance: 4 km
The nonbreaking space keeps “4” and “km” together when wrapping. It is not a substitute for margins, padding, or indentation.
Opening and closing optional information
Summary is the toggle label and first child of details. The first explanation starts collapsed; the second starts expanded because open is present. Both can be toggled with a pointer or keyboard.
Code
<details>
<summary>What does the route name mean?</summary>
<p>The name refers to the ridge above the valley.</p>
</details>
<details open>
<summary>Which note is already visible?</summary>
<p>This explanation starts open.</p>
</details>
Output
What does the route name mean?
The name refers to the ridge above the valley.
Which note is already visible?
This explanation starts open.
Identifying and classifying content
This paragraph has a unique ID and two reusable classes. The existing note class adds the guide’s note styling; route-note has no styling rule here. Data-route stores a custom value without displaying it.
Code
<p id="ridge-note" class="note route-note" data-route="ridge">
The route follows the ridge.
</p>
<p title="An optional extra description">Route notes</p>
Output
The route follows the ridge.
Route notes
Title may appear as a hover tooltip. Put essential instructions directly in visible text rather than relying on that tooltip.
Declaring a local language and direction
Lang describes the language of a passage, which can help pronunciation tools. Dir explicitly sets direction for the Arabic greeting. These attributes describe the supplied text; they do not translate it.
Code
<p>The French greeting is <span lang="fr">Bonjour</span>.</p>
<p lang="ar" dir="rtl">مرحبا</p>
Output
The French greeting is Bonjour.
مرحبا
Keeping currently irrelevant content hidden
The second paragraph exists in the document source but is excluded from normal rendering by hidden. No reveal control is supplied here. Use details instead when readers should be able to open optional content themselves.
Code
<p>The current route note is available.</p>
<p hidden>This older note is not currently relevant.</p>
Output
The current route note is available.
This older note is not currently relevant.
Hidden content is still in the delivered HTML. Anyone inspecting the source may read it, so never use hidden to protect sensitive information.
Further reading: W3Schools: Other HTML. The explanations and examples here are written for this guide.
Study questions
Answer in your own words before opening each explanation.
Are HTML comments private?
No. They are omitted from the rendered page but remain readable in the HTML source.
How can I show <nav> as ordinary text?
Write <nav> in the HTML source. The character references display angle brackets without starting a nav element.
How do named and numeric references differ?
A named reference uses a defined name, such as ©. A numeric reference uses a character number, such as ©. Both examples produce ©.
When is a nonbreaking space useful?
When adjacent text should remain together on one line, such as a number and its unit. Use CSS for layout spacing.
What makes details start expanded?
The open attribute. Because it is boolean, open="false" still expands it; remove open to begin collapsed.
How do id and class differ?
An ID must be unique in the document. A class can be reused on many elements, and an element can have multiple space-separated class names.
Can title be the only place for an important instruction?
No. A hover tooltip is not reliably available to every reader. Put important information in visible content.
Does lang translate a sentence?
No. It identifies the language of the text already present. Dir controls text direction independently.
Does hidden protect information?
No. The text is still in the source. Hidden affects presentation and relevance, not access control.
Does data-route create visible text or an interaction?
No. It stores a custom value for tools or application code to use. HTML does not automatically display it or act on it.