Thursday, September 8, 2011

epub: Hyperlinks in ePub Books

I also have a site at where I talk about how to make ePub files by hand. I'm going to do some blog posts here with bits and pieces about the ePub format that don't fit in the guide. I thought this might be of interest, since I've almost decided to self-publish.

Anyway, today's topic, links in ePub files.

I've gotten a few questions about how to do links inside of ePub books. This question is both easy to answer, and complicated.

The easy answer is: Just like you would in a normal web-page. This is complicated by the fact that the ePub specification does not give any usage guidelines on how to handle links.

iBooks, Stanza, and Adobe Digital Editions all handle it this way:

If you have an eBook with two files in it, say, "chap01.xhtml" and "chap02.xhtml", and they are in the same sub-folder in the zip file, adding a link is easy:

<a href="chap02.xhtml">Link title here</a>

Put that code somewhere in the chap01.xhtml file, and it'll jump you right to it. This is handy if you want to include a Table of Contents, or link to annotations or something similar. (Yes, if you use the TOC.ncx file, the ePub book will have a Table of Contents, but only through the menu. If you want one to show up as part of the book when you're paging through it, you have to make a separate page with links.)

Also, note that all links in ePub files are relative links. If your folder structure looks like this:

  - title.xhtml
  - chap01.xhtml
  - annotations.xhtml

You can link between title and chap01 by just using the filename, not the full path. If you want to link from chap01 to annotations.xhtml, your link would have to look like this:

<a href="../extras/annotations.xhtml">link text</a>

(Please note that while you can put files in different folders like this, it's not recommended.)

Linking outside of the book can be interesting. The standard link:

<a href="">Link</a>

works in iBooks, Stanza, and Digital Editions. I'm not sure if it works in others, due to limited testing ability. (I don't have a Sony Reader or Nook.)

1 comment:

  1. Hi
    It would be useful to know how to get back from a link. I understand that you can use the back button to return but there must be a way of jumping back to the point that left. Some links in a book I have when pressed will return. But how do you create them with that in mind.

    It would be great if you could cover that sometime in the future.

    Regards Chib (