ePUB doesn’t like named anchors, use IDs

In a recent ePUB I got lazy. Rather than having separate XHTML files for each section I kept them in larger files and used HTML anchor and name tags to match them up. Problem is this doesn’t work.

What I had was one file per chapter, divided into sections. So my content.xhtml looked like this:

<h1>Chapter Title</h1>
<h2><a name="part1"></a>First Part</h2>
<h2><a name="part2"></a>Second Part</h2>
<h2><a name="part3"></a>Third Part</h2>

This was then accessed from toc.ncx as follows. Important link bits in bold:

<navPoint playOrder=11><navLabel><text>Chapter Title</text></navLabel>
  <content src="chapter.xhtml" /></navPoint>
<navPoint playOrder=12><navLabel><text>Part 1</text></navLabel>
  <content src="chapter.xhtml#part1" /></navPoint>
<navPoint playOrder=13><navLabel><text>Part 2</text></navLabel>
  <content src="chapter.xhtml#part2" /></navPoint>
<navPoint playOrder=14><text>Part </text></navLabel>
  <content src="chapter.xhtml#part3" /></navPoint>

None of these TOC links work when published as an ePUB (testing in Adobe Digital Editions).

Bit of digging (this helped) and testing, and issue is with the “name” piece. These are not recognised. Instead you need to use IDs.

The good news is you don’t have to change the toc.ncx references, or any other references to the sections. Only the targets need to change. Edit your content from:

<h2><a name="part1">First Part</h2>

To any of the following:

<h2><a id="part1"></a>First Part</h2>
<h2><a id="part1" name="part1"></a>First Part</h2>
<h2 id="part1">First Part</h2>

Obviously the third option (add the ID to the heading itself) is the cleanest. However the first option is the one easiest to implement via global search and replace. Which means it was the one I chose.

3 thoughts on “ePUB doesn’t like named anchors, use IDs

  1. Frank

    The Azardi eBooks Reader strangly does not like any of the options mentioned above. Can’t get anchors to work. Does anyone know a solution for that?
    Thanks in advance


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>