Tag Archives: ebook

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.

Speaking in Code: Ebook HTML basics

This post originally appeared on Joel Friedlander’s wonderful site, TheBookDesigner.com.

If, as I keep saying, an ebook is just a website a box, then in order to know how to get in and edit your ebook, you’re going to want to know some HTML. However you choose to work on the file, knowing the basic building blocks is essential in creating a finished  product that presents your book to its best advantage.

 

When we talk about HTML, we’re actually talking about two separate things:

Continue reading Speaking in Code: Ebook HTML basics

Weekly Roundup: 7 Interesting Articles on eBook Publishing

As a matter of principal (both personal and professional) I spend quite a bit of my time keeping up to date with what’s new in the eBook publishing world. Having built up a relatively substantial feed to scroll through, it recently occurred to me that I’m not the only one who could benefit from a list of new ePublishing articles  to peruse while I’m going about my day.

With that in mind, I’ve decided to start publishing a weekly list of recent articles I think are important, interesting, innovative, etc. Here are this week’s choices:

Continue reading Weekly Roundup: 7 Interesting Articles on eBook Publishing

Launch your summer reading with a free time-travel adventure!

Looking for a great way to launch your summer reading?

Last month, Heather Albano’s Timepiece: A Steampunk Time-Travel Adventure hit #1 on Amazon, Kobo, iBooks, Barnes and Noble, and Google Play in the Steampunk genre! It was part of a two-day promotion that was more successful than we could ever have hoped.

Well, in case you missed it, here’s your chance to get the book free! Continue reading Launch your summer reading with a free time-travel adventure!

The Convertible Cloud: Ebook Conversion Online

This is the next in my on-going series of post on ebook creation; it focusses, obviously, on ebook conversion online. It was originally posted over at Joel Friedlander’s wonderful site for indie publishers, TheBookDesigner.com

The Convertible Cloud: Ebook Conversion Online

Previously, I’ve compared some of the computer apps that you can use to convert your manuscript into an ebook.

This month I’ll talk about online conversion tools — all of the ones I’m going to discuss are attached to the retailers and distributors that you are going to be interested in.

 

Eye of the Hurricane: Top Ebook Retailers

 

Let’s start with the most popular retailers and their conversion tools (or lack thereof).

 

Once again, I’m assuming that you’re in the US — which isn’t a given, I know. (Most of this information is true for non-US publishers as well.) Also, I’m defining “manuscript” as synonymous with “Microsoft Word document” (either .doc or .docx), since that’s the most common file format for authors to work with, and that’s the format I used in comparing the desktop conversion tools.

 

As before, these are the major retailers you will probably be looking at:

Continue reading The Convertible Cloud: Ebook Conversion Online

Reserve your copy of Risuko on Kickstarter

Reserve your copy of Risuko: A Kunoichi Tale now and help support Stillpoint Digital Press’s publication of this exciting new teen historical adventure novel!

Author David Kudler has launched a crowdfunding campaign to help make Risuko the best book it can possibly be. While Risuko is nearly finished, there’s still more we’d like to do, and we need your help.

Rewards for backers include copies of the novel in all available formats — paperback, hardcover, ebook, and audiobook — at below retail cost, plus much more!

The campaign is 90% funded with three days to go — join us before it ends on Friday, March 4!

For more information, check out the campaign at http://risuko.net/kickstarter


 

Risuko 3D coverRisuko follows a young woman along the first dangerous steps to discovering who she truly is.

Though Japan has been devastated by a century of civil war, Risuko just wants to climb trees. Growing up far from the battlefields and court intrigues, the fatherless girl finds herself pulled into a plot that may reunite Japan — or may destroy it. She is torn from her home and what is left of her family, but finds new friends at a school that may not be what it seems.

Set in Japan during the Sengoku Jidai — the century-long civil war era — Risuko is a coming-of-age adventure for a young woman adrift in a tumultuous, terrifying world.

It is the first in a planned four-book series, Seasons of the Sword.

In the Picture: Prepping Images for Your Ebook

Last month I discussed how to clean up your manuscript to prepare it for ebook conversion. This time I’m going to be looking at how to do the same thing with images.[1]

There’s one big difference, however: where the advice that I gave you about getting your text squeaky clean was equally valid for preparing to convert your words to either print or ebook format, these suggestions are ebook-only.

What’s the difference?

Well, in either case, you’re going to start by finding the perfect picture to go with your words. You’re going to crop the picture (cutting out any extraneous bits) and enhance it (or get someone who knows how to do so) so that it looks beautiful.

However, there are two enormous differences between the image files you want to use in an ebook and ones you’re going to get printed on paper:

  1. In a print book, color is expensive, while in an ebook beautiful color costs (essentially) the same as black and white.
  2. On the other hand, in print, you want the image file that goes off to the printer to be as high quality (that is to say, large) as possible, while in an ebook, every kilobyte costs you (I’ll explain how below).

 

Continue reading In the Picture: Prepping Images for Your Ebook

Which Ebook Format Should I Choose, ePub or PDF?

I was asked recently which file format was better, ePub or PDF. (Just as well that the Kindle-only mobi format was left out! It’s easy to convert from an ePub file anyway.)

Which is better depends on what purpose the file is going to serve. Continue reading Which Ebook Format Should I Choose, ePub or PDF?

Is Big Brother watching what you read?

Earlier this year, at the Digital Book World conference, a company called JellyBooks announced what it called “Google Analytics for ebooks.”

For some readers, this raised the specter of Big Brother — or his corporate brethren — reading over their shoulders. Should we be worried about what we’re reading being tracked? Continue reading Is Big Brother watching what you read?