Tag Archives: ebook formatting

Elements of Style: CSS for Ebooks

I originally published this post on Joel Friedlander's wonderful resource for self-publishers, TheBookDesigner.com

If HTML is the blueprint, showing how an ebook (or a web page) should be laid out, Cascading Style Sheets (CSS) are the interior design, saying how everything should look. While that may sound superficial, in fact learning to use CSS can have a profound impact on your ebook.

This is a somewhat complicated topic, so I am going to take three posts to cover it. This time round, I’m going to show you what CSS is and how to apply it. In the second post, I’m going to look at some of the different properties that you can use to define how your ebook looks. And in the last post, I’m going to talk about how to know which rules take precedence when.

CSS was created to define the presentation style to any XML document — but it's most common use is in conjunction with the HTML in web pages and, of course, in ebooks. It's what allows you to add color, to change fonts and (within reason) typefaces, to define where and how images display, and much more.

The Rule of Law

All CSS really comes down to is a series of rules that define how a particular element (or kind of element) will look when an ereader displays it.[1]

Each rule has two parts:

  1. A selector, which can be:
  • One or more type of HTML element (that is, any instance of a particular tag: <p>, <body>, <em>, etc.)
  • One or more class (that is, any elements that have been given the attribute class="whatever")
  • An element with a particular ID (that is, having the attribute id="whatever")
  1. A declaration — that is to say, one or more properties defining how the selector(s) should display

So here's an example from the stylesheet file Styles/Styles.css in the "White Robes" ebook that I linked to a couple of posts back:

h1, .Text-break {color:darkred;}

That's a lot simpler than it might at first look.

The part before the curly brackets ({}) marks the selector(s); there are two here, separated by a comma:

  • h1: If you look back to my last post, you'll see that h1 marks the top-level section head — usually a chapter title.
  • .Text-break: This is a class — the name of a particular style. You can tell because of the period at the beginning. [2]

Sometimes you see these selectors combined. I could have marked the second one p.Text-break, because in fact it's a style that I apply to whole paragraphs — in the case of "White Robes," paragraphs that contain a single em-dash to be used as a separator between sections of text. But I don't have to say what kind of element the class needs to be applied to (although that can sometimes be useful). By leaving the p off before the period, I could apply the same rule to other kinds of elements, if I wanted.

The part inside the curly brackets is the actual rule — the declaration. It contains a color property that tells the ereader to turn sections that match those selectors dark red. Simple, right?
Since there can be more than one rule per declaration, each declaration needs to end in a semicolon:

h1, .Text-break {color:darkred;font-weight:bold;}

I'll get into more detail on some of the various rules that you care about in another post — sorry, this one is already complicated enough!

Location, location, location

Now there are three places you can place a CSS rule:

  1. Inline as part of an HTML tag
  2. In a <style> tag inside the <head> at the top of the HTML file
  3. In a linked CSS file, either in the ebook itself or on the open internet

Inline Style

If you want to apply a CSS style to just one HTML tag, you can do it inline by adding a style attribute to an HTML tag.

For example, if I were to want to center a particular paragraph, I could add style="text-align:center" to the <p> tag, like so:

<p style="text-align:center;">

(Remember: those quotes must be "straight," not “smart” or “curly.”)

Likewise, if I were to want to make a particular section (or <span>) of text in Garamond, I would do this:

Here is a phrase displayed as <span style="font-family:Garamond;">Garamond</span>.

Now, that would only work if Garamond were actually installed on the ereader or embedded in the ebook, so I added the generic serif, which tells the ereader to use its default serif typeface.

Notice that I don't use a selector when using the style attribute. Since the declaration is inside a tag, it's clear what the rule refers to. Make sense?

<style> Tag

You can do all sorts of wonderful things when you apply styles inline — but when you do, they will only apply to those tags where they're added, and they will always apply there, until you search through and change them. (I know, setting all of the text to orange Comic Sans seemed like such a good idea!)

So how can you make your CSS styles more powerful and easier to edit?

Use global styles. These are the equivalent of the styles used in word-processing and page-layout apps — changing the style changes every part of the document to which the style has been applied.

There are two ways to do this in any HTML document — including one of the files that makes up your ebook.

The first is to add a <style></style> block to your file. As I said, it is placed in the <head> section of the file, and would look something like this:

<html>
<head>
<style type="text/css">
h1, .Text-break {color:darkred;}
p {color:black;}
#Green {color:green;}
</style>
</head>
<body>
<h1>This header is red</h1>
<p>This paragraph is black.</p>
<p class="Text-break">This paragraph is red!</p>
<p id="Green">This paragraph is green.</p>
</body>
</html>

Notice that the <style> tag should always include the attribute type="text/css." That tells the ereader that this is standard CSS.

That would display this way:

Now, notice that in addition to styling the <p> element black and the h1 element and the Text-break class red, I styled the paragraph with the id attribute Green as… you know, green.

Just as a period before the name tells us that the selector is a class, the pound sign (#) before the name lets us know that the selector is an ID. So in the style tag above, that looked like this:

#Green {color:green;}

By having a single set of declarations in the header, you can save yourself the trouble of adding the same rule over and over again, and then having to change every instance of it if you decide, for example, to make the .Text-break blue instead of red.

CSS Style Sheet

The third way to add style to your ebook’s HTML pages is to create one or more style sheets and link to them in the <head> section of each page.

This works just like having a <style></style> section at the top of your chapter — except the rules will apply not just in one file, but in all of them. This is especially important if you’re working on a longer, multi-chapter ebook.

First create an empty text file with the file type .css. In the “White Robes” ebook, I created the file Styles.css in the directory/folder Styles:

You can also create the file in whatever editing software you’re using. In Sigil, go to the File menu, and then select Add>Blank Stylesheet.

The contents of the file looks almost exactly like the <style type="text/css"> section I showed you above. Here’s the style sheet from “White Robes”:

Some of those rules might make sense to you; most of them won’t. Don’t worry; the next post will be a primer on some of the most important rules and properties.

Oh — notice that each property in the declaration section (the part inside the curly brackets) is on its own line. Just as in HTML, those line breaks are meaningless. What separates one property from the next is, as I said above, a semicolon. So Sigil puts each rule on its own line for no reason other than making it easier to read.

Also, something important: in CSS, the rule closer to the bottom has priority. So notice in the third rule that says that a whole bunch of different kinds of HTML elements, including the body element will have no margin, no padding, and no border width.[3]

Then the very next rule sets the padding (that is, the space inside the edge of the element before words and images can show up) to five pixels.[4]

Because the second <body> declaration is closer to the bottom, it takes priority. The rule means that every page in the ebook will have a thin white space around the outside edge of the screen or window.

(Prioritization — which rules apply in which cases — is a complicated issue. I will get to it in the third and final post on CSS.)

Okay, so you’ve got a style sheet. But if it’s not linked from the XHTML file, those files won’t show up at all!

So you’ve got to add a <link> element to the <header> block of each XHTML file (that is, chapter) in your ebook to which you want to style sheet to apply.

The link element looks like this:

<link href="../Styles/Styles.css" rel="stylesheet" type="text/css"/>

That tells the ereader to look for the file Styles.css in the directory Styles (just where I put it); it tells the ereader that the relationship between the files is that Styles.css is a style sheet for the current XHTML file, and that’s it’s a CSS file.

Once you’ve added that link, all of the formatting that you’ve defined in the sheet will be applied to your now-beautiful chapters.

So, that’s our quick introduction to CSS.

Next time, I’ll run through some of the most important properties that you can define using CSS, and after that, I’ll finish up with a quick overview of style priorities.

Like this post? Sign up for my newsletter, and you’ll get my ebook An Indie Publisher’s Intro to Ebooks for free!


[1] Remember: an element is a block of HTML, usually set off at the beginning and end by open and close tags, like this: <tag>[stuff]</tag>

[2] By the way, you can apply multiple classes to a single element — just separate them with a space: class="red small-caps”. Two things to keep in mind: First, the later class will take precedence, so if I added a class attribute with the values red and blue, the text would display as blue, since the blue class came last. Second, some older ereaders don’t handle multiple classes well (I’m looking at you, old-style Kindle). I usually try to stick to one class per element for that reason.

[3] You’ll often see a rule like this at (or near) the top of a style sheet; it’s called zeroing out or initialization. It makes sure that there isn’t any carry-over from the ereader’s default settings or other styles.

[4] Yeah, it looks like it’s stuttering; the four 5px values apply to the top, right, bottom, and left padding, in that order. That allows you to customize the spacing between elements quite a lot.