SlideShare a Scribd company logo
CSS & Ebooks: Rachel Andrew, ConFoo 2015
CSS and EBooks
Rachel Andrew

http://rachelandrew.co.uk



@rachelandrew
24ways.org/newsletter
Why write books in HTML & CSS?
EBooks Formats
• EPUB - used by iBooks and other readers
• MOBI / KF8 - used by Kindles
• PDF - readable everywhere and can also be
printed
Under the hood ...
• EPUB - HTML & CSS
• MOBI / KF8 - HTML and CSS
• PDF
Two of our formats are actually
HTML and CSS under the hood.
A familiar toolset
Write once, output everywhere
Automating Builds
The Basics of CSS for Books
CSS Paged Media Module
http://www.w3.org/TR/css3-page/
The @page rule
Setting the page
size within the
@page rule.
@page {
size: 5.5in 8.5in;
}
You can use size
keywords in
addition to
specifying page
dimensions.
@page {
size: A4;
}
Keywords for
page orientation -
portrait or
landscape.
@page {
size: A4 landscape;
}
The Page Model
Your content goes into the Page
Area. The margin is divided into
16 margin boxes and can accept
generated content.
http://www.w3.org/TR/css3-page/#margin-boxes
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;
}
}
Spread pseudo-classes
The :left and :right pseudo-class
selectors
:left and :right
pseudo-class
selectors
@page :left {
margin-left: 3cm;
}
@page :right {
margin-left: 4cm;
}
The :first pseudo-
class selector
targets the first
page in a printed
document.
@page :first {
}
The :blank
pseudo-class
selector
@page :blank {
@top-center { content: "This page
is intentionally left blank" }
}
Media Queries
Media Queries
and paged media.
@media print and (width: 21cm) and
(height: 29.7cm) {
@page {
margin: 3cm;
}
}
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 */
}
Numbering things
Page Numbering
CSS Counters
http://www.w3.org/TR/CSS21/generate.html#counters
Using a CSS
Counter.
article {
counter-reset: para;
}
article p:after {
counter-increment: para;
content: "Paragraph: " counter(para);
}
The predefined
page counter
always stores the
value of the
current page.
counter(page);
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);
}
}
The page counter
can be reset and
incremented.
@page {
counter-increment: page 2;
}
The pages
counter holds the
total number of
pages in the
document.
@page:left{
@bottom-left {
content: "Page " counter(page) "
of " counter(pages);
}
}
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) ". ";
}
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) ". ";
}
Setting Strings
CSS Generated Content for Paged
Media Module
http://www.w3.org/TR/css-gcpm-3/
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 breaks
Making h1.title
always start a
new page.
h1.title {
page-break-before: always;
}
Do not break
directly after a
heading.
h1,h2,h3,h4,h5 {
page-break-after: avoid;
}
Avoid breaking
inside figures and
tables.
table, figure {
page-break-inside: avoid;
}
Cross-references
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>
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) ")";
}
Using our knowledge in the real
world
https://github.com/rachelandrew/css-books
Creating an EPUB
http://johnmacfarlane.net/pandoc/
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>
Run pandoc at
the commandline.
> pandoc -o builds/book.epub book.html --
epub-metadata=metadata.xml --toc --toc-
depth=2
• -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
CSS for Ebooks
--epub-cover-image=cover.png
To add a cover
image, set it as a
parameter when
building your
book.
Including a CSS
file.
--epub-stylesheet=my-stylesheet.css
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; }
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 breaks
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;
}
Use CSS to format the text of your
EPUB - with care!
Custom fonts can
be included in
your pandoc build
and used just like
a font on the
web.
--epub-embed-font=FILE
CSS for Ebooks
http://sigil-ebook.com/
Validating EPUB files
http://validator.idpf.org/
Creating a MOBI
The Kindlegen Tool
http://www.amazon.com/gp/feature.html?docId=1000765211
Use an EPUB file
as input for the
kindlegen tool.
> /Applications/KindleGen/kindlegen book.epub
CSS for Ebooks
CSS and the Kindle
Use a Media
Query to target
Kindles that
support KF8, and
more CSS.
@media amzn-kf8
}
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.”
Amazon Publishing Guidelines
kindlegen.s3.amazonaws.com/AmazonKindlePublishingGuidelines.pdf
Creating a PDF
Generating a PDF is more
complicated than you might think.
princexml.com
Creating a PDF
with Prince
> prince book.html -o book.pdf
CSS for Ebooks
In a new CSS file
set a page size.
@page {
size: 5.5in 8.5in;
margin: 50pt 60pt 70pt;
}
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
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;
}
}
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;
}
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;
}
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;
}
Adding the
chapter number
before each
chapter.
body {
counter-reset: chapternum;
}
h1.chapter:before {
counter-increment: chapternum;
content: counter(chapternum) ".
";
}
Creating cross
references.
a.xref:after {
content: " (page " target-
counter(attr(href, url), page) ")";
}
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>
We are using
target-counter as
before and also
setting a leader.
ul.toc a::after {
content: leader('.') target-
counter(attr(href), page);
}
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
CSS for Ebooks
Resources and further reading
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
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
Ebook Resources
http://rachelandrew.co.uk/presentations/css-books
Thank you
Rachel Andrew
@rachelandrew
me@rachelandrew.co.uk
http://rachelandrew.co.uk/presentations/css-books

More Related Content

PPTX
ο παπουτσωμένος χιονόδρακος
PPTX
με το σεις και με το σας (2), σελ.24-25
PPTX
μην αγγίζετε! έχει αγκάθια!
PDF
ασκήσεις παρουσίασης
PPTX
γραφή της πρόσθεσης με τη χρήση συμβόλων, κεφ.14
PPTX
δώρα για την ιωάννα
PPTX
αφαίρεση με αφαιρετέο μικρό αριθμό, κεφ.28
PPTX
βόλτα στο βουνό(1)
ο παπουτσωμένος χιονόδρακος
με το σεις και με το σας (2), σελ.24-25
μην αγγίζετε! έχει αγκάθια!
ασκήσεις παρουσίασης
γραφή της πρόσθεσης με τη χρήση συμβόλων, κεφ.14
δώρα για την ιωάννα
αφαίρεση με αφαιρετέο μικρό αριθμό, κεφ.28
βόλτα στο βουνό(1)

What's hot (18)

PPT
ΓΛΩΣΣΑ Δ ΔΗΜΟΤΙΚΟΥ Β ΤΕΥΧΟΣ
PPTX
Fasteners.pptx
PPTX
Τα σήματα οδικής κυκλοφορίας.
PDF
παιχνίδια που ταξιδεύουν
DOCX
αντίο θάλασσα 1
DOC
προγραφικες 2, λ και χ
DOCX
Μέμα- Επανάληψη ως και το Λ
PPTX
με πινέλα και φαντασία
PDF
ενας καινούριος μαθητής
DOCX
στο δρόμο για το σχολείο (3)
PPTX
πού είναι ο άρης (6),ε , λα ,ελα σελ. 16,17
DOC
πεπονι πεπονι 1
DOCX
αντίο θάλασσα 2
PPTX
παιχνίδια που ταξιδεύουν
PPTX
παιχνίδια που ταξιδεύουν
DOCX
π α πα
PPTX
Μαθαίνω να γράφω τα γράμματα της αλφαβήτας. (https://blogs.sch.gr/sfaira-sti-...
PPTX
παίζουμε παντομίμα;
ΓΛΩΣΣΑ Δ ΔΗΜΟΤΙΚΟΥ Β ΤΕΥΧΟΣ
Fasteners.pptx
Τα σήματα οδικής κυκλοφορίας.
παιχνίδια που ταξιδεύουν
αντίο θάλασσα 1
προγραφικες 2, λ και χ
Μέμα- Επανάληψη ως και το Λ
με πινέλα και φαντασία
ενας καινούριος μαθητής
στο δρόμο για το σχολείο (3)
πού είναι ο άρης (6),ε , λα ,ελα σελ. 16,17
πεπονι πεπονι 1
αντίο θάλασσα 2
παιχνίδια που ταξιδεύουν
παιχνίδια που ταξιδεύουν
π α πα
Μαθαίνω να γράφω τα γράμματα της αλφαβήτας. (https://blogs.sch.gr/sfaira-sti-...
παίζουμε παντομίμα;
Ad

Similar to CSS for Ebooks (20)

PDF
The beauty behind ebooks: CSS - ebookcraft 2015 - Iris Febres
PDF
How to Think Inside the Box: Programming Fixed Layout for E-Books
PPTX
Making Your Site Printable: Booster Conference
PPTX
Introduction to PrintCSS.live
PPTX
Responsive web design
PPTX
Making Your Site Printable: CSS Summit 2014
PDF
Maintainable CSS
PPT
Unit 2 (html)
PDF
Article css
PDF
PPTX
Castro Chapter 11
PPTX
The Third Screen: Using HTML+CSS to format for Print
PPT
Html & CSS - Best practices 2-hour-workshop
PPTX
Making your site printable: WordCamp Buffalo 2013
PPTX
PPT
Web Design-III IT.ppt
PPT
Web Design.ppt
PDF
Dynamic User Interfaces for Desktop and Mobile
PDF
Girl Develop It Cincinnati: Intro to HTML/CSS Class 4
PDF
Web Design for Literary Theorists II: Overview of CSS (v 1.0)
The beauty behind ebooks: CSS - ebookcraft 2015 - Iris Febres
How to Think Inside the Box: Programming Fixed Layout for E-Books
Making Your Site Printable: Booster Conference
Introduction to PrintCSS.live
Responsive web design
Making Your Site Printable: CSS Summit 2014
Maintainable CSS
Unit 2 (html)
Article css
Castro Chapter 11
The Third Screen: Using HTML+CSS to format for Print
Html & CSS - Best practices 2-hour-workshop
Making your site printable: WordCamp Buffalo 2013
Web Design-III IT.ppt
Web Design.ppt
Dynamic User Interfaces for Desktop and Mobile
Girl Develop It Cincinnati: Intro to HTML/CSS Class 4
Web Design for Literary Theorists II: Overview of CSS (v 1.0)
Ad

More from Rachel Andrew (20)

PDF
All Day Hey! Unlocking The Power of CSS Grid Layout
PDF
SmashingConf SF: Unlocking the Power of CSS Grid Layout
PDF
Unlocking the Power of CSS Grid Layout
PDF
The Creative New World of CSS
PDF
Into the Weeds of CSS Layout
PDF
Solving Layout Problems with CSS Grid & Friends - DevFest17
PDF
Graduating to Grid
PDF
View Source London: Solving Layout Problems with CSS Grid & Friends
PDF
DevFest Nantes - Start Using CSS Grid Layout today
PDF
Start Using CSS Grid Layout Today - RuhrJS
PDF
404.ie: Solving Layout Problems with CSS Grid & Friends
PDF
Solving Layout Problems with CSS Grid & Friends - WEBU17
PDF
Laying out the future with grid & flexbox - Smashing Conf Freiburg
PDF
Solving Layout Problems with CSS Grid & Friends - NordicJS
PDF
Google Developers Experts Summit 2017 - CSS Layout
PDF
Web Summer Camp Keynote
PDF
New CSS Layout Meets the Real World
PDF
An Event Apart DC - New CSS Layout meets the Real World
PDF
Perch, Patterns and Old Browsers
PDF
Evergreen websites for Evergreen browsers
All Day Hey! Unlocking The Power of CSS Grid Layout
SmashingConf SF: Unlocking the Power of CSS Grid Layout
Unlocking the Power of CSS Grid Layout
The Creative New World of CSS
Into the Weeds of CSS Layout
Solving Layout Problems with CSS Grid & Friends - DevFest17
Graduating to Grid
View Source London: Solving Layout Problems with CSS Grid & Friends
DevFest Nantes - Start Using CSS Grid Layout today
Start Using CSS Grid Layout Today - RuhrJS
404.ie: Solving Layout Problems with CSS Grid & Friends
Solving Layout Problems with CSS Grid & Friends - WEBU17
Laying out the future with grid & flexbox - Smashing Conf Freiburg
Solving Layout Problems with CSS Grid & Friends - NordicJS
Google Developers Experts Summit 2017 - CSS Layout
Web Summer Camp Keynote
New CSS Layout Meets the Real World
An Event Apart DC - New CSS Layout meets the Real World
Perch, Patterns and Old Browsers
Evergreen websites for Evergreen browsers

Recently uploaded (20)

PPTX
Spectroscopy.pptx food analysis technology
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Encapsulation theory and applications.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Electronic commerce courselecture one. Pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Network Security Unit 5.pdf for BCA BBA.
Spectroscopy.pptx food analysis technology
The Rise and Fall of 3GPP – Time for a Sabbatical?
Spectral efficient network and resource selection model in 5G networks
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Unlocking AI with Model Context Protocol (MCP)
Review of recent advances in non-invasive hemoglobin estimation
NewMind AI Weekly Chronicles - August'25 Week I
Encapsulation theory and applications.pdf
Programs and apps: productivity, graphics, security and other tools
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Reach Out and Touch Someone: Haptics and Empathic Computing
Dropbox Q2 2025 Financial Results & Investor Presentation
Understanding_Digital_Forensics_Presentation.pptx
Electronic commerce courselecture one. Pdf
Advanced methodologies resolving dimensionality complications for autism neur...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Per capita expenditure prediction using model stacking based on satellite ima...
Network Security Unit 5.pdf for BCA BBA.

CSS for Ebooks