css for ebooks

100
CSS & Ebooks: Rachel Andrew, ConFoo 2015

Upload: rachel-andrew

Post on 15-Jul-2015

728 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: CSS for Ebooks

CSS & Ebooks: Rachel Andrew, ConFoo 2015

Page 2: CSS for Ebooks

CSS and EBooksRachel Andrew

http://rachelandrew.co.uk

@rachelandrew

Page 3: CSS for Ebooks

24ways.org/newsletter

Page 4: CSS for Ebooks

Why write books in HTML & CSS?

Page 5: CSS for Ebooks

EBooks Formats

• EPUB - used by iBooks and other readers

• MOBI / KF8 - used by Kindles

• PDF - readable everywhere and can also be printed

Page 6: CSS for Ebooks

Under the hood ...

• EPUB - HTML & CSS

• MOBI / KF8 - HTML and CSS

• PDF

Page 7: CSS for Ebooks

Two of our formats are actually HTML and CSS under the hood.

Page 8: CSS for Ebooks

A familiar toolset

Page 9: CSS for Ebooks

Write once, output everywhere

Page 10: CSS for Ebooks

Automating Builds

Page 11: CSS for Ebooks

The Basics of CSS for Books

Page 12: CSS for Ebooks

CSS Paged Media Module http://www.w3.org/TR/css3-page/

Page 13: CSS for Ebooks

The @page rule

Page 14: CSS for Ebooks

Setting the page size within the @page rule.

@page { size: 5.5in 8.5in; }

Page 15: CSS for Ebooks

You can use size keywords in addition to specifying page dimensions.

@page { size: A4; }

Page 16: CSS for Ebooks

Keywords for page orientation - portrait or landscape.

@page { size: A4 landscape; }

Page 17: CSS for Ebooks

The Page Model

Page 18: CSS for Ebooks

Your content goes into the Page Area. The margin is divided into 16 margin boxes and can accept

generated content.

Page 19: CSS for Ebooks

http://www.w3.org/TR/css3-page/#margin-boxes

Page 20: CSS for Ebooks

Adding a footer to a printed document.

@page:right{ @bottom-left { margin: 10pt 0 30pt 0; content: "My Book Title"; font-size: 8pt; color: #000; } }

Page 21: CSS for Ebooks

Spread pseudo-classes

Page 22: CSS for Ebooks

The :left and :right pseudo-class selectors

Page 23: CSS for Ebooks

:left and :right pseudo-class selectors

@page :left { margin-left: 3cm; }

@page :right { margin-left: 4cm; }

Page 24: CSS for Ebooks

The :first pseudo-class selector targets the first page in a printed document.

@page :first {

}

Page 25: CSS for Ebooks

The :blank pseudo-class selector

@page :blank { @top-center { content: "This page is intentionally left blank" } }

Page 26: CSS for Ebooks

Media Queries

Page 27: CSS for Ebooks

Media Queries and paged media.

@media print and (width: 21cm) and (height: 29.7cm) { @page { margin: 3cm; } }

Page 28: CSS for Ebooks

Using the amzn-kf8 keyword along with size media queries to target a specific device. @media amzn-kf8

and (device-height:1024px) and (device-width: 758px), amzn-kf8 and (device-height:758px) and (device-width: 1024px) { /* Styles for Paperwhites can go here */ }

Page 29: CSS for Ebooks

Numbering things

Page 30: CSS for Ebooks

Page Numbering

Page 31: CSS for Ebooks

CSS Counters http://www.w3.org/TR/CSS21/generate.html#counters

Page 32: CSS for Ebooks

Using a CSS Counter.

article { counter-reset: para; }

article p:after { counter-increment: para; content: "Paragraph: " counter(para); }

Page 33: CSS for Ebooks

The predefined page counter always stores the value of the current page.

counter(page);

Page 34: CSS for Ebooks

Adding the page count to the bottom right of right-hand pages and bottom left of left-hand pages.

@page:right{ @bottom-right { content: counter(page); } }

@page:left{ @bottom-left { content: counter(page); } }

Page 35: CSS for Ebooks

The page counter can be reset and incremented.

@page { counter-increment: page 2; }

Page 36: CSS for Ebooks

The pages counter holds the total number of pages in the document.

@page:left{ @bottom-left { content: "Page " counter(page) " of " counter(pages); } }

Page 37: CSS for Ebooks

In my case an h1 with a class of chapter indicates a new chapter.

body { counter-reset: chapternum; }

h1.chapter:before { counter-increment: chapternum; content: counter(chapternum) ". "; }

Page 38: CSS for Ebooks

Counting chapters and figures. body {

counter-reset: chapternum figurenum; }

h1 { counter-reset: figurenum; }

h1.title:before { counter-increment: chapternum; content: counter(chapternum) ". "; }

figcaption:before { counter-increment: figurenum; content: counter(chapternum) "-" counter(figurenum) ". "; }

Page 39: CSS for Ebooks

Setting Strings

Page 40: CSS for Ebooks

CSS Generated Content for Paged Media Module

http://www.w3.org/TR/css-gcpm-3/

Page 41: CSS for Ebooks

Taking the content of the h1 and using it in generated content in the page header.

h1 { string-set: doctitle content(); }

@page :right { @top-right { content: string(doctitle); margin: 30pt 0 10pt 0; font-size: 8pt; } }

Page 42: CSS for Ebooks

Page breaks

Page 43: CSS for Ebooks

Making h1.title always start a new page.

h1.title { page-break-before: always; }

Page 44: CSS for Ebooks

Do not break directly after a heading.

h1,h2,h3,h4,h5 { page-break-after: avoid; }

Page 45: CSS for Ebooks

Avoid breaking inside figures and tables.

table, figure { page-break-inside: avoid; }

Page 46: CSS for Ebooks

Cross-references

Page 47: CSS for Ebooks

An internal link in my document. It has a class of xref and a link to the id applied to my chapter 1 heading.

<a class="xref" href="#ch1" title="Chapter 1">Chapter 1</a>

Page 48: CSS for Ebooks

We use the target-counter value to indicate the page number that the target location is on and output this with generated content.

a.xref:after { content: " (page " target-counter(attr(href, url), page) ")"; }

Page 49: CSS for Ebooks

Using our knowledge in the real world

Page 50: CSS for Ebooks

https://github.com/rachelandrew/css-books

Page 51: CSS for Ebooks

Creating an EPUB

Page 52: CSS for Ebooks

http://johnmacfarlane.net/pandoc/

Page 53: CSS for Ebooks

Book metadata.

<dc:title id="t1">Productivity Essays</dc:title> <dc:language>en-GB</dc:language> <dc:creator opf:file-as="Andrew, Rachel" opf:role="aut">Rachel Andrew</dc:creator> <dc:publisher>Rachel Andrew</dc:publisher> <dc:date opf:event="publication">2014-07-11</dc:date> <dc:rights>Copyright ©2014 by Rachel Andrew</dc:rights>

Page 54: CSS for Ebooks

Run pandoc at the commandline.

> pandoc -o builds/book.epub book.html --epub-metadata=metadata.xml --toc --toc-depth=2

Page 55: CSS for Ebooks

• -o builds/book.epub = the output file

• book.html = my chapters

• --epub-metadata=metadata.xml = my metadata file

• --toc --toc-depth=2 = generate a table of contents two levels deep

Page 56: CSS for Ebooks
Page 57: CSS for Ebooks

--epub-cover-image=cover.png

To add a cover image, set it as a parameter when building your book.

Page 58: CSS for Ebooks

Including a CSS file.

--epub-stylesheet=my-stylesheet.css

Page 59: CSS for Ebooks
Page 60: CSS for Ebooks

Basic formatting added by pandoc.

body { margin: 5%; text-align: justify; font-size: medium; } code { font-family: monospace; } h1 { text-align: left; } h2 { text-align: left; } h3 { text-align: left; } h4 { text-align: left; } h5 { text-align: left; } h6 { text-align: left; } h1.title { } h2.author { } h3.date { } ol.toc { padding: 0; margin-left: 1em; } ol.toc li { list-style-type: none; margin: 0; padding: 0; }

Page 61: CSS for Ebooks

The title page is generated from meta tags in the source file.

<title>Productivity Essays</title> <meta name="author" content="Rachel Andrew"/> <meta name="date" content="2014-07-15"/>

Page 62: CSS for Ebooks

Page breaks

Page 63: CSS for Ebooks

Managing page breaks in an EPUB.

h1,h2,h3,h4,h5 { font-weight: bold; page-break-after: avoid; page-break-inside:avoid; text-align: left; }

h1+p, h2+p, h3+p { page-break-before: avoid; }

table, figure { page-break-inside: avoid; }

Page 64: CSS for Ebooks

Use CSS to format the text of your EPUB - with care!

Page 65: CSS for Ebooks

Custom fonts can be included in your pandoc build and used just like a font on the web.

--epub-embed-font=FILE

Page 66: CSS for Ebooks
Page 67: CSS for Ebooks

http://sigil-ebook.com/

Page 68: CSS for Ebooks

Validating EPUB files http://validator.idpf.org/

Page 69: CSS for Ebooks

Creating a MOBI

Page 70: CSS for Ebooks

The Kindlegen Tool http://www.amazon.com/gp/feature.html?docId=1000765211

Page 71: CSS for Ebooks

Use an EPUB file as input for the kindlegen tool.

> /Applications/KindleGen/kindlegen book.epub

Page 72: CSS for Ebooks
Page 73: CSS for Ebooks

CSS and the Kindle

Page 74: CSS for Ebooks

Use a Media Query to target Kindles that support KF8, and more CSS.

@media amzn-kf8

}

Page 75: CSS for Ebooks

Section 3.1.1 Amazon Publishing Guidelines

“The body text in a reflowable Kindle book (fiction and non-fiction) must be all defaults. Amazon encourages content creators to use creative styles for headings, special paragraphs, footnotes, tables of contents, etc., but not for body text. The reason for this is that any styling on body text in the HTML will override the user’s preferred default reading settings. Users report such behavior as a poor reading experience.”

Page 76: CSS for Ebooks

Amazon Publishing Guidelines kindlegen.s3.amazonaws.com/AmazonKindlePublishingGuidelines.pdf

Page 77: CSS for Ebooks

Creating a PDF

Page 78: CSS for Ebooks

Generating a PDF is more complicated than you might think.

Page 79: CSS for Ebooks

princexml.com

Page 80: CSS for Ebooks

Creating a PDF with Prince

> prince book.html -o book.pdf

Page 81: CSS for Ebooks
Page 82: CSS for Ebooks

In a new CSS file set a page size.

@page { size: 5.5in 8.5in; margin: 50pt 60pt 70pt; }

Page 83: CSS for Ebooks

The -s option is how you pass in a CSS file when building your book.

> prince -s ebook-styles.css book.html -o book.pdf

Page 84: CSS for Ebooks
Page 85: CSS for Ebooks

Adding page numbers.

@page:right{ @bottom-right { margin: 10pt 0 30pt 0; border-top: .25pt solid #000; content: counter(page); font-size: 9pt; } } @page:left { @bottom-left { margin: 10pt 0 30pt 0; border-top: .25pt solid #000; content: counter(page); font-size: 9pt; } }

Page 86: CSS for Ebooks
Page 87: CSS for Ebooks

Using generated content to add the book title to each page.

@bottom-left { margin: 10pt 0 30pt 0; border-top: .25pt solid #666; content: "Productivity Essays"; font-size: 9pt; color: #333; }

Page 88: CSS for Ebooks

Using string-set to put the chapter title into the top of the page.

h1 { string-set: doctitle content(); }

@top-right { content: string(doctitle); margin: 30pt 0 10pt 0; font-size: 9pt; color: #333; }

Page 89: CSS for Ebooks

Using the page-break-before property to break h1 headings to a new page.

h1 { page-break-before: always; } h1,h2,h3,h4,h5 { font-weight: bold; page-break-after: avoid; page-break-inside:avoid; }

h1+p, h2+p, h3+p { page-break-before: avoid; }

table, figure { page-break-inside: avoid; }

Page 90: CSS for Ebooks

Adding the chapter number before each chapter.

body { counter-reset: chapternum; }

h1.chapter:before { counter-increment: chapternum; content: counter(chapternum) ". "; }

Page 91: CSS for Ebooks

Creating cross references.

a.xref:after { content: " (page " target-counter(attr(href, url), page) ")"; }

Page 92: CSS for Ebooks

My table of contents is a separate HTML document.

<h1>Productivity Essays</h1> <ul class="toc"> <li><a href="book.html#ch1">Your email inbox is not your to-do list</a></li> <li><a href="book.html#ch2">GTD and OmniFocus 2 - my productivity workflow</a></li> <li><a href="book.html#ch3">How to become good at estimating time</a></li> </ul>

Page 93: CSS for Ebooks

We are using target-counter as before and also setting a leader.

ul.toc a::after { content: leader('.') target-counter(attr(href), page); }

Page 94: CSS for Ebooks

Pass the toc.html file to Prince to be added to the front of the book.

> prince -s pdf-styles.css toc.html book.html book.pdf

Page 95: CSS for Ebooks
Page 96: CSS for Ebooks

Resources and further reading

Page 97: CSS for Ebooks

Samples and Demos

Starting point HTML and CSS plus outputs generated from those starting points can be found on Github. https://github.com/rachelandrew/css-books

Page 98: CSS for Ebooks

More on CSS for Print & PDF

My Smashing Magazine article, going into more detail on CSS for print and PDF output. http://www.smashingmagazine.com/2015/01/07/designing-for-print-with-css

https://github.com/rachelandrew/css-for-print

Page 99: CSS for Ebooks

Ebook Resources

http://rachelandrew.co.uk/presentations/css-books

Page 100: CSS for Ebooks

Thank you

Rachel Andrew

@rachelandrew

[email protected]

http://rachelandrew.co.uk/presentations/css-books