DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. Handling footnotes and references in HTML5

Handling footnotes and references in HTML5

Axel Rauschmayer user avatar by
Axel Rauschmayer
·
Dec. 05, 11 · Interview
Like (0)
Save
Tweet
Share
17.50K Views

Join the DZone community and get the full member experience.

Join For Free

This 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>

Source: http://www.2ality.com/2011/12/footnotes.html

HTML

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The 5 Books You Absolutely Must Read as an Engineering Manager
  • How To Handle Secrets in Docker
  • 10 Best Ways to Level Up as a Developer
  • Container Security: Don't Let Your Guard Down

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: