Showing posts with label html. Show all posts
Showing posts with label html. Show all posts

Monday, July 15, 2013

Playmobil triggers, or why I spend so much time on Twitter

Last night, my daughter decided to recreate our entire kitchen in Playmobil. The counters and kitchen furniture, a dog and three cats, a bowl in the island and silverware in the drawer just where we keep ours, but also dishes in the sink, overflowing compost, father making dinner, son playing soccer, and me on Twitter. I'm biased but it's quite brilliant.

Liz (and her phone) in Playmobil

My kids tease me about my Twitter addiction and at dinner, after I tweeted the tomato picture they also accused me of being one of those “food tweeters” (gasp!), clearly not a good thing. But subscribing to Tim O'Reilly's tenet that ambient intimacy is one of Twitter's keystones, I forged ahead undaunted, and tweeted the part of the Playmobil portrait that was of me and my coffee and my phone.

Which is when Twitter did its magic. My Twitter friend Melissa Techman favorited it and asked if she could use it for a Thimble mashup she was doing with her teens. Melissa is a librarian in Virginia (Maryland?) who is always working on amazing ways to get kids thinking and working on books and technology. Though we haven't (yet) met in person, we've been following each other for a year or so, and seem to continually find interests in common and ways to bounce ideas of each other. She organized an online workshop last year for which she invited me to do a short presentation on ebook production. I'm excited to see what else we can collaborate on in the future.

Melissa and Thimble

Meanwhile my daughter wanted to know what a Thimble was, so I asked Melissa, and she pointed me to the website. And it turns out it's a way of creating web pages—I haven't figured out the remixing part yet—and so my daughter and I spent the next hour or so writing HTML. It's hard for me to quantify what a treat it is to be able to share my knowledge with my kids. They know almost everything already (!) so it's pretty rare that they let me teach them anything. But if someone else suggests it... well, thank you, Melissa!

After my daughter went to bed, I spent a little while following some of Melissa's tweets about #clmooc. I keep bumping into MOOCs and I know I should know more about them and that I will love them when I get there, but I haven't had a moment to look more closely. But just reading those MOOC-related tweets is what gives me hope about the world. There are so many people working together, sharing knowledge, learning cool things, inventing, making.

It didn't stop there. This morning I found that Kevin Hodgson had started to follow me. It caught my eye that he lives in Western Mass, but as I wandered about the last few tweets in his timeline, I found interesting clues to just what #clmooc is up to, and also found a great list of Techno Skills, written by Kevin Kelley, which I shared with my Catalan tweeps. Hopefully, one of them will be inspired to translate the list into Catalan so it can be shared further.

One of Kevin Hodgson's links also got me thinking about Minecraft which helped me realize I should probably not freak out quite so much about how much time dear daughter spends with it. Indeed, encourage her even. And so it goes full circle, while leaving and sharing a little bit at every stop.

Bonus. This is not the first time I've been depicted in toys. I met another Twitter friend, Maia Weinstock after reading in her bio that she makes "science and music personalities come to life in LEGO". (I love Lego.) Then I noticed she retweeted my Catalan stuff, and it turns out she's part Catalan too! We met in person for the first time for a Sant Jordi event at Harvard University in April, when she presented me with a Liz Castro minifig. Totally honored!

Friday, March 23, 2012

Centering in ebooks across ereaders

A long time ago, I realized that the most basic parts of childrearing—eating, sleeping, and peeing and pooping—were by far the hardest to figure out. So it is with ebooks. It seems almost ridiculous to have to write a big, long complicated post about centering—something so essential and so fundamental—but I keep bumping up against it, so write I will. I hope I'll save you some sleepless nights.

Centering in ebooks involves CSS, the box model, inheritance, and ereader inconsistencies. The main problem with centering lies in not understanding how these aspects relate to each other.

The text-align property

I'll start with the simple. There is a CSS property called text-align whose possible values are left, right, justify, and center. But there is one very important caveat: text-align only affects inline-level objects within the block-level objects to which text-align has been applied. Let's look at that closely, since it is the source of many misunderstandings and thus errors.

Here is a simple p element that contains text.

<body>
<p class="center">Here is some text that should be centered</p>
</body>


And here is the CSS:

.center {text-align:center}

And here is what it looks like:

Preview: centeringex.html

You may think that it is the p element that has been centered, but you'd be wrong. It's actually the inline contents of the p element, an anonymous inline block, if you can stand the CSS parlance, or simply the text itself (if you can't), that is being centered. The p element itself is not centered.

How can you tell? We'll set the width of the p element to 60%, and then set the background color to yellow, so you can actually see it.

.center {text-align:center; width: 60%; background: yellow}

Preview: centeringex.html

You can now clearly see that the p element is still aligned left (which is the default in most browsers and ereaders) even while the inline text that it contains is aligned center within the p element.

Centering block-level elements

So how do you center a block-level element? The canonical way is to set the right and left margins to auto. According to the CSS spec, when both left and right margins are auto, they should be equal, and thus the item will be centered. And don't forget that unless your block element has a width that is smaller than the window size, you won't be able to tell if it's centered or not.

So, to center our p element, we'd have this:

.center {text-align:center; width: 60%; background: yellow; margin-right: auto; margin-left:auto}

And we would get this:

Preview: centeringex.html

This works on most major browsers and ereaders:

Centering-iBooks iPad

And is converted properly for Kindle Fire:

Kindle Fire Previewer - CenteringAcrossEreaders

(Regular Kindle doesn't support width and thus can't center a block-level element at all.)

CSS bug in ADE, NOOK, etc.

Unfortunately, ADE, and the ereaders based on it (like NOOK, NOOK Color, and presumably Sony Reader though I didn't test this last one), don't properly interpret the auto value for left and right margins, and the result is that the blocks are not centered:

NOOK Color centering

Independence of alignment of contents and of the block that contains them

Before we get to the solution, I just want to underline how the text-align properties and centering of the block level elements using automatic right and left margins are independent of each other.

That is, the alignment of the inline-level elements within a block-level element need not match the alignment of the block-level element. That is, we can center the p element within the window and then left-align the contents of the p element:

.mismatch {text-align: left; width: 40%; background: red;margin-right: auto; margin-left:auto }

And that should look like this:

Aligned left within centered block

Notice how the image and the text are aligned to the left within a box that is centered on the page.

Again, ADE, NOOK, and presumably Sony Reader, don't properly interpret the right and left margins set to auto:

NOOK centering mismatch

The Solution

The solution hinges on the fact that text-align only affects inline elements. (This is why simply enclosing an inner div in an outer div whose text-align property is set to center will not work.) We'll enclose the p elements in a wrapper div, whose text-align property we'll set to center, and then we'll set the display property for the p elements themselves to inline-block. The inline-block value is sort of a hybrid between inline and block, that for our purposes keeps the p elements starting their own paragraphs (like regular block-level elements) but allows them to be affected by text-align (like inline-level elements).

Here's the CSS with the new bits in bold:

.wrap {text-align:center}
.center {text-align:center; width: 60%; background: yellow; display:inline-block}
.mismatch {text-align: left; width: 40%; background: red;display:inline-block }


And here's our new HTML:

<div class="wrap">
<p class="center">Here is some text that should be centered</p>
<p class="mismatch"><img src="momo.jpg" alt="momo" width="200" height="150" /><br />This image and text will be aligned to the left within the centered block that contains them.</p>
</div>


Let's look first at iBooks on iPhone: (I've put it in landscape orientation just so it's easier to see, it looks the same in portrait.)

iPhone Centering

Everything is as it should be. Our boxes are centered, but the contents within them are centered and left-aligned respectively.

How about NOOK Color?

NOOK Color-centering inline-block

NOOK Color also likes this solution, even when Publisher Defaults are turned off (although then you have other margin issues).

And Kindle Previewer converts the file just fine for KF8 and the Kindle Fire ("old" Kindle can't center divs but maintains the text-alignment for the inline level elements):

Kindle Fire Previewer - CenteringAcrossEreaders6

But what happens when you view the ebook in iBooks on iPad?

iBooks iPad Centering

The boxes are centered as desired. But the text and image that should be left aligned in the lower box are now justified! Look at those ugly spaces between the words. What happened?

Now we've bumped up against iPad's default overrides. Remember that by default, Full Justification is set to ON, hidden away in the iBooks area of the Settings panel. I don't think most people who use iBooks will ever know this setting exists so ebook designers have to plan for it. You used to be able to add a span tag around the text that you wish to have aligned to the left, but that hack no longer works.

Thankfully, there is still a workaround. If you add the com.apple.ibooks.display-options.xml file to the META-INF folder, with the <option name="specified-fonts">true</option>, the justification that you choose in your CSS will be maintained, regardless of the Full Justification setting chosen. The user can still override your choices by choosing a different font in the Fonts menu, but at least the book will start out the way you want it:

iBooks iPad

To resume:

The text-align property only applies to inline elements within the block-level element to which the property is applied. It is inherited by block level elements that may be enclosed in the outer block-level element and in turn applied to the inline elements that the inner block level elements contain. If you need more info on block level vs inline, see the CSS spec or my my HTML/CSS book.

The canonical way to center a block-level element is by setting both margin-right and margin-left to auto. The fact that this is not supported by ADE or NOOK (and presumably not by Sony Reader or other ereaders based on ADE) is a serious bug.

You can center block-level elements by wrapping them in another block level element (a div), setting the text-align to the outer div to center, and then setting the display property of the inner div (or p or whatever) to inline-block.

iBooks on iPad has a default setting of Full Justification for text, which you can override by adding the com file to the META-INF folder. This has ramifications with respect to font choice, of course, as well.

iBooks on iPhone has the Full Justification setting, but by default it is set to OFF (presumably because of the narrow screen).

This should be much simpler.

And yes, it's true that I have no AT&T coverage in my office!


Wednesday, June 23, 2010

Apple kills fonts in iBooks, strikes blow to standards


Oh Apple, what are you doing? So misguided. You add DRM to all your ebooks. And now, you have crippled iBooks 1.1 so that it won't recognize fonts applied with perfectly standard CSS to any body, p, div, or span element.

Your guidelines state that ebook designers should not choose fonts, stating that it "creates a bad user experience". You are wrong. Apple designers choose fonts for everything Apple does. Because fonts matter. Indeed, you have chosen the fonts for iBooks and for the iPad. And now you have chosen to keep ebook designers from choosing the body font for their ebooks. It is a very shortsighted decision.

The ePub specification requires that conforming ereaders, like yours purports to be, support font-family, among other CSS 2.0 properties. Indeed, you do support font-family for most inline elements, like b, em, code, and even some block level elements like dl and li. Why then not the biggies: p, div, and span?

Your desire for control will ultimately break these standards or it will break iBooks. It will break standards as it incites designers to use ugly hacks to overcome iBooks' broken support for standards. It will break iBooks as people design beautiful standards-compliant ebooks that look great in other readers that support standards.

Or should we just go back to Internet Explorer 5?

Here is a screenshot of a number of elements, all of which are styled with the following declaration: {font-family: sans-serif}. Here is the XHTML file and the perfectly standard CSS on which the ePub is based. Click on it to see, in a standards-compliant browser, what it should look like (hint: everything should display in a sans-serif font). Or download the ePub file itself to your own iPad.

Apple kills fonts, and strikes blow to standards

[updated June 24, 9am] Just in case it wasn't clear, the example above is not meant to be an ebook design, for goodness sake! It is the simplest possible example that shows which elements can be modified with font-family in iBooks 1.1. The "Ew" in the screenshot means "iBooks is disregarding standard CSS" NOT "I don't like serif fonts".

Tuesday, March 30, 2010

Avoiding the eReader Wars - A Call for ePub Standards

As a veteran of the Browser Wars between Internet Explorer and Netscape Navigator in the 1990’s, I’m growing increasingly concerned about the potential upcoming conflict between eReaders.

The other day I read on Ibis Reader’s blog that they intentionally “try to override the following CSS properties when used on an iPhone” and other devices:
  • left and right padding and margin
  • width
  • font-size
  • font-family
Even when visitors are using the web version of Ibis Reader, they discourage the use of padding and margin, width, min-width, max-width, background-image (and friends), and absolute positioning.

These last two seem reasonable, as they are already excluded from the official OPS spec, but the first set smack of paternalistic design sensibilities that, frankly, get my goat. I know they mean well, but frankly, no thank you.

And I don’t mean to single out Ibis Reader—which in many other ways is a very elegant solution. Every eReader I’ve seen to date would rather reformat your carefully crafted ePub document than trust you to have designed your book on purpose.

I truly believe that one of the reasons that the Web took off like it did was because there were no authorities on high dictating elitist rules of design. If you were bent on making a hideous page, there was no browser that was going to stand in your way, or choose more “appropriate” fonts or colors to save you from embarrassment.

And although there may have been a number of ugly pages at the beginning, this lack of censorship also lay the groundwork for a most beautiful explosion of democracy. Anyone could create a web site. And everyone did.

Enter the eBook. Somehow, eReaders have decided that those of us who want to create our own eBooks shouldn't be able to design them the way we want. They’re afraid we may choose ugly formatting: perhaps brash fonts or large indents that make our ePubs hard to read. Oh, the horrors! The solution they offer is to ignore the formatting that book designers have chosen—across the board!—and instead, apply their own styles to our eBooks.

There are numerous problems with this approach. Most importantly, no designer is spared. Whether your design is beautiful or hideous, every eReader I’ve seen will ignore it. There will be no creativity allowed or tolerated.

Second, I imagine it won’t surprise you to hear that each eReader ignores the book design from the ePub file in its own special way. Stanza strips out almost everything, Adobe Digital Editions likes tables, but not small caps, Ibis Reader overrides the properties outlined at the beginning of this article. We’ll see on Saturday how Apple’s iBooks app for the iPad will treat formatting from ePub files, but I can’t deny I’m pessimistic.

This means that book designers will have very little control over how a book is laid out in each eReader, and that in addition, that layout will change from eReader to eReader.

A much more sane approach would be for book designers and eReader software manufacturers to agree to follow the OPS spec. If the OPS spec says a particular CSS property should be supported, the eReader should support it and not assume it knows better. Book designers should be able to rely on the OPS spec to determine which CSS properties are allowed in the final design.

If eReaders are so scared of ugly books, they should add a single “Override Original Design” option and let readers, human readers, decide.

Otherwise, it is just a matter of time before some enterprising software developer (Marc Andreessen, where are you?) comes up with an eReader that does allow you to, say, add video, background images, and whatever else a book designer wants to add to their ePub-based eBooks—and a book reader might want to have there. And by then, it will be too late, the eReader Wars will have begun.

Wednesday, February 10, 2010

The Importance of Character Encodings

I'm amazed at how many non-supported characters I see out on the Web. I spent a long time researching and deciphering just how to use the proper character encoding so that this doesn't happen to you. You can read all about it in Chapter 21, Symbols and Non-English Characters, of my HTML, XHTML, and CSS: Visual QuickStart Book.

Doing some research on Barnes and Noble's new electronic book reader, the nook, I saw this:
Wrong Character Set on Barnes and Noble site

Looking at the source code, you can see that B&N did not declare their character encoding. Firefox assumed it was UTF-8, but since it wasn't, the special symbols display incorrectly. If you're just a Web site visitor and want to see the page without those annoying question marks, go to View > Character Encoding, and choose a different encoding from the one Firefox tried originally. If that happens to match the character encoding that the Web site creator used, you're in luck. If not, try again until you find it.

If you're a Web site designer, be sure and declare your page's Character Set so that browsers don't have to guess. At the top of your page, put:

<meta http-equiv="content-type" content="text/html;charset=code" />

where code is the character set encoding you used to write your page. I explain how to tell which one you used (and how to choose an appropriate one if you're not already), in my book!

The Importance of Character Encodings

I'm amazed at how many non-supported characters I see out on the Web. I spent a long time researching and deciphering just how to use the proper character encoding so that this doesn't happen to you. You can read all about it in Chapter 21, Symbols and Non-English Characters, of my HTML, XHTML, and CSS: Visual QuickStart Book.

Doing some research on Barnes and Noble's new electronic book reader, the nook, I saw this:
Wrong Character Set on Barnes and Noble site

Looking at the source code, you can see that B&N did not declare their character encoding. Firefox assumed it was UTF-8, but since it wasn't, the special symbols display incorrectly. If you're just a Web site visitor and want to see the page without those annoying question marks, go to View > Character Encoding, and choose a different encoding from the one Firefox tried originally. If that happens to match the character encoding that the Web site creator used, you're in luck. If not, try again until you find it.

If you're a Web site designer, be sure and declare your page's Character Set so that browsers don't have to guess. At the top of your page, put:

<meta http-equiv="content-type" content="text/html;charset=code" />

where code is the character set encoding you used to write your page. I explain how to tell which one you used (and how to choose an appropriate one if you're not already), in my book!

Monday, November 30, 2009

Why is position:relative necessary when you're positioning absolutely?

So I'm reading Dan Cederholm's new book, Handcrafted CSS: More Bulletproof Web Design). I'm really liking it.

There was a small example that deals with absolute positioning that I want to highlight. He's talking about formatting a list of coffee drinks that have a title and a price. The goal is to have the link be the entire line so that it makes it easy for readers to click on their desired cup of coffee.

The original code looks like this:

<ul class="lst">   
<li><a href="#"><em>2.79</em> Latte</a></li>
<li><a href="#"><em>2.99</em> Cappuccino</a></li>
<li><a href="#"><em>1.80</em> Cafe Americano</a></li>
<li><a href="#"><em>2.00</em> Espresso</a></li>
<li><a href="#"><em>10.49</em> Caramel Macchiato</a></li>
</ul>


Cederholm suggests using absolute positioning, with code much like the following (his is part of a larger web page and so I've adjusted it to appear on its own):

ul.lst li {
     list-style-type:none
     }

ul.lst li a   {
     position: relative;
     display:block;
     padding: 7px;
     border-bottom: 1px solid #f3f2e8;
     width:200px;
     text-decoration:none;
     font: .9em Verdana;
     }
    
ul.lst li a:hover {
     background-color:#e0e0e0;
     }
   
ul.lst li a em  {
     position: absolute;
     right: 7px;
     top: 7px;
     font-style:normal;
     }


The result can be seen here.

And though Cederholm says the position:relative line is necessary, he doesn't explain why. The reason is that when you position an element absolutely—in this case the coffee prices, which are em elements—their position is calculated with respect to the nearest positioned ancestor, or if there is none, to the body element. Since he wants the prices to go in the same line as the a element that contains them, he must position the a element. By adding position:relative to the definition for the a element, it becomes positioned, and any descendants that are absolutely positioned will be positioned relative to it. This happens even though he's not defining any offsets; the a elements stay in their natural place in the flow.

You can find more information about relative and absolute positioning on pages 178-179 in my HTML, XHTML, and CSS, Sixth Edition.

Also note that Cederholm concludes this example by showing how absolute positioning is not very flexible for text elements. That is, when the text is enlarged, the name of the coffee and its price overlap each other. Instead, absolute positioning makes more sense if one of the pieces is a static image whose size can always be accounted for.

Tuesday, November 18, 2008

Learning HTML in Chinese, or Spanish, or French, or...

My publisher sent me a Chinese edition of my HTML book today... I confess I wasn't quite sure what language it was just by looking at the cover:

My books in many languages

And I truly love seeing my books in different languages. I've always been interested in languages (apart from English, I speak Catalan, Spanish, French, and Italian, with varying success), so it's fun to see what the other languages look like, especially when I can sometimes pick out what they're talking about from the context. And I love seeing my screenshots and photographs translated:

HTML, XHTML, and CSS in other languages HTML, XHTML, and CSS in other languages HTML, XHTML, and CSS in other languages

My books have been translated into a lot of languages. (And that list isn't even up to date!)

But while I love to have one copy, my publisher usually sends me several.

So I'd like to share the extras with you. Hey, the holidays are coming. Maybe you know someone (or are someone) who would like one of my books in a foreign language. All you have to pay is the postage. USPS.com tells me it's $5 within the US, $10 for Canada and Mexico and $12 anywhere else.

OK, there's one more thing you have to do: something nice for someone else. You tell me what you've done, send me the postage (in cash or US stamps, please), and I'll send you the book.

(Want an English one? Read down to the very bottom.)

Here's what I've got left:

HTML, XHTML, and CSS, 6th edition: Czech (2), Chinese (2), and Spanish (3)
HTML 4th edition: French (2)
Creating a Web Page with HTML: Italian (2), German (2)

XML for the World Wide Web, 1st edition: Italian (2), French (1)

Publishing a Blog with Blogger: Italian (1), Swedish (2), French (1)

Perl and CGI, 2nd edition: Japanese (2)

If you're interested, leave me a comment with your email, or use my contact form.

And as a special bonus, I'll give the first person who can identify all three of the languages in the screenshots above any book of mine they like, postage paid, in English!

More of my books