Handling footnotes and references in HTML5
Join the DZone community and get the full member experience.
Join For FreeThis post examines what options one has for handling footnotes and references in HTML. It then presents a library that helps you with handling them.
Requirements
Handling footnotes and references comes with several requirements:
- On screen, one wants to show the footnote text as close as possible to the number pointing to the footnote. Whatever solution one chooses, it should also work on touch devices. Hence, a hover-only approach is not feasible.
- In print, footnotes should be shown, as well. Hence, a tooltip-only solution is not acceptable.
- Lastly, things should degrade gracefully if JavaScript is switched off.
The HTML5 spec recommendations for footnotes
The HTML5 specification gives several tips on how to format footnotes.
Short inline annotations: title attribute.
<b>Customer</b>: Hello! I wish to register a complaint. Hello. Miss? <p> <b>Shopkeeper</b>: <span title="Colloquial pronunciation of 'What do you'" >Watcha</span> mean, miss? <p> <b>Customer</b>: Uh, I'm sorry, I have a cold. I wish to make a complaint. <p> <b>Shopkeeper</b>: Sorry, <span title="This is, of course, a lie.">we're closing for lunch</span>.Longer annotations: bidirectional linking via <a>:
<p> Announcer: Number 16: The <i>hand</i>. <p> Interviewer: Good evening. I have with me in the studio tonight Mr Norman St John Polevaulter, who for the past few years has been contradicting people. Mr Polevaulter, why <em>do</em> you contradict people? <p> Norman: I don't. <sup><a href="#fn1" id="r1">[1]</a></sup> <p> Interviewer: You told me you did! ... <section> <p id="fn1"><a href="#r1">[1]</a> This is, naturally, a lie, but paradoxically if it were true he could not say so without contradicting the interviewer and thus making it false.</p> </section>
Wikipedia-style highlighting of the currently active footnote
The CSS pseudo-selector :target allows you to style the HTML element whose ID is the same as the page fragment identifier:
li:target { background-color: #BFEFFF; }Thus, if the page URL ends with #explanation then the following list item would be highlighted:
<li id="explanation">It works like this: ...</li>
Using the html_footnotes library
You can download html_footnotes on GitHub and try it out online.
Terminology: An annotation is either a footnote or a reference (citation). The markers in the main text referring to those annotations are called annotation pointers.
- A footnote pointer is a number written in parentheses. Example: (1)
- A reference pointer is a number written in square brackets. Example: [1]
Activating the library: The library consists of CSS to style annotations and pointers and of JavaScript that post-processes the HTML so that less code has to be written.
<link rel="stylesheet" href="footnotes.css" type="text/css"> <script src="footnotes.js"></script>Footnotes: The library processes the HTML so that footnote pointers are formatted as superscript and become links that, when clicked on, display the text of the footnote inline.
You can use an IIFE<a class="ptr">(1)</a> to avoid the global namespace<a class="ptr">(2)</a> being polluted. ... <h2>Footnotes</h2> <ol id="footnotes"> <li>IIFE is an acronym for Immediately-Invoked Function Expression.</li> <li>The global scope is reified as an object in JavaScript.</li> </ol>References: Reference pointers are processed and become links. The library also adds IDs to the reference list items. Due to the appropriate CSS, a list item will be highlighted and scrolled to if one clicks on a pointer.
JavaScript has many functional language constructs <a class="ptr">[1]</a>. For example: consult <a class="ptr">[2]</a> for an introduction to closures. ... <h2>References</h2> <ol id="references"> <li><a href="http://en.wikipedia.org/wiki/Functional_programming" ><i>Functional programming</i></a>. In <i>Wikipedia</i>. Retrieved 2011-12-03.</li> <li>Douglas Crockford, <i>JavaScript: The Good Parts</i>. O’Reilly. 2008-05-16.</li> </ol>
Opinions expressed by DZone contributors are their own.
Comments