Take pride in your eBook formatting (Part V)
Posted by Guido ·Jan 3
This is the fifth installment of a series of articles. To read the previous one, please click here
Now that we’ve seen some of the structural basics of HTML, it is time to examine how you can affect the actual look of these elements. The easiest, most efficient and most reliable way is through so-called style. A style sheet is nothing more than a list of definition that allows you to tell the device exactly what you want it to do with each of the available HTML tags.
A valid style definition in HTML would look something like this…
<style type=text/css>
p
{
text-indent: 1.5em;
}
</style>
We need to surround these styles with a <style> HTML tag to tell the device which part of our page is the actual style definition or it would otherwise mistake it for structural HTML tags and end up with syntax errors.
There is a remarkable wealth of things you can directly influence with these types of style settings. The most commonly used ones are things such as font face, font size, line spacing, indentations, margins and so forth, but it is also possible to actually create things such as borders, drop shadows and other exciting things with, or even include specific images for display with each one of their respective tasks.
Some of the biggest problems I have seen while I was creating HTML sources for eBooks had often to do with the fact that every eBook reader has its own default settings. Some of them are set up to include extra space at the end of a paragraph, others are set up to create a 20 pixel margin around the text, others yet will indent the first line of every paragraph by default.
For us, this kind of random behavior is oftentimes unacceptable, and fortunately there is an easy remedy for it — a so-called style reset.
html, body, div, h1, h2, h3, h4, h5, h6, ul, ol, dl, li, dt, dd, p, pre, table, th, td, tr { margin: 0; padding: 0; }
This line, which will be the very first line in our style definitions, tells the device to remove all default margins and padding by setting them to 0 for all major tags we might use. The next step for us is then to go create specific style settings for all the tags we are going to use.
Since we will be using only a very small subset of HTML tags, in practice this is very little work but instantly gives you 100% control over the look of your final eBook. No more guessing or chance formatting for you!
“Hold on,” I can already hear you say. “Not all paragraphs are equal, for example,” and you are absolutely correct. In our eBook we will have paragraphs for general text, we might have headlines, we certainly will have chapter headings, and each of these will require a different look.
Fortunately, HTML gives us an easy way to create customized tags and allows us to style each of them individually. We do this by assigning a class to a tag, thus letting the device know which style to use for it. Take a look at this line, for example.
<p class=headline>Look at me</p>
Upon encountering this paragraph tag, the device will look for the paragraph style called “headline,” and use its settings to display the paragraph. If we want to tell it to display this type of paragraph in a large bold font, we would simply create a style that could look like this.
p.headline
{
text-size: 2em;
text-weight: bold;
}
This is pretty easy stuff, isn’t it? But what the heck does 2em mean?
While it would be possible to define the text size in points, the way you would do in a word processor, in the publishing world it is oftentimes easier to use the alternate measurement of Em.
Since text sizes in a document are usually in a certain relationship to each other, it makes much more sense to use a relative measurement than absolute font scaling. In practice this means that if you change the base font size of the document all other sizes in the document are automatically scaled up as well to an equal degree. All measurements made with ems are derived from that one base font size definition.
Not only does this ensure that the size relationship between fonts in your document remains intact at all times, it also means that you will not have to change all your styles manually, if, after a long day’s work, you decide that all the text is actually a tad too small. By using the relative measurement of em, this won’t be problem for you, ever.
I could bore you with the history of the em and talk about the old days when typesetters like myself would work with lead characters, but to make things easy for you, simply think of one em as the space taken up by the character M. In most typefaces, this creates a space that is as wide as it is high and is therefore a recognized measure.
If we are working in a 12pt font, for example, 1 em is also 12pt. Now, let’s assume we want to create an indentation in our text that is four characters – or 48pt – wide, we would simply make the indentation 4em. The advantage, as described above, is that if we should decide to switch to a 10 font instead, the indentation of 4em will still be four characters wide — 40pt in the new font size. The visual balance of our text will remain unaffected and it will remain pleasing to the eye without us having to raise a single finger. That is the magic of using the right tools for the job…
There are many parameters that you can adjust through HTML styles — far too many to cover here. We will encounter a number of them as we go along. Since most of them are self-explanatory, I may not necessarily explain the function of each one. However, if there are certain things to look out for, or if we should encounter unusual settings, I will certainly let you know about it.
There is one HTML tag in particular that I think I should single out at this point, however. I usually stay away from using H1 tags and its brethren H2, H3, H4, H5 and H6. They are strange beasts and their behavior can be quite unpredictable, depending on the device or browser you are using. Since we can recreate the behavior of these tags easily through the use of specially styled paragraphs, I usually prefer going that route.
A replacement of the H1 tag, for example, could look like this.
<p class="h1">This is a large headline</p>
By using an appropriate style for the paragraph class we can now give it the size, font and weight we desire.
p.h1
{
text-size: 4em;
text-weight: bold;
}
Below you will find an example of a style description that could easily use in any eBook right out of the box and get good results, no doubt.
<style type=text/css>
html, body, div, h1, h2, h3, h4, h5, h6, ul, ol, dl, li, dt, dd, p, blockquote, pre, form, fieldset, table, th, td, tr { margin: 0; padding: 0.1em; }
p
{
text-indent: 1.5em;
}
p.title
{
font-size: 1.5em;
font-weight: bold;
margin-top: 5em;
}
p.headline
{
text-indent: 1.5em;
font-weight: bold;
margin-top: 1.5em;
}
p.chapter
{
text-indent: 1.5em;
page-break-before: always;
font-weight: bold;
margin-top:5em;
margin-bottom:2em;
}
</style>
I will leave you with this example for this time. Feel free to explore style settings in a bit more detail in the meanwhile. In our next installment we will take a look at how to put it all together into an actual eBook source file.
Part I • Part II • Part III • Part IV • Part V • Part VI • Part VII • Part VIII • Part IX




27 comments
Comment by A.R. Williams on January 18, 2011 at 2:20 am
I have a question.
I thought I had read somewhere that CSS should not be used for kindle formatting (I may have misread this). Looking at some of the samples you have on Kindle, I really like the formatting of your books. The formatting is nice, clean, professional, plus easily read. This is definitely the style I want to use when I start publishing my own work.
My question is, does using the CSS formatting require any additional programming for when a user decides to increase the size of the text?
As I wouldn’t want it to be broken because I set the text size and unknowingly prevented them from being able to resize.
Thanks for any info you can provide to the above.
A.R. Williams
Comment by Guido on January 18, 2011 at 7:13 am
The Kindle can handle CSS just fine, In fact, if you really want to make sure your books display consistently without problem on any platform, I found CSS the best and safest way to go.
Once you have your style sheet n place for your book it requires absolutely no additional programming, particularly if you use the relative em sizing, when readers change the font size on their device. The text will nicely reflow and adjust to the new setting.
Comment by A.R. Williams on January 18, 2011 at 10:49 am
Thanks for the info! I’m interested in reading more of your post.
Comment by Lois Miller on January 22, 2011 at 9:24 am
Thanks for the wonderful information. You are a very good teacher. Much appreciated. Regards, Lois
Comment by Glenn McCreedy on February 13, 2011 at 7:52 pm
Good and solid practices! These will serve us well as the technology evolves and new platforms and formats appear.
Comment by Brian on March 10, 2011 at 7:40 am
Thanks for all the great formatting instruction. Just wanted to let you know it is much appreciated. I’ll buy the first Jason Dark novel today.
Comment by James Reynolds on March 14, 2011 at 10:27 am
I have greatly enjoyed and benefited from your series.
All of the books I convert are of a theological nature and consequently there are a lot of quotes from the Bible.
When I have had these books printed I set up a ‘format’ for the Scripture quotes giving them an equal right / left indent with justification.
The following CSS style works great in a browser viewed html but in the Kindle it gives no right indent.
p.myquote
{
text-align: justify;
margin-top: 1.0em;
margin-right: 3.5em;
margin-bottom: 1.0em;
margin-left: 3.5em;
}
Is the Kindle capable of giving a right indent?
Also, it doesn’t seem that on normal (full width) text it is necessary to justify as the Kindle seems to do it automatically.
Thanks
James
Comment by Andrew Fildes on May 4, 2011 at 4:02 pm
Sorry mate, it looks like a worthy effort but by part 5. I was utterly lost and confused. I can write and I can do enough basic html to pretty up an ebay listing but this simply does not make any sense to me. I think the point at which I got lost was the ‘make sure you use the ‘alt=’ command … er, why. What is it? I’ve got the wrong kind of brane perhaps.
Why can’t I format it it in Word using ‘real world’ commands and then just convert to htm before uploading? I know how to format a document, insert bookmarks and links and do a decent title page layout. I have to assume that Word is going to do a reasonable job, don’t I? Even if it isn’t perfect, I think I could knock out a couple of novels in the time it would take me to get competent at writing code!
Comment by Guido on May 4, 2011 at 4:28 pm
I said my piece about Word and eBook exports. Nothing to add there. The eBooks generated through word processors are prone to cause problems and errors across various platforms and end up being plain ugly to begin with. I prefer an eBook that is properly formatted.
I can’t imagine any writer would really NOT care about the presentation quality of their books. I mean, it is your book. Your name is on it. People are seeing it, they pay money for it and you say even if it is broken, it is “good enough?” Why would you even bother to write it? Put some random letters on a page – it’s good enough.
If you can’t do the eBook conversion yourself, then hire someone to do it for you, but please, don’t be another one of those amateurs who put out their eBooks without any regard for quality. You are not only hurting yourself, you are hurting all of us!
As for the alt parameter, it is simply a short text description of what the image shows. I noticed that I am a bit cursory there, though, you are correct, and I may edit the passage to include some additional information.
Comment by Patricia Farrell on August 2, 2011 at 6:13 pm
I am considering writing a series of eBooks for Amazon’s new eBook publishing service. If I write it in Word, won’t they do the conversion for me? Yes, I know that they will also have to do html graphics code as well as code to embed videos which I may use, but isn’t that what they do for their authors?
I’d appreciate your response. Thanks. I know you have provided a fine article on eBooks here, but I, too, just can’t get into coding. It’s not what I can do.
Comment by Guido on August 2, 2011 at 6:21 pm
Sorry, but no, that is not what Amazon is providing. Their conversion capabilities are extremely limited.
Comment by Paul Salvette on August 7, 2011 at 3:21 am
Mr. Henkel,
I commend you on your most excellent eBook formatting series. It really helped me get my first short story formatted for the Kindle. Thank you so much for providing this.
I hate to be an HTML nerd, but the needs to have quotes around “text/css” or else it will fail validation.
Should be
Comment by Paul Salvette on August 7, 2011 at 3:27 am
Mr. Henkel,
I commend you on your most excellent eBook formatting series. It really helped me get my first short story formatted for the Kindle. Thank you so much for providing this.
I hate to be an HTML nerd, but the <style=text/css> needs to have quotes around “text/css” or else it will fail validation.
Should be <style="text/css">
Thanks again for great series.
Comment by Guido on August 7, 2011 at 9:37 am
You are correct. I always have quotes, but because I was writing much of this in a word processor, which would have converted them into unwanted curly quotes, I wrote the code without quotes at all and probably forgot to re-insert them later. Thanks for pointing it out.
Comment by Paul Salvette on August 8, 2011 at 7:47 am
You’re welcome and sorry to be the turd in the punch bowl. Again, a great series on eBook formatting!
Comment by Mark on October 26, 2011 at 4:54 am
You should use H tags for headings for semantic reasons. If you find the behavior unpredictable, you can “reset” it by explicitly specifying all the CSS that might be set by default. This series tells you all you need to know about that:
http://sixrevisions.com/css/the-history-of-css-resets/
Comment by Marilyn on December 3, 2011 at 4:41 pm
I am a writer who needs to publish non-fiction ebooks. I respect the idea of having a better understanding of the underlying principles behind one’s website and (now, through your arguments) one’s ebooks. It makes sense. I have a basic understanding of what you are talking about but am wondering if I’m making a mistake embarking on a sharp learning curve re. developing the ability to create the underlying .html structure for an ebook I create. Will it lead me to never getting it right and ultimately having to go back to a “publisher”. Yes, ultimately most of us can learn anything but I’m worried about the length of time of “ultiamtely”
For example,
I already got side tracked in your discussion to a link to a more indepth discussion of CSS style sheet to “clear underlying problematic defaults in various formats”. I can understand the use of the style sheet but considering the different arguements is going way too far for me.
I’ve gone on here, thanks for considering my concerns.
Comment by Guido on December 3, 2011 at 5:13 pm
There is a difference between HTML used for web development and that for eBooks. HTML used to eBooks is essentially just a very small subset of the entire HTML standard. Problems that crop up in web develop oftentimes do not apply to eBook development. It is a different world and a very different approach.
I can’t really answer your question, because everybody has a different threshold, but in my opinion it is always worth understanding the underlying technology of what you are doing, if only to understand its limitations. You can hire someone like me to do your formatting for you, sure, but if you have read my tutorial and, as a result, are already aware of certain technical limitations of current eBooks, it will be much easier for us to communicate and work on the bets possible result. So, even if you decide not to do the coding yourself, it still gives you more control over what happens to your book in the end.
As for the “underlying problematic defaults in various formats” I would not pay too much attention to stuff like that. Most of it is blown out of proportion by a handful of hardliners. The fact of the matter is that all eBook formats are perfectly capable of doing what we need them to do for 95% of all mainstream eBooks.
Comment by Dan on December 8, 2011 at 3:04 pm
Just a note – the reset messes up ordered and unordered lists. Took me some time to figure out why my lists weren’t displaying properly!
Comment by Guido on December 8, 2011 at 3:07 pm
Messes up, as in?
It can’t mess anything up, it simply resets all the margins. If you create a list, naturally you will then have to define the margins the way you want them to be.
Comment by Dan on December 8, 2011 at 4:30 pm
Try it. Bullets and numbers show in HTML but not when rendered as an ePub file unless I define list classes in the css style sheet or remove the ul and ol from the reset.
Comment by Dan on December 8, 2011 at 4:41 pm
After reset, I added the following code to the style sheet to render lists properly. Your example above didn’t include any styling for lists, so I had to add it. Thanks for the detailed series! It was very helpful in understanding ebook structure.
ol, ul {
margin-left: 1.75em;
padding: 0;
}
Comment by Deb Kinnard on May 11, 2012 at 7:34 am
I, too, am completely lost. I still don’t know how to change a Word document into a HTML document. I looked in the Word software and bashed my developer-licensed husband’s brain against this, and we couldn’t find it.
I don’t think not knowing how to do this on an intuitive basis equates to lack of concern for quality. Can you suggest a primer on how (step by baby step) to convert a Word document to a HTML formatted document?
Thank you.
Comment by Guido on May 11, 2012 at 7:52 am
Well, you are reading that primer right there… Just start with part I of the series and make your way through it.
Comment by R. Baker on August 16, 2012 at 4:46 pm
Do you know if the ebooks formatted in this way using html, are compatible with the Smashwords style requirements and can pass through the Meatgrinder conversion ? Has anyone published through Smashwords using this method? Will the html need modifying if you are using different MS Word upgrades such as Word 2007 and Word 2010 on a PC not Mac?
Comment by Guido on August 16, 2012 at 5:35 pm
Smashwords does not accept HTML files. They require you to submit a word processor file and then they will butcher it until it no longer even resembles an eBook. And then they will sell it to their customers… no, make that YOUR customers. So, tell me again, why would you want to do this?
Comment by Cindi on December 18, 2012 at 7:12 pm
I would love to see this series of posts available as a download. It would make it easier to search to reread something and purchasing a copy would be a great way to say thank you for the info.