Saturday, March 30, 2013

Using the @media query for Kindles


Amazon's new format, KF8, has added support for many features that the older mobi format was lacking. That's great, right? Well, KF8 is only supported on newer devices: the fourth generation of Kindles and newer.

But, I've heard from many authors that they don't want to limit their market. They want to sell their books on as many people as they can and I can certainly understand that.

So, what's the problem? The problem for me is, should I code the book for the older devices, or should I code it for newer devices?

The example I'm going to be using is tables since that's what I first experimented with.

Older Kindles had very limited support for tables. They supported simple tables, but they didn't look very great. So, the advice has generally been convert your tables to images and use that. That way you know your table will appear the way you want it to. On the other hand, the new KF8 format has much better support for tables. They also look and function much better.

If you code a table for a KF8 file, it won't look very good (or may display terribly) on older devices. But if you code a table for the older mobi format, you know that it could have been better on newer devices.

So, what to do?

Well, another new feature that came with KF8 was support for media queries. What does that mean? Basically you code for both versions in one file. The KF8 code will appear on devices that support it and the older code will appear on devices that don't.

I wrote up my experience when trying this for the first time as I was doing it. Here it is for anyone who would like to learn along with me:

So, first I build the book for KF8. That means building the tables in html. I like to do this separately at first. For example just open TextEdit/NotePad and write out the code for the table and fill it in. Once you're done with it you can copy/paste it into the book.

~~

Ok, now that the book is all finished and formatted for KF8, it's time to go back and code in the table images that will (hopefully) appear on older devices.

So, here's what I've added to my stylesheet:

.kf8
{ }
.mobi
{ display: none; }

@media amzn-mobi
{
.kf8
{ display: none; }
.mobi
{ display: block;
margin-left: auto;
margin-right:auto;}
}

So, the "mobi" class should only display on devices using the old mobi format. And I didn't want to change any of the formatting I'd already done so I left the "kf8" class blank.

Any table I have in the document gets the class "kf8" and just below them I'm adding in the images and giving them the class of "mobi".

<table border="1" cellpadding="5" class="kf8">
<p><img class="mobi" src="../Images/filename.jpg" /></p>

I left off the mobi class first to make sure the images display properly. Add in the mobi and watch them disappear.

Now, do this to all the tables in the book.

Now to toss it into kindlegen and see what happens. *Fingers crossed*
It completed the book with no warnings, that's a good sign.

Now to test the file. First, I'll open it in Kindle for Mac and see how it looks. Other than forgetting a kf8 class on one of the tables it seems to have worked. Now to try it in Kindle Previewer... And it works. I'm kinda surprised it went that easily. The tables are displayed on the Paperwhite and Fire, and the images are displayed on the Kindle and DX. Now, a final test. Load the file onto my Kindle Touch (KF8 supported) to see how it looks.

The tables are looking very good.

A success!

Here's a sample from Amazon's Kindle Publishing Guidelines (slightly editted by me) that you can try out for yourself. Try it in sigil or your favorite eBook/HTML editor and learn how to apply it to your own work.

Here's what you would add to your stylesheet:

.kf8
{ display: block; }
.mobi
{ display: none; }
@media amzn-mobi
{
.kf8
{ display: none; }
.mobi
{ display: block; }
}

And here's the code that would appear in your book:

<table class="kf8" bordercolor="#E66C2C" border="1" cellpadding="4">
<tr>
<th>Heading 1</th>
<th>Heading 2</th>
<th>Heading 3</th>
</tr>
<tr>
<td>Cell 1</td>
<td>
<table bordercolor="#003399" border="1" cellpadding="4">
<tr>
<td>Nested 1</td>
<td>Nested 2</td>
</tr>
<tr>
<td>Nested 3</td>
<td>Nested 4</td>
</tr>
</table>
</td>
<td>Cell 2</td>
</tr>
</table>
<img class="mobi" src="../Images/tableimage.jpg" />


A final note:
I used tables as an example, but this is definitely not limited to that. You can use this for any coding that you want to appear differently on one device vs another.
Just apply the “KF8” class to what you want to appear on the devices that support the new format. And apple the “mobi” class to what you want to appear on older devices.

No comments:

Post a Comment