Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you automatically number cross-references in HTML+CSS, without JavaScript?

Tags:

html

The usual method for creating cross-references in LaTeX is to put a \label inside something you want to refer to later, and then use \ref. So for example, writing as we saw in Section~\ref{intro} in the LaTeX source might produce "as we saw in Section 1" in the final output. Is it possible to get the same effect using just HTML and CSS? Newer features of CSS allow sections and so on to be numbered automatically, but I haven't found anything that lets you reference these counter values from elsewhere in the document.

Here is a concrete example:

<!DOCTYPE html>
<html>
<head>
    <title>Cross references</title>
    <meta charset="utf-8">
    <style type="text/css">
        body {
            counter-reset: section;
        }
        section > h1::before {
            counter-increment: section;
            content: counter(section) ". ";
        }
    </style>
</head>
<body>
<section>
    <h1 id="intro">Introduction</h1>
    <p>Pineapples contain essential vitamins.</p>
</section>

<section>
    <h1>Further discourse on pineapples</h1>
    <p>As we saw in Section ??, pineapples contain essential vitamins.</p>
</section>
</body>
</html>

In this example, all the section headings are prepended with an automatically generated number. I would like to know if there is any HTML markup or CSS styling I could use in place of ?? that will insert the number corresponding to #intro.

I realize I could use <a href="#intro"> to create a cross-hyperlink to the Introduction, but I'd like to include the section number in the link text as well.

If this is not possible with HTML and CSS alone, are there any JavaScript libraries for adding cross-references within a document?

like image 670
DGrady Avatar asked May 09 '13 02:05

DGrady


1 Answers

A better way for me to have phrased this question (in hindsight) would have been "How can you access content generated automatically by CSS without using JavaScript?", and that these questions are relevant:

  • Is it possible to access the content generated by a css :before rule?
  • How to access CSS generated content with JavaScript

It looks like accessing the automatically generated counter values is not easy even with JavaScript, and not possible without it, so that answers my question.

There are a number of documents from the W3C that discuss counters and generated content, but none as far as I could tell discuss referencing generated content from other parts of a document:

  • CSS Counter Styles Level 3
  • CSS Lists and Counters Module Level 3
  • CSS Generated and Replaced Content Module

There is a note in the W3C's List of suggested extensions to CSS that mentions exactly the cross-reference problem - it comes from an email written in 1998, and I haven't found any follow-up work, so I guess this wasn't a high priority.

Update

The CSS Generated Content for Paged Media Module discusses cross references explicitly, and describes the target-counter and target-text functions to implement them with CSS. However, this is a working draft dated 29 November 2011; these functions don't appear to be implemented in any major browser. An article from A List Apart talks about using these CSS features to produce PDF eBooks from HTML using some random propriety tools.

like image 167
DGrady Avatar answered Sep 27 '22 19:09

DGrady