Using letter-spacing
seems to really screw with text-align
: http://jsfiddle.net/NFAzv/
Googling hasn't turned anything up. Am I missing something here?
EDIT #1: Here's what I'm getting (Firefox 3.6.8 and Chrome 12.0.742.91 on Vista): link removed due to link limits for newbies (it can be found in the comments) (notice how the centers do not line up at all)
EDIT #2: Here's IE9, with a centered vertical line to show what's wrong: http://i.stack.imgur.com/C0J0n.png
EDIT #3: I have a confirmation in the comments below that the problem shows up for Chrome 10 on Windows 7. At least I'm not the only one going crazy.
Short answer: your text isn't centered because the elements are floated, and floated elements "shrink" to the content, even if it's a block level element.
letter-spacing also supports negative values, which will tighten the appearance of text, rather than loosening it. According to this definition, 0 should be equivalent to the default value of normal since 0 + X = X.
Letter Spacing The letter-spacing property is used to specify the space between the characters in a text.
It seems that all the browser have converged on a letter-spacing implementation that is plainly at odds with what css3 says should happen.
In particular see Example XV at http://dev.w3.org/csswg/css3-text/#letter-spacing0
Browsers simply don't do this. IE has even changed its behaviour recently (IE9, I think) to be more like the other browsers, and less like the CSS3 spec as it's currently written.
The CSS3 spec in question is still at working draft status, so presumably at some point it will be changed to match what the browsers do.
You may be able to rebalance the lines by adding a padding-left value to match the letter-spacing but that may not always be possible.
Since the problem isn't really described I assume that the text isn't centered perfectly..
You can use text-indent
at half the size of the letter spacing to correct this..
.centered {
letter-spacing:12px;
text-indent:6px;
}
Only tested on Chrome OSX though..
It seems to work, but your requirement for this pixel perfectness does not comply with the default working of letter-spacing
. If you highlight the text, you will see that it does center.
You could use the following workaround: demo fiddle
<div style="text-align: center;">
<div style="font-size: 130%; letter-spacing: 0.6em; padding-left: 0.6em;">THIS</div>
<div style="font-size: 350%; letter-spacing: 0.4em; padding-left: 0.4em;">ILLUSTRATES</div>
<div style="font-size: 130%; letter-spacing: 0.4em; padding-left: 0.4em;">THE</div>
<div style="font-size: 200%; letter-spacing: 0.1em; padding-left: 0.1em;">PROBLEM</div>
</div>
I have seen similar problem with links underline:last letter's width includes the letter-spacing value.
So apply letter-spacing:0;
for the last letters should resolve the issue.
It's ugly but it works
...
<div style="font-size: 350%; letter-spacing: 0.4em;">
ILLUSTRATE<span style="letter-spacing:0;">S</span>
</div>
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With