Showing posts with label HTML. Show all posts
Showing posts with label HTML. Show all posts

September 26, 2012

The eBook Design and Development Guide - My Latest

After eight-months of toil, the eBook Design and Development Guide is finally published. I'm pretty proud of this one, since it includes some nice color-coded snippets and over 150 images. Here are some screen shots:


You can pick it up at Amazon for $6.99. Thanks so much.

While eBook sales continue to increase around the world, the actual layout and presentation of eBooks is in a sorry state. Mangled formatting, defective images, and non-reflowable content are just a few symptoms of the problem. Many publishing houses and independent authors believe that converting an eBook directly from Word, InDesign, or another publishing program will produce a “good enough” eBook. However, poor sales and poor reviews from readers suggests otherwise.

The eBook Design and Development Guide is a technical reference for serious publishers and authors who want to produce professional eBooks with a focus on quality. This guide provides all the technical knowledge of HTML, CSS, EPUB, XML, and KindleGen that is necessary to create your eBook from a source manuscript into an eBook ready for sale at all the major eBook vendors, including the Amazon Kindle Store, Barnes & Noble Nook, Kobo, and the iBookstore.

No prior experience is required and the process of eBook production requires no expensive third-party software. This guide will teach you how to master the following tasks:

  • Converting your manuscript into HTML
  • Adding hyperlinks, bullets, and tables to your eBook
  • Embedding correctly-sized images in your eBook
  • Creating a clickable Table of Contents for your eBook
  • Adding basic presentation and styling to your eBook with CSS such as font sizing, margins, and indentation
  • Embedding fonts in your eBook
  • Adding advanced styling to your eBook such as gradients, drop caps, and backgrounds
  • Creating a valid EPUB eBook
  • Creating a valid MOBI/KF8 eBook with KindleGen
  • Adding metadata into your eBook
  • Working with regular expressions for quickly converting an eBook

Additionally, the guide provides a step-by-step walkthrough for a sample conversion of an eBook, as well as some helpful tips on the publishing and marketing process. All HTML, CSS, and EPUB templates are available for immediate use in this guide so you can start making your eBook for Kindle, Barnes & Noble, iBookstore, and all the other major platforms.

This book is a follow-up to How to Format Your eBook for Kindle, NOOK, Smashwords, and Everything Else with much more details on HTML, CSS, and the EPUB standard. It also explains how to properly design an eBook for the new Kindle Format 8 (KF8) devices such as the Kindle Fire, Kindle Touch, and Paperwhite.

Buy this 65,000-word guide that contains 150 images and 200 code snippets, and you will be able to create professional eBooks that really stand out from the crowd.
Share/Bookmark

June 15, 2012

Adding Background Images and Gradients to Your eBooks


Thank you for visiting this eBook design tutorial. We now have an eBook design startup—BB eBooks—dedicated to helping independent authors and small presses get their eBooks formatted, converted, and ready for sale at all the major online retailers (e.g. Amazon's Kindle Store, Barnes & Noble's Nook, iBookstore, Smashwords, etc.) Please contact us for a no-obligation quote. For those writers, editors, and publishers looking to go the DIY route for eBook production (you probably are if you visited this page), we offer free online tutorials and apps to help you professionally design your eBook. Please visit our Developers page and let’s work together to improve the overall standards of eBooks. Also, please sign up for the mailing list for promotions, design & marketing tips, plus eBook industry news.


Looking for a complete guide on eBook production? Please consider The eBook Design and Development Guide, which contains everything you need to know about HTML, CSS, EPUB, and MOBI/KF8 to make an eBook like a pro. Pick it up at Amazon for $6.99 today.

Changing the background in eBooks is not possible on e-ink Kindles/Kindle for iOS; however, since the Kindle Fire runs on the Webkit engine, it is possible to use higher-level CSS properties to style different types of backgrounds on this device. To change the color, you simply use the background-color: color code; syntax. This may be suitable in some situations, but print books often have repeating images or gradients in the background of certain types of content to draw the reader's attention. This guide discourages altering the background for the entire eBook, because it prevents the user from selecting their default background (on the Kindle Fire users have a choice between white, sepia, and black). Full background alterations can also looks tacky--like a MySpace page circa 2005.

Background Images


Hypothetically, you have a public domain image from a site like Open Clip Art that you would like to be the background for some boxes of content in your eBook. This 50x50px image has the filename seashell.jpg and it is in the same directory as your CSS file. The CSS syntax to declare the seashell as a background image is as follows:
p.myseashells
{
background-image: url("seashell.jpg");
}
Tip: If your image file was in a different directory than your CSS, you simply specify a relative path for the URL such as "./myimages/seashell.jpg". In this example, the CSS file is in the parent directory to myimages.

Now that you have declared a background image for paragraph elements with the myseashells class, you can implement the CSS with the following HTML markup:
<p>Normal paragraph content</p>
<p class="myseashells">This is paragraph content where there is a background image applied. Notice how by default it repeats both horizontally and vertically. We will learn how to further customize our CSS to allow repeating images.</p>
Background Image on Kindle Fire

By default the background image repeats vertically and horizontally to encompass the entire box of the paragraph element. The first seashell is located at the top and left of the box, and it repeats until the start of the border section of the box. This means that if any padding is applied on the box, the background image will be rendered behind the padding section as well as the content section.

The preceding example is a bit unseemly, because it overwhelms the content. There isn't a good way in CSS to add transparency to a background image. To make the image have some opacity, you can add an alpha channel in a suitable image editor and save the image as a PNG. You may also want the background-image to repeat in specific directions or not at all. Here is the CSS to perform this task:
background-repeat: repeat-x; /* Repeat Image Horizontally Only */
background-repeat: repeat-y; /* Repeat Image Vertically Only */
background-repeat: no-repeat; /* Image Only Appears in the Top Left */
Different Repeating Background Directions

Gradients


Adding gradient colors to your eBook gives a very professional look on content you would like to emphasize to the reader. While using a gradient for the entire background of the viewport would cause poor readability, gradients can be useful for individual boxes of content you would like to stand out. The syntax for generating a gradient background can get a bit complicated. It also doesn't help that the CSS3 standard for gradients is still evolving and changing, even though it was introduced way back in 2008. Since Kindle Fire is based on the Webkit engine, the following is the basic CSS syntax for a linear gradient. Please note the -webkit- prefix and the fact that there are no spaces before or after the parentheses:
background-image: -webkit-gradient(linear, staring point, ending point, from(color), to(color));
If you wanted a gradient to start on top of the box with a white color and end with a gray color, some sample CSS would look as follows:
p#graygradient
{
background-image: -webkit-gradient(linear, left top, left bottom, from(white), to(gray));
border-radius: 15px;
border: 1px solid black;
padding: 5px;
}
Gradients on Kindle Fire

Notice how the gradient starts at the top with the white color and terminates at the bottom with the gray color. It is also possible to provide different directions in the first and second values. The entire specification that is unique for Webkit is here.

Important Note: The -webkit- prefix is required for the CSS3 to be recognized on Chrome and Kindle Fire. The newer gradient specification that uses a different syntax does not yet render on the Kindle Fire.
Share/Bookmark

April 28, 2012

Adding Images to Your Kindle eBook


Thank you for visiting this eBook design tutorial. We now have an eBook design startup—BB eBooks—dedicated to helping independent authors and small presses get their eBooks formatted, converted, and ready for sale at all the major online retailers (e.g. Amazon's Kindle Store, Barnes & Noble's Nook, iBookstore, Smashwords, etc.) Please contact us for a no-obligation quote. For those writers, editors, and publishers looking to go the DIY route for eBook production (you probably are if you visited this page), we offer free online tutorials and apps to help you professionally design your eBook. Please visit our Developers page and let’s work together to improve the overall standards of eBooks. Also, please sign up for the mailing list for promotions, design & marketing tips, plus eBook industry news.



Looking for a complete guide on eBook design and development? Please consider The eBook Design and Development Guide, which contains everything you need to know about HTML, CSS, EPUB, and MOBI/KF8 to make an eBook like a pro. Pick it up at Amazon for $6.99 today.

Previous Post: Page Breaks

Similar to page breaks, images are something that frequently get mangled by publishers who don't invest the time and energy toward professional eBook design. In print books there is tremendous attention to detail regarding the image sizing, how text flows around an image, and how captions are displayed below an image. Unfortunately, the state of eBook land is nowhere near that standard, and it is not uncommon for readers to see an image that is haphazardly placed on their Kindle, completely disrupting their reading experience. This partly due to the fact that publishers are still getting used to the concept of reflowable content, but it also due to sloppy HTML markup and CSS. There are limitations on how images can be arranged in the e-ink Kindles and Kindle for iOS; however, the Kindle Fire supports many more features (including floating, which will be discussed in this section).

Styling Images (All Kindle Devices)


The e-ink Kindles and Kindle for iOS can only support images as, essentially, a block element. It is not possible to wrap text around an image, which is also known as "floating." However, care should be taken to ensure that the image is the right size, that it is properly aligned in the viewport, and perhaps it has a caption if needed. Recall the proper HTML markup for an image:
<img src="tester.jpg" alt="Centered Baby Pic" width="134" height="200" />
In this case tester.jpg is a 134px wide/200px high image located in the same directory as our HTML file. Do not neglect to add the alt attribute or your HTML will fail EPUB validation. Please also recall that img elements should not appear on their own in your HTML markup, and they should be wrapped in a block element (typically <p>…</p> tags). As an example, let's try adding an image with a caption in one paragraph element and see how it renders on an e-ink Kindle:
<p><img alt="Centered Baby Pic" height="200" src="tester.jpg" width="134" />Pic with Caption That Has No CSS</p>
Example of Poor Image Layout with no CSS

The preceding example is most certainly not how you want the layout of your text and image to look, but a large number of not-to-be-named eBooks appear like this (even the expensive ones!). Notice that the image is in-line with the caption text, which aligns vertically with the bottom of the image by default. Let's try adding some CSS to center both the image and caption. Also, you can force the caption text directly below the image with a line break tag (<br />).

Some example CSS for images that functions well on all Kindle devices is as follows:
p.imgcentered
{
text-indent: 0;
margin: 1em 0 0 0;
padding: 0;
text-align: center;
font-size: 0.8em;
}
And the HTML:
<p class="imgcentered"><img src="tester.jpg" alt="Centered Baby Pic" width="134" height="200" /><br />Centered Pic</p>
Properly Centered Image with Caption Below

Important Note: Be careful with the text-indent property when centering images. You should set it to 0 to avoid the box that the image is in from being slightly off-center.

This is the limit to how much styling you can do to images for the e-ink Kindles and Kindle for iOS. Since floating is not recognized on e-ink Kindles and Kindle for iOS, it is best to treat every image like a block element. If you want to make a collage of two images together, this guide recommends using your favorite photo-editing software to save the collage as one big image. However, the Kindle Fire allows for many more styling features for images.

Adding Borders to Images (Kindle Fire Only)


You may want to create a border around an image just so that your readers can easily distinguish it from textual content. Creating borders around images is very simple and done in the same fashion as a block element. It is also possible to add a drop shadow utilizing CSS3 properties and values, and you will learn more about that in the Advanced CSS section. Here is some example CSS to add a simple red border around an image:
p.imgcentered img
{
border-width: 2px;
border-style: solid;
border-color: red;
}
Image with a Border in Kindle Fire
Tip: You can use the shorthand CSS property border to include all three of these styles in one declaration. In this example, the declaration would be border: 2px solid red;.

Understanding Floating (Kindle Fire Only)


Floating an image is the process of wrapping text around it, which is typical of a print book. You can either float images to the left or to the right. You can actually float just about any element, and you will learn how to use floating to make drop caps in the Advanced CSS section. The float: left; and float: right; declarations are used to make an element (in our case a paragraph element wrapped around an image) float to either the left or right. This sounds like a very simple concept, but the actual way that floating ties in with HTML/CSS can be rather complicated, particularly in complex web design. If you are interested in learning about all of the intricacies of floating, A List Apart and CSS Tricks have excellent articles on the subject. For our purposes, think about floated elements as content that gets shoved to the left or the right, with the following content in the HTML markup trying to wrap around whatever was floated.

Suppose you want to have an image floated to the left with text wrapped around it. Some example CSS would be as follows:
p.imgleft
{
text-indent: 0;
margin: 1em 0 0 0;
padding: 5px;
text-align: center;
font-size: 0.8em;
float: left;
}
And the HTML:
<p class="imgleft"><img src="tester.jpg" alt="Floated Left Baby Pic" width="134" height="200" /><br />Floated Left Pic</p>
<p></p>
 Image Floated to the Left on the Kindle Fire
You will notice that padding was added to the paragraph element that was floated. This ensures that the text that wraps around it does not directly butt up against the image. Also, the floated element is placed before the content that will wrap around it.

Comprehension of the CSS Box Model is needed to explain how the Kindle Fire renders it. In this case, the width of the box that is floated to the left collapses to the width of the image plus the padding. If you have a longer text caption (one that exceeds the width of the image), the width of the box will become the width of the caption. Observe this example:

Floated Box Expands Based on Caption Length
If you wish to avoid this problem, you can declare the width property of the floated element. This will ensure that the text wraps to the next line.

Clearing the Float (Kindle Fire Only)


Floating works well if the text wrapping around the image is long enough so that the height of its box is longer than the height of the image box. However, if you have a paragraph element that is short, the next paragraph element will also continue to wrap around the floated element. This may not be the design that you intended. Observe the following CSS for an image floated to the right:
p.imgright
{
text-indent: 0;
margin: 1em 0 0 0;
padding: 0 0 0 5px;
text-align: center;
font-size: 0.8em;
float: right;
}
And the HTML:
<p class="imgright"><img src="tester.jpg" alt="Floated Right Baby Pic" width="134" height="200" /><br />Floated Right Pic</p>
<p>A very short first paragraph that is too short to get around the image.</p>
<p>The next paragraph - not clearing the float!</p>
Example of Not Clearing the Float
The fact that both paragraph elements are wrapped around the image is not a bug in the Kindle Fire. All markup and content in the HTML following a floated element will try to wrap around it, unless you clear the float. Clearing is done with the CSS declaration clear: both;, and it ensures that nothing is to the left or the right of that selected element. In this case, you want to clear the float on the second paragraph to ensure that it appears below the image floated to the right. Some example CSS to add to the preceding example:
p.clearit {clear: both;}
Some example HTML that alters the second paragraph:
<p class="clearit">The next paragraph - clearing the float and then some more!</p>
Example of Clearing the Float
Perpetual worrywarts like to say that eBooks will never be the same as print books due to the lack of good methods to layout the images in reflowable content. However, HTML and CSS provide powerful tools that allow you to layout images in a variety of different ways (particularly on the Kindle Fire). Take the time to experiment with your presentation and content, and you will distinguish yourself as a professional eBook designer.

Next Post: Advanced CSS - Borders, Rounded Corners, and Box Shadows
Share/Bookmark

April 24, 2012

Adding Page Breaks for Your Kindle eBook


Thank you for visiting this eBook design tutorial. We now have an eBook design startup—BB eBooks—dedicated to helping independent authors and small presses get their eBooks formatted, converted, and ready for sale at all the major online retailers (e.g. Amazon's Kindle Store, Barnes & Noble's Nook, iBookstore, Smashwords, etc.) Please contact us for a no-obligation quote. For those writers, editors, and publishers looking to go the DIY route for eBook production (you probably are if you visited this page), we offer free online tutorials and apps to help you professionally design your eBook. Please visit our Developers page and let’s work together to improve the overall standards of eBooks. Also, please sign up for the mailing list for promotions, design & marketing tips, plus eBook industry news.



Looking for a complete guide on eBook design and development? Please consider The eBook Design and Development Guide, which contains everything you need to know about HTML, CSS, EPUB, and MOBI/KF8 to make an eBook like a pro. Pick it up at Amazon for $6.99 today.

For a long time in web development, no one really cared about page breaks due to the simple fact that websites are designed to be scrolled. However, inserting page breaks and the art of pagination has become essential with eBook design. Proper page breaking vastly improves the reader's experience by clearly annotating breaks in content. Hopefully by now you have realized that page numbers are not needed or wanted in reflowable content eBooks, since the page size is completely arbitrary based on the reader's device and personal settings. But, that doesn't mean that page breaking at the right point in your content is unimportant. A lack of page breaks at the proper location in eBooks is truly the mark of an amateur. People who make the misguided step of converting an eBook directly from a .doc/.docx or, even worse, from a PDF, are bound to have pagination issues and a sloppy eBook.

Forcing Page Breaks in the Kindle

With CSS, forcing page breaks is very easy. There are two basic ways to do this: the page-break-before: always; declaration and the page-break-after: always; declaration. As you probably guessed, the former creates a page break before the markup selected is rendered and the latter creates a page break after the markup selected is rendered. Typically you will want to make page breaks in relationships to major breaks in the content such as chapter headings, section breaks, and appendices. Here is some example CSS to force page breaks:
h2.pagebreak_beforeme 
{
margin: 2em 0 0 0;
page-break-before: always;
text-align: center;
}

h2.pagebreak_afterme 
{
margin: 2em 0 0 0;
page-break-after: always;
text-align: center;
}

Example of Page Breaks in the Kindle Fire

All versions of the Kindle (Kindle Fire, Kindle for iOS, and e-ink Kindles) recognize these page break properties of CSS. Unfortunately, at the time of this writing there is a discrepancy amongst the different devices with the way that the margin is rendered from the top of the Kindle's viewport to the heading with a page-break-before: always; declaration. You will notice that a top margin of 2em was declared in the preceding CSS. Observe how this is rendered differently between the Kindle Fire, Kindle for iOS, and e-ink Kindles:
Problems with Margin Rendering on Page Breaks
The Kindle Fire clips the top margin (and top padding) on anything following a page break, while the Kindle for iOS and e-ink Kindles render it properly. This presents a frustrating conundrum, because anywhere there is a page break in your eBook (and there will most likely be several), you cannot control the spacing between the viewport and the top of the heading on the Kindle Fire. You may be tempted to add blank spaces to force spacing, but you should not resort to such silly tactics. To achieve perfection on your page breaks, there really is only one solution…

The Alternative and Better Method for Controlling Page Breaks on the Kindle

This guide has not discussed yet how to actually build the EPUB package and convert it into a MOBI/KF8 eBook. It is considered good practice to split your HTML file that contains all of your markup and content into numerous smaller files within the eBook package. For technical reasons, this prevents the eReading device from having to load one massive HTML file into its memory, which would make it run slower. Generally, splitting the files should be done somewhere logical (e.g. one HTML file for each chapter). At every split in the HTML file, the eBook on all Kindle devices (and all eReading devices for that matter) will appear as a page break to the reader, regardless of the CSS declarations. Whenever there is a new heading at the start of one of these HTML files, the margin declarations will be recognized.

You will learn more about file-splitting during the discussion of the EPUB package structure. For now just recognize the limitations of using CSS for page breaks.

Avoiding Page Breaks

In some cases you may want to avoid a page break within an element. A good example is the caption of an image. Seeing the image's caption on the page after the image does not make for a good reading experience. To avoid this common scenario, you can use the CSS declaration page-break-inside: avoid;, which will cause the eReading device to try and put all of the selected content on the same page.

Important Note: The page-break-inside: avoid; CSS declaration is only recognized on the Kindle Fire.

Here is some example CSS to declare a paragraph class where the Kindle Fire will try to render all the content in the paragraph element on one page:
p.pagebreak_avoid
{
page-break-inside: avoid;
text-align: center;
font-size: 0.8em;
}
And the corresponding HTML:
<p></p>
<p class="pagebreak_avoid"><img src="tester.jpg" width="400" height="600" alt="Picture of a Cute Baby" /><br />The Caption would go here. If it's really long, you probably want it to be on the same page as the picture.</p>
<p>
</p>
Avoiding Page Breaks in the Kindle Fire
This image has a large height (600px) that almost takes up an entire page of the Kindle Fire in portrait orientation. What's happening is the Kindle Fire will insert a page break before the image to ensure that there is room on the subsequent page for both the image and caption. If page-break-inside: avoid; is not declared, you could end up in a situation where the image is rendered at the bottom of the page, and the caption appears on the following page.

Avoiding page breaks is also useful in other situations where you want to guarantee that content is rendered on the same page such as: multiple tables, lengthy quotations, or technical specifications. If you want to avoid a page break in between multiple paragraph elements, consider wrapping everything in a div block element and using CSS to select that div to have the page-break-inside: avoid; declaration.

Tip: If you're workflow involves debugging in a web browser (Chrome is recommended), you can see how the pagination functions in your document under the "Print Preview" feature. Only during printing do web browsers render page breaks.

Hyphens and the Kindle - Not Just Yet

So the good news is that the CSS3 specification allows for the control of hyphenation. This is especially useful in situations where you want to avoid hyphens (like headings). The bad news is that the team at Amazon has not coded their Kindles (even the Kindle Fire) to recognize the CSS to alter hyphenation control. The Kindles do not have a very aggressive hyphenation policy (unlike iBooks), so we're not totally S.O.L. as eBook designers. However, if they decide to issue an update that supports changing hyphenation, the CSS is pretty shake and bake:
Some selector {hyphens: none;}

No Way to Control Hyphens on the Kindle

Share/Bookmark

February 5, 2012

Working with Images on the Kindle Fire


Thank you for visiting this eBook design tutorial. We now have an eBook design startup—BB eBooks—dedicated to helping independent authors and small presses get their eBooks formatted, converted, and ready for sale at all the major online retailers (e.g. Amazon's Kindle Store, Barnes & Noble's Nook, iBookstore, Smashwords, etc.) Please contact us for a no-obligation quote. For those writers, editors, and publishers looking to go the DIY route for eBook production (you probably are if you visited this page), we offer free online tutorials and apps to help you professionally design your eBook. Please visit our Developers page and let’s work together to improve the overall standards of eBooks. Also, please sign up for the mailing list for promotions, design & marketing tips, plus eBook industry news.



Looking for a complete guide on eBook design and development? Please consider The eBook Design and Development Guide, which contains everything you need to know about HTML, CSS, EPUB, and MOBI/KF8 to make an eBook like a pro. Pick it up at Amazon for $6.99 today.

Previous Post: Introduction to HTML for the Kindle Fire

Whether a vignette in between section breaks or a portrait of yourself playing with your cats in your workspace, images are a way to spice up your manuscript and make your eBook a more enjoyable read. While the e-ink Kindles are grayscale, the Kindle Fire, as well as other eReading devices like the Nook Color, iPad, etc., are in color. You don't need to be too concerned about including color images for some devices and grayscale images for others, because the e-ink Kindles will automatically convert color images to grayscale.

Images can be a bit tricky to work with, particularly when making them viewable on older Kindles, but they are well worth the time and effort. Do yourself a favor and get some free software such as Photoscape, to resize your images and convert them to various different formats. It may also be worthwhile to learn how to create your own images using Gimp (free) or Photoshop (expensive). For those not artistically inclined, public domain images can be acquired for commercial use at Wikipedia or at Open Clip Art. You can also purchase images for royalty-free usage at sites like iStockphoto or Dreamstime.

Important Note: Images that are used in eBooks should always be saved in RGB color model rather than CMYK, because CMYK is strictly for printing purposes. Also, Amazon advises saving images as 300 pixels per inch (300 ppi).

Image Formats Supported by Kindle


You should be aware of the different formats of images, because they should be chosen based on what type of imagery you are trying to display to your readers. The Kindle Fire supports four different formats as follows:
GIF: 256-color format that is suitable for line art, tables, and graphical text.
JPG: 16 million-color image format that can be degraded in quality to keep the size small. JPG is the most used image format and is suitable for photographs and artwork
PNG: high-quality, big-file size image format that can be used for logos, small complex images, and anything you want to look sharp.
BMP: Uncompressed image format (recommend never using due to its large file size).
SVG: Scalable Vector Graphics format that is composed of XML describing how the two-dimensional canvas should be drawn rather than pixels. As the name implies, it can be scaled to any size. This format is only supported on the Kindle Fire.

Size Does Matter for eBook Images


Why not just use the high-quality PNG format for all your images to give your readers the most sharp reading experience? Unfortunately, PNG images tend to be much larger in size than JPGs and GIFs, and the Kindles have limited space. You want to keep the size of your image files as small as possible without sacrificing too much quality. Many readers will be downloading your eBook via a cellular connection or through a crummy wireless connection at the local Starbucks. If your eBook is a huge clunker that takes up the same amount of space as a full-length movie, your reader may have a bad first impression waiting for the eBook file to download to their device.

Most importantly, size costs money. Amazon has a policy of charging a "delivery fee" to the publisher based on the size of your eBook for each one sold. It is currently $0.15/MB for any eBook with the 70% royalty option (typically eBooks selling at $2.99 or higher). So, if you are trying to sell a 5MB eBook, you would lose 75 cents per eBook. It may not sound like much, but if you plan on selling a thousand eBooks, that's $750—a full rent check for most of us. Beware of the size of your eBook.

Here are some tips to keep the file size of your images minimized:
  1. For line-art, tables, and images that are text-heavy, use the GIF format, which doesn't have many colors.
  2. Always consider degrading the quality of your JPGs with photo-editing software (simply cutting down the quality from 100% to 90% can half the size of the image file in some cases)/
  3. Only use PNGs or SVGs when you need an image to look sharp (like your personalized logo).
  4. Always cut down the image height and width to the size you want it to appear on the Kindle Fire. Since the Kindle Fire viewport is 1024x600px, you never need an image bigger than those dimensions (not applicable to the SVG format since it can be scaled to any size).
  5. Amazon Kindle allows image sizes up to 256KB, but you should get each image much smaller than that. When the MOBI/KF8 eBooks is compiled, KindleGen automatically converts images greater than 127KB to a smaller size.
  6. The cover image should be exactly 600x800px. A nice cover image will typically be in between 80-127KB.
  7. Images within the eBook (except for the cover image) should be no bigger than 500x600px so they work properly and don't get automatically converted on older e-ink Kindles with the 524x640px viewport.

Adding Images to Your eBook


Now that you understand the various formats and how to minimize the size, it's time to start adding them into your HTML markup. The HTML tag is simply <img />; however, it's important that you include attributes. HTML attributes provide additional information about the particular element they reside in. For example, here is some markup that tells the browser or eReading device to insert the tree.gif image in the images directory with a description of "A Picture of a Tree".
<img alt="A Picture of a Tree" src="images/tree.gif" />
The src attribute (pronounced "source") defines the directory and the image filename, while the alt attribute provides an alternative description of the image in case the browser or eReader cannot render it properly. The file directory specified in the src attribute is relative to the directory where the HTML file is located. So, if your html file is in c:/temp/myebook.html, the tree image would be in c:/temp/images/tree.gif. You always need to include the alt attribute for images in order for your eBook to pass EPUB validation.

Important Note: The filename and directory specified in the src attribute is case sensitive.

Try adding an image into the elf story to see how it looks in your browser. Some example HTML would be as follows:
<p>It was not a good time for the elves residing in the woods. The warlocks had poisoned their fields and stolen all their food, the bastards.<img src="images/tree.gif" alt="A Picture of a Tree" /></p>
<p>By the time the manuscript got around to the second paragraph, everyone but one elf was dead—what a shame!</p>
Example of an Image In-Line with the Content
If you want to have the image function like a block element and not be placed in-line with the text, you can wrap paragraph tags around it like so:
<p>It was not a good time for the elves residing in the woods. The warlocks had poisoned their fields and stolen all their food, the bastards.</p>
<p><img src="images/tree.gif" alt="A Picture of a Tree" /></p>
<p>By the time the manuscript got around to the second paragraph, everyone but one elf was dead—what a shame!</p>
Example of an Image Acting Like a Block Element
In general, you should wrap the self-closing <img /> tag in paragraph tags to make it function like a block element. By using CSS to adjust the paragraph tags, you can wrap text around the image, align it, and even add drop shadows. Unfortunately, for the older e-ink Kindles, they have very limited support for where you can place the image. They must not be "floated", which means they cannot have text wrapping around them. This makes fancy typography like drop-caps impossible. There is also the problem of the older e-ink Kindles randomly resizing your images.

Avoid Automatic Resizing of Images on Old Kindles


The tree.gif image used in the above example is 179x145 pixels. Once the MOBI/KF8 file is compiled, the tree image will look okay in the Kindle Fire as well as the Kindle apps for iPhone, iPad, and Android. However, the older e-ink Kindle will completely bloat the dimensions of the image, making the aspect look ridiculous and the image appear grainy.
Example of Image Mangling in Older Kindles
To avoid the maddening automatic resizing of images on older e-ink Kindles, you can specify height and width attributes. This forces the image to display with its exact dimensions. An example of the markup for the 179x145px tree image would be as follows:
<p><img alt="A Picture of a Tree" height="145" src="images/tree.gif" width="179" /></p>
Proper Image Resizing for Older Kindles

Tip: The order of attributes does not matter inside HTML elements. For example, you could have specified the width attribute before the height attribute, and the HTML markup would be perfectly valid.

Special Note on Using SVG Images for the Kindle Fire


Since SVG images can be scaled to any size, you should always specify either a height or width attribute within the <img /> tag. By only specifying one of the attributes, the aspect of the SVG will be maintained. Some example HTML markup for an image called fancytree.svg would be as follows:
<p><img src="images/fancytree.svg" height="200" alt="A Picture of a Tree with Awesome SVG" /></p>
An SVG Image in the Kindle Fire

Unfortunately, SVG images are only supported in the Kindle Fire. Older e-ink Kindles (including the Kindle App for the iPad and iPhone) will completely ignore them and not even display the description in the alt attribute. Once you master CSS, you will learn some tricks to show the SVG image for readers with the Kindle Fire, while providing a fallback JPG or GIF for readers with older Kindles.


Next Post: Adding Audio and Video Content to Your eBook for the Kindle Fire
Share/Bookmark

January 26, 2012

HTML Tutorial for the Kindle Fire (Part 1)


Thank you for visiting this eBook design tutorial. We now have an eBook design startup—BB eBooks—dedicated to helping independent authors and small presses get their eBooks formatted, converted, and ready for sale at all the major online retailers (e.g. Amazon's Kindle Store, Barnes & Noble's Nook, iBookstore, Smashwords, etc.) Please contact us for a no-obligation quote. For those writers, editors, and publishers looking to go the DIY route for eBook production (you probably are if you visited this page), we offer free online tutorials and apps to help you professionally design your eBook. Please visit our Developers page and let’s work together to improve the overall standards of eBooks. Also, please sign up for the mailing list for promotions, design & marketing tips, plus eBook industry news.



Looking for a complete guide on eBook design and development? Please consider The eBook Design and Development Guide, which contains everything you need to know about HTML, CSS, EPUB, and MOBI/KF8 to make an eBook like a pro. Pick it up at Amazon for $6.99 today.

Previous Post: Introduction to the Kindle Fire

Introduction to HTML


HTML is the markup language of the internet and of eBooks. It is the language that determines how web browsers and eReading devices render the content to show to the user. Go to any website in the web browser of your choice and press CTRL+U, and you will see the HTML source code. It may appear a bit daunting at first, but if you break it down into small little pieces, you will begin to have a basic understanding of how to convert your manuscript into the HTML necessary for a beautifully designed eBook.

The official syntax for how HTML should be structured is defined by a published, open-source specification. The World Wide Web Consortium (or W3C) is a group of internet architects, web developers, tech industry experts, and other smart folks who determine this specification. The smart people working with Apple, Mozilla, and Google who develop web browsers (e.g. Safari, Chrome, Firefox, etc.) use this specification to create their product that gets used by billions of people throughout the world. Also, web designers and developers use this specification to create websites.

XHTML was an HTML specification finalized by the W3C in 2001, and it was utilized by many developers throughout the last decade to create websites. However, XHTML is being phased out in favor of HTML5, which has better support for audio and video, working with files, and a myriad of other new features that users are demanding. The HTML5 specification is still under development, but all browsers support at least some elements of it. eReading devices are also transitioning from XHTML to HTML5. This is a process that doesn't happen overnight, which is why eReaders like Kindle Fire support XHTML with some HTML5 elements thrown in. All of this technology and geek politics can be a bit hand-wringing at times. In order to prevent confusion, this guide will simply refer to everything as HTML.

In website design and development, it is considered a best practice to separate code for "content" and "presentation"--the same is true for eBook design. From an author's point of view, an example of "content" would be the paragraph of text in a chapter. An example of "presentation" would be the authoring desiring design characteristics for that paragraph of text such as a serif font, an indent on the first line of text, the paragraph becoming fully-justified, etc. For eBook design, content is specified in the HTML and presentation is specified in the CSS, which will be covered later.

In order that eBooks are reflowable, it is necessary to convert the content of your eBook into HTML. You should most certainly not save your manuscript in your word processor or PDF as an HTML file. Direct conversion from the proprietary formats into HTML is god awful, to say the least. By learning the basics of HTML, you will be able to convert your manuscript into a professional eBook manually, and the end result will look beautiful to your readers. So, let's get started.

Tools Recommended


Along with your word processing software that you use, here are some additional tools recommended to begin designing your eBook. Don't worry, they are all free.
  1. Text editor (Notepad++ [free], TextWrangler for Mac users [free]
  2. Adobe Digital Editions (previews EPUB files [free])
  3. Kindle Previewer (previews KF8/MOBI files [free])
  4. Gimp (for image design [free])
  5. Photoscape (Windows) or Picasa (Mac) (for resizing and manipulating images [free])
Some small presses and self-publishers like to use Adobe InDesign. It is a very fancy (and very expensive) software package used by many professional publishing companies. You do not need this program to make a professional eBook. If you would like to learn about using Adobe InDesign to create an eBook, I recommend Liz Castro's publications on the subject; however, we will not discuss Adobe InDesign in this guide. Although, if your source file was created in Adobe InDesign instead of text in a word processor, you can utilize that in the design workflow for your eBook throughout this guide.

The recommended web browsers are either Google Chrome or Safari. This is because they use the WebKit engine, which is the same software used by the Kindle Fire. A web browser is a good place to start first looking at your eBook before you compile it into the KF8/MOBI format. However, if you are a dedicated Firefox user, that browser should work fine as well. This guide does not recommend using Internet Explorer, ever.

HTML Structure


An HTML file is simply a text file with a .html extension. It is divided into two parts: the head section and the body section. The head section contains metadata, instructions to the web browser or eReader on how to read the HTML code, and other information that is not content displayed to the reader. The body section contains that actual marked-up content that will be displayed to the reader. Do not be intimidated by this code, and you can simply copy and paste into your text editor with CTRL-C and then CTRL-V from this guide. Here is an example:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
<title>Your Title Here</title>
</head>
<body>
Your Content Here!
</body>
</html>
After you copy and paste this code, save it as a .html file, such as myebook.html. If you are using Notepad++ as your text editor, it would look as follows:
Notepad++ HTML Example

Say you had a hypothetical story in your word processing program--quite possibly the world's worst, but suitable as an example. You could copy and paste it into the text editor program in between the <body> and </body> tags, which is where the content of your eBook should go. It would look like this in your text editor:
World's Worst eBook in a Text Editor
Note how every time there is a carriage return (achieved by pressing ENTER in the word processor), the text gets assigned a new line. Entire paragraphs of text should appear on a single line.

Tip: This manuscript contains curled quotes or fancy quotes, and they should appear in your text editor. If it is showing up as junk in your text editor, change the encoding to UTF-8 (under the Encoding menu for NotePad++).

Tip: The word wrap can be toggled on or off under the View menu for NotePad++. The preceding examples have word wrap turned on.

Since this is an HTML file, you can open it with your web browser. To open an HTML file located on your hard drive, simply press CTRL+O from within your web browser and locate the file. For this particular example, we have not applied any HTML markup to the content, so it will appear as one long string of text.
HTML with No Markup Applied

Obviously, this is not how you want your eBook to look at all. That is why it is necessary to encode all of your content into what is called HTML tags, so that a web browser and eReader knows where each paragraph or heading starts and ends.

Paragraph, Heading, and Div Tags


All well-formed HTML in the body section must exist within the opening and closing tags of block elements, which are HTML markups that make a rectangular object that does not break across lines. The most basic and frequently used example of a block element tag is the opening paragraph tag, <p>, and the closing paragraph tag, </p>, which wraps around a paragraph of text. An example is as follows:
<p>This is my paragraph of text. Hello, my friends!</p>
Notice how there should be no whitespace before the opening or after closing paragraph tag in your text editor. HTML syntax for all block elements follows similar rules: there is an opening tag <tag> and a closing tag annotated with a forward slash, </tag>.

The heading tags are used for block elements that are, you guessed it, headings. Heading tag range from <h1> down to <h6>. Typically, web browsers and eReading devices render <h1> tags the biggest and most important, <h2> the second biggest, <h3> the third biggest, etc. You will learn how to define the shape and size of headings when you learn about CSS. It is important to markup your heading content with these tags rather than simple paragraph tags, because it aids in search engine optimization. While search engine spiders going through eBooks is in the very early and rudimentary stages, it could become more important in the future. An example of properly marked-up content with heading tags would be as follows:
<h1>Title of my book</h1>
<h2>Chapter 1</h2>
<p>Content</p>
<h2>Chapter 2</h2>
<p>More content</p>
<h3>Chapter 2 Subheading</h3>
Important Note: The case of the HTML tags is important, and it should always be lowercase. The XHTML standard requires lower case tags, and the validation process for eBooks will require adhering to XHTML standards.

The <div> tag is a block element that wraps around other block elements. It is utilized to separate logical sections of content where you want to apply different types of presentation. For example, you may have several paragraphs of text in your eBook where you want a specific font. You would wrap the opening <div> and closing </div> tags around the three paragraphs, which are each wrapped in <p> and </p> tags. You will learn how to apply the presentation to the content within the <div> and </div> tags when you learn about CSS.

Now that you understand the basic concept of block elements, you can try applying them to your hypothetical story. The title should be wrapped in <h1> and </h1> tags, the chapter headings should be wrapped in <h2> and </h2> tags, and the content should be wrapped in <p> and </p> tags. The full HTML would look as follows in your text editor:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
<title>Your Title Here</title>
</head>
<body>
<h1>The End of Days</h1>
<p>By Joe Selfpubber</p>
<p>Copyright 2012</p>
<h2>Chapter 1</h2>
<p>It was not a good time for the elves residing in the woods. The warlocks had poisoned their fields and stolen all their food, the bastards.</p>
<p>By the time the manuscript got around to the second paragraph, everyone but one elf was dead—what a shame!</p>
<h2>Chapter 2</h2>
<p>The elf approached the warlock and said, “Why do you poison our fields? Now you shall die.” He drew his scabbard and slashed at the warlock’s waist. The warlock perished.</p>
<p>The End</p>
<h2>About the Author</h2>
<p>Joe Selfpubber is trying to make it big by selling his eBooks on the Kindle Fire so that he can quit his lousy day job. Drop him a line at http://joeselfpubber.com sometime.</p>
</body>
</html>
Save your HTML file in your text editor, and try refreshing your web browser with F5. The HTML that is rendered in your web browser should look as follows:
World's Worst eBook with Proper Markup

While this may not be the prettiest eBook in the world, it is functional and encoded properly in HTML. Try using the HTML validator on the W3C website, and it should say that the document was successfully checked. If it fails validation, re-examine your code to see where you made a mistake. Writing HTML can be a very unforgiving process that leaves little room for error, so remain patient and keep trying to find where the bugs are located in your text editor. The validator will usually tell you what line of code is problematic and needs debugging.

Line Breaks and Horizontal Rules


The way HTML is rendered in web browsers and eReading devices is in a reflowable format. You can see this if you adjust the viewing window width of your web browser. The number of lines per paragraph will increase as the width of your browser window decreases. Occasionally, it may be advantageous to force a line break within a paragraph to ensure that a sentence begins on the next line (e.g. poetry).

The HTML for a line break is simply the standalone <br /> tag, which is analogous to pressing SHIFT+ENTER in a word processing program. Some sample code would be as follows:

<p>This is a paragraph of text residing in between some paragraph tags. If you want, you can make sure that some text starts on a new line with a<br />line break.</p>
Example of How a Line Break Tag is Rendered

While the <br /> tag is useful in some situations, you should not use it to manually force whitespace between your paragraphs or force a page break in an eReading device. Utilizing CSS is much more appropriate at that task.

Important Note: Don't forget the forward slash at the end of the line break tag. All self-closing tags require a forward slash at the end to maintain XHTML validity.

Another example of a self-closing tag is the horizontal rule. This adds a horizontal bar across the width of the viewing window. These can be useful in the eBook design process when separating out blocks of text or for pull quotes. Like other HTML elements, these can be styled with CSS to give them different colors, different widths, shadows, and other features. For right now, let's learn about how to add them into the HTML markup of your content. You simply add the self-closing tag <hr />.

Perhaps you want to add a bar below the About the Author heading of your elf story. The HTML markup would look as follows:
<h2>About the Author</h2>
<hr />
<p>Joe Selfpubber is trying to make it big by selling his eBooks on the Kindle Fire so that he can quit his lousy day job. Drop him a line at http://joeselfpubber.com sometime.</p>
Example of a Horizontal Rule

This HTML code may seem trivial, but it is an important part of learning how your content needs to be marked up.

HTML Entity Names and Comments


Within your content, you may want to display the greater-than or less-than signs to the reader (i.e. ">" or "<"). However, if you try to write them in your text editor to appear as content, it will create serious problems for you and is improper HTML coding. This is because the greater-than and less-than signs function as reserved characters in HTML. When using them, the browser thinks your HTML is trying to form a tag, but in reality you are trying to display content. The same is true for the ampersand (i.e. "&").

As a workaround you can encode in your HTML code what is called an HTML entity name. The HTML entity name for the greater-than symbol is &gt;, the less-than symbol is &lt;, and the ampersand is &amp;.If you wanted a paragraph of your content to say, "4 > 1 & five is also < eight", the correct HTML would look as follows:

<p>4 &gt; 1 &amp; five " ' is also &lt; eight</p>
HTML entity names always start with "&" and end with ";", and a full list can be viewed here. Once upon a time, some eBook readers required that certain "fancy" characters (e.g. “curled quotes” , em dashes —, etc.) be converted into HTML entity names. Basically, any character outside of the ASCII set required conversion. This is not the case with any of the Kindle devices. Therefore, you can leave fancy characters that appear outside of the normal ones appearing on an English keyboard in your content.

Tip: If you are interested in converting your fancy characters into HTML entity names for older eReading devices, please have a look at the author's post on the subject from 2011.

It should be noted that for most HTML block elements if more than one consecutive space or a line break is utilized within the paragraph tag, the web browser or eReading device will render it with just one space. Therefore, if you had code that was as follows:
<p>This is a paragraph with a   lot   of   whitespace</p>
It would simply read "This is a paragraph with a lot of whitespace" within your browser. You can actually write the HTML across numerous lines within your text editor, and the browser and eReading device will render it the same. For some situations (e.g. poetry), you may want to add more than one space in between two letters. To accomplish this, you can the HTML non-breaking space entity name, which is &nbsp;. Try this HTML code in your text editor to see the difference:
<p>This is a paragraph with a lot of whitespace</p>
<p>This is a paragraph with a&nbsp;&nbsp; lot&nbsp;&nbsp; of&nbsp;&nbsp; whitespace</p>
Example of Using the Non-Breaking Space

HTML entity names may look nasty in your text editor, but the reader is oblivious to them if they are encoded properly.

You as the eBook designer may want to leave a few notes for yourself inside your HTML code. However, you do want this to render in the browser. To add a comment you simply use the opening tag <!-- and the closing tag -->. An example from the elf story would be as follows:
<p>By the time the manuscript got around to the second paragraph, everyone but one elf was dead—what a shame!</p>
<!-- I gotta come back and fix these headings with some CSS -->
<h2>Chapter 2</h2>
Example of a Comment Being Ignored by the Web Browser

The content within the comment tags will be completely invisible to the user, because the browser or eReading device does not process anything within the comment tags.

Next Post: Working with Images
Share/Bookmark