Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS3 ch unit inconsistent between IE9+ and other browsers

IE9+ claims to support the ch CSS unit. By the definition, this unit is 'Equal to the advance measure of the "0" (ZERO, U+0030) glyph' of the current font, or, in simpler words, the width of the character box for the glyph for “0”. This interpretation seems to be right for Firefox 10+ and Chrome 27+: <div style="width: 10ch;"></div> and <div>0000000000</div> have exactly the same width given they have the same font of the same size. But in IE9+ the ch unit seems to mean something slightly different.

Here is the fiddle demonstrating this issue: http://jsfiddle.net/CNsPg/6/

What is the logic of the behavior of IE with this unit? Or is it just a bug? Is it possible to make IE treat ch unit like other browsers (probably with some IE-specific text rendering "magic")?

like image 632
Ilya Streltsyn Avatar asked Jul 24 '13 05:07

Ilya Streltsyn


3 Answers

You have to create a IE-specific CSS that overrides all rules including ch units and multiply them by 1.162 for a monospace font.

This value may change with another font so you may have to calculate the ratio by measuring the actual width in pixels for 100x0 and a div with width: 100ch.

Normal browser (default) :

input[type="text"][size="10"] { width: 10ch }

IE11 :

input[type="text"][size="10"] { width: 11.62ch }
like image 23
NicolasBernier Avatar answered Sep 30 '22 14:09

NicolasBernier


Looking at the fiddle, it's an interesting case of differing interpretations of the spec.

The spec itself is somewhat .... uh.... brief; I can see room for both interpretations.

The spec says:

ch unit
Equal to the advance measure of the "0" (ZERO, U+0030) glyph found in the font used to render it.

So 'ch' is the width of an "0". The question that isn't answered in the spec is whether that should include the spacing around the character:

  • Should it be consistent with the 'em' unit (which is the width of the 'm', including font spacing)?

  • Or should it be consistent with 'ex', which is the height of the 'x' character (without any spacing considerations; ie just the actual glyph)?

I think I know which one I'd pick if I was writing a browser, but as I say I can see an argument for both cases given the lack of clarity in the spec.

I guess in the face of that kind of ambiguity, and with two clearly different implementations between browsers, the only sensible option is not to use the 'ch' unit at all until there is better consensus.

Thankfully 'em' and 'ex' are available and consistent, and also provide font-relative sizing. I guess under the circumstances, the best advice I can give is to stick with them.

The only way I can think of to work around it would be to have a custom font with a default of zero letter spacing. My thought process is that this should remove the ambiguity between the interpretations, but would mess up other aspects of your font rendering somewhat. I guess you could then set the letter spacing manually, but you'd still probably have other issues. Not a solution I'd reall suggest trying, to be honest.

like image 43
Spudley Avatar answered Sep 30 '22 12:09

Spudley


According to caniuse.com, IE interprets 1ch as the width of the 0 glyph, without the surrounding space. This makes 1ch shorter in IE compared to any other browser.

like image 92
damd Avatar answered Sep 30 '22 14:09

damd