Standard Ebooks

General XHTML and CSS Patterns

This section covers general patterns used when producing XHTML and CSS that are not specific to ebooks.

id attributes

id attributes of <section> and <article> elements

  1. Each <section> and <article> element has an id attribute.

  2. <section> or <article> elements that are direct children of the <body> element have an id attribute identical to the filename containing that <section> or <article>, without the trailing extension.

  3. In files containing multiple <section> or <article> elements, each of those elements has an id attribute identical to what the filename would be if the section was in an individual file, without the trailing extension.

    <body epub:type="bodymatter z3998:fiction"> <article id="the-fox-and-the-grapes" epub:type="se:short-story"> <h2 epub:type="title">The Fox and the Grapes</h2> <p>...</p> </article> <article id="the-goose-that-laid-the-golden-eggs" epub:type="se:short-story"> <h2 epub:type="title">The Goose That Laid the Golden Eggs</h2> <p>...</p> </article> </body>

id attributes of other elements

  1. id attributes are generally used to identify parts of the document that a reader may wish to navigate to using a hash in the URL. That generally means major structural divisions. Therefore, elements that are not <section> or <article> elements do not have an id attribute, unless a part of the ebook, like an endnote, refers to a specific point in the book, and a direct link is desirable.

  2. id attributes are not used as hooks for CSS styling.

  3. If an element that is not a <section> or <article> requires an id attribute, the attribute’s value is the name of the element followed by -N, where N is the sequence number of the element starting at 1.

    <p>See <a href="#p-4">this paragraph</a> for more details.</p> <p>...</p> <p>...</p> <p id="p-4">...</p> <p>...</p>
  4. id attributes on elements that do not have an epub:type of division, volume, or part are unique across the entire ebook.

    1. If an element requires an id attribute that would conflict with one in a different file, the id attribute of the closest parent sectioning element, followed by -, is prepended to each id attribute to differentiate them.

      <!-- chapter-1.xhtml --> <section id="book-1" epub:type="division"> <section id="chapter-1" epub:type="chapter"> <p id="p-1">...</p> </section> </section> <!-- chapter-2.xhtml --> <section id="book-1" epub:type="division"> <section id="chapter-2" epub:type="chapter"> <p id="p-1">...</p> </section> </section>
      <!-- chapter-1.xhtml --> <section id="book-1" epub:type="division"> <section id="chapter-1" epub:type="chapter"> <p id="chapter-1-p-1">...</p> </section> </section> <!-- chapter-2.xhtml --> <section id="book-1" epub:type="division"> <section id="chapter-2" epub:type="chapter"> <p id="chapter-2-p-1">...</p> </section> </section>
    2. Elements that have an epub:type of division, volume, or part may share id attributes across the ebook, and if they do, they are assumed to be the same element when an ebook is recomposed.

class attributes

Classes denote a group of elements sharing a similar style.

  1. Classes are not used as single-use style hooks. There is almost always a way to compose a CSS selector to select a single element without the use of a one-off class.

    .business-card{ border: 1px solid; padding: 1em; }
    <body epub:type="bodymatter z3998:fiction"> <section epub:type="chapter"> <p>...</p> <p>...</p> <p>...</p> <p>...</p> <blockquote class="business-card"> <p>John Doe, <abbr class="eoc">Esq.</abbr></p> </blockquote> </section> </body>
    #chapter-3 blockquote{ border: 1px solid; padding: 1em; }
    <body epub:type="bodymatter z3998:fiction"> <section id="chapter-3" epub:type="chapter"> <p>...</p> <p>...</p> <p>...</p> <p>...</p> <blockquote> <p>John Doe, <abbr class="eoc">Esq.</abbr></p> </blockquote> </section> </body>
  2. Classes are used to style a recurring class of elements, i.e. a class of element that appears more than once in an ebook.

    .business-card{ border: 1px solid; padding: 1em; }
    <body epub:type="bodymatter z3998:fiction"> <section id="chapter-3" epub:type="chapter"> <p>...</p> <p>...</p> <blockquote class="business-card"> <p>Jane Doe, <abbr class="eoc">Esq.</abbr></p> </blockquote> <p>...</p> <p>...</p> <blockquote class="business-card"> <p>John Doe, <abbr class="eoc">Esq.</abbr></p> </blockquote> </section> </body>
  3. Class names describe what they are styling semantically, not the actual style the class is applying.

    .black-border{ border: 1px solid; padding: 1em; }
    .business-card{ border: 1px solid; padding: 1em; }

xml:lang attributes

  1. When words are required to be pronounced in a language other than English, the xml:lang attribute is used to indicate the IETF language tag in use.

    1. The xml:lang attribute is used even if a word is not required to be italicized. This allows screen readers to understand that a particular word or phrase should be pronounced in a certain way. A <span xml:lang="TAG"> element is used to wrap text that has non-English pronunciation but that does not need further visual styling.

    2. The xml:lang attribute is included in any word that requires special pronunciation, including names of places and titles of books.

    She opened the book titled <i epub:type="se:name.publication.book" xml:lang="la">Mortis Imago</i>.
    1. The xml:lang attribute is applied to the highest-level element possible. If italics are required and moving the xml:lang attribute would also remove an <i> element, the parent element can be styled with body [xml|lang]{ font-style: italic; }.

    <blockquote> <p><i xml:lang="es">“¿Cómo estás?”, él preguntó.</i></p> <p><i xml:lang="es">“Bien, gracias,” dijo ella.</i></p> </blockquote>
    <blockquote xml:lang="es"> <p>“¿Cómo estás?”, él preguntó.</p> <p>“Bien, gracias,” dijo ella.</p> </blockquote>

The <title> element

  1. The <title> element contains an appropriate description of the local file only. It does not contain the book title.

  2. The value of the title element is determined by the algorithm used to determine the file's ToC entry, except that no XHTML tags are allowed in the <title> element.

Headers

  1. <header> elements have at least one direct child block-level element. This is usually a <p> element, but not necessarily.

Ordered/numbered and unordered lists

  1. All <li> children of <ol> and <ul> elements have at least one direct child block-level element. This is usually a <p> element, but not necessarily; for example, a <blockquote> element might also be appropriate.

    <ul> <li>Don’t forget to feed the pigs.</li> </ul>
    <ul> <li> <p>Don’t forget to feed the pigs.</p> </li> </ul>

Tables

  1. <table> elements have a direct child <tbody> element.

    <table> <tr> <td>1</td> <td>2</td> </tr> </table>
    <table> <tbody> <tr> <td>1</td> <td>2</td> </tr> </tbody> </table>
  2. <table> elements may have an optional direct child <thead> element, if a table heading is desired.

    1. <th> elements are used in <thead> elements, instead of <td>.

    2. <th> elements only appear in <thead> elements, unless they contain the scope attribute set to either row or rowspan. The scope attribute set to those values may be used to semantically identify a table header which applies to a horizontal row instead of a vertical column.

  3. <table> elements that are used to display tabular numerical data, for example columns of sums, have CSS styling for tabular numbers: { font-variant-numeric: tabular-nums; }.

    table td:last-child{ text-align: right; font-variant-numeric: tabular-nums; }
    <table> <tbody> <tr> <td>Amount 1</td> <td>100</td> </tr> <tr> <td>Amount 2</td> <td>300</td> </tr> <tr> <td>Total</td> <td>400</td> </tr> </tbody> </table>
  4. <table> elements that are not used to format plays/dramas, and that do not otherwise inherit a visible margin (for example, they are not children of <blockquote>), have { margin: 1em; } or { margin: 1em auto 1em auto; }.

Blockquotes

  1. <blockquote> elements must contain at least one block-level child, like <p>.

  2. Blockquotes that have a citation include the citation as a direct child <cite> element.

    <blockquote> <p>“All things are ready, if our mind be so.”</p> <cite><i epub:type="se:name.publication.play">Henry <span epub:type="z3998:roman">V</span></i></cite> </blockquote>

Definition lists

Definition lists, i.e. combinations of the <dl>, <dt>, and <dd> elements, are often found in glossaries.

See here for glossaries.

  1. <dd> elements have at least one direct child block-level element. This is usually a <p> element, but not necessarily.

CSS rules

  • text-align: initial; is used instead of text-align: left; whenever it's necessary to explicitly set left-aligned text. This allows the reading system to opt to use text-align: justify; if the user prefers.