Skip to content

Instantly share code, notes, and snippets.

@elmimmo
Last active October 27, 2017 11:09
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elmimmo/00b5a2bc8e58287f8078 to your computer and use it in GitHub Desktop.
Save elmimmo/00b5a2bc8e58287f8078 to your computer and use it in GitHub Desktop.
Center images in ebooks
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8"/>
<title>Centering images in ebooks</title>
<!-- <link rel="stylesheet" type="text/css" href="style.css"/> -->
<style type="text/css">
.center-image {
margin: 1em auto;
text-align: center;
}
.center-image .image {
display: inline-block;
margin-left: auto;
margin-right: auto;
}
.image img {
display: block;
width: 100%;
height: auto;
margin: 0 auto;
}
.width-100 {
width: 100%;
}
.width-66 {
width: 66%;
}
.width-40px {
width: 40px;
padding: 0 10px 10px 10px;
box-sizing: content-box;
}
</style>
<!-- <link rel="stylesheet" type="text/css" href="responsive.css"/> -->
<style type="text/css">
/* Instead of embedding this CSS in here, it should reside in an external stylesheet (as commented out right above) so that other styles survive in ADE */
@media only screen and (max-width: 100ex) {
.width-66 {
width: 90%;
}
}
</style>
</head>
<body>
<p>100% wide image:</p>
<div class="center-image">
<div class="image width-100">
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=" alt=""/>
</div>
</div>
<p>66% wide image for standard viewports; 90% wide for small ones:</p>
<div class="center-image">
<div class="image width-66">
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=" alt=""/>
</div>
</div>
<p>Centered group of inline images:</p>
<div class="center-image">
<div class="image width-40px">
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=" alt=""/>
</div>
<div class="image width-40px">
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=" alt=""/>
</div>
<div class="image width-40px">
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=" alt=""/>
</div>
<div class="image width-40px">
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=" alt=""/>
</div>
</div>
</body>
</html>
@elmimmo
Copy link
Author

elmimmo commented Sep 22, 2014

Tested and works in:

  • iBooks 1.0.1 for Mac; iBooks 3.2 for iOS 7; iBooks 4.0 for iOS 8.
  • Adobe Digital Editions 4.0.3 and 1.7.2 (in the later's case, except the @media fragment—see below) .
  • Google Play Books for web (except media query—see below), iOS and Android (except if CSS width are set in pixels—see below).
  • Kobo for iOS.
  • KF8-compatible Kindle devices (except if CSS width are set in pixels—see below).
  • KF8 apps (some—see below).

Catches:

  1. Adobe Digital Editions 1.7.2 (and 2, although this particular markup was not explicitly tested on it) not only do not support CSS media queries, but will ignore a whole stylesheet that includes one. The @media CSS fragment must therefore be linked to as a separate external stylesheet after the base styles (as commented out in the markup), so that ADE does use those at least.

  2. Google Play Books for web (2015-02) messes the width of images for which a variable width dependent on a media query was specified, as is the case of the 2nd example ("66% wide image for standard viewports; 90% wide for small ones").

    In this example, the ereader injects inline CSS width & height px-based values to the img element that are always equivalent to 90% of the parent's width in pixels, even though, if anything, it should be 66% when the media query is not triggered (or, rather, no value at all since our CSS would work fine). The parent's width is still affected by our media query, therefore causing the contained image to overflow and appear uncentered.

  3. Google Play Books for Android and KF8-compatible Kindles (well, observed in Kindle Fire HDX do not size CSS pixels appropriately, instead using that value for hardware pixels (tested on a 2013's Nexus 7). Image widths sized in CSS pixels therefore result in tinier than intended when displayed on displays with a high pixel density (HDPI or higher).

  4. Mobipocket-only (i.e. non-KF8) compatible Kindles display all images at width 100% of the viewport no matter the specified CSS width. The "centered row of inline images", then, do not display as inline (as each occupies the whole width of the viewport), but one below the other.

  5. Works in Kindle for Mac. Not in Kindle for iOS, as observed with the AZK file that Kindle Previewer produces for previewing purposes. The reliability of such preview, though, as compared to how an ebook purchased and downloaded from the Kindle store displays, is questionable, so I cannot say for a book obtained from Amazon.

@elmimmo
Copy link
Author

elmimmo commented Feb 16, 2015

It did not work in ADE 4.0, but does work in ADE 4.0.3, so I updated the compatibility notes in the comment above.

@elmimmo
Copy link
Author

elmimmo commented Feb 16, 2015

Found defect in Google Play Books for web (2015-02), so I updated the compatibility notes in the comment above.

@elmimmo
Copy link
Author

elmimmo commented Feb 16, 2015

Noticed that Kindle Fire HDX has the same issue as Google Play Books for Android, incorrectly mapping CSS pixels to hardware pixels, so I updated the compatibility notes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment