[英]Letter-spacing wrong text center alignment
I have noticed a odd behavior in using letter-spacing and text-align:center together.我注意到一起使用 letter-spacing 和 text-align:center 的奇怪行为。
Increasing the space, bring the text to be closer to the left margin of the element.增加空间,使文本更接近元素的左边距。
HTML HTML
<div>
<p>- Foo Bar Zan -</p>
</div>
CSS CSS
div {
width: 400px;
height:400px;
background-color: #3b0d3b;
text-align:center;
margin:auto;
}
p {
color:#fff;
margin-top: 40px;
text-align:center;
padding-top:30px;
font-size: 1.2em;
letter-spacing:.9em; <--- Here is the problem
}
I have create a codepen to show what I mean .我创建了一个代码笔来展示我的意思。
I spot the same behavior on last Firefox and Chrome.我在上一个 Firefox 和 Chrome 上发现了相同的行为。 Is there a way to fix this issue?
有没有办法解决这个问题?
It seems you need to indent the text by the same amount as the letter-spacing.似乎您需要将文本缩进与字母间距相同的数量。 The first letter does not have the spacing applied to the left side
第一个字母没有应用于左侧的间距
div { width: 400px; height: 400px; background-color: #3b0d3b; text-align: center; margin: auto; } p { color: #fff; background: black; text-align: center; font-size: 1.2em; padding: 0; margin: 0; } .spacing { letter-spacing: .4em; } .spacing-large { letter-spacing: 0.9em; text-align: center; text-indent: 0.9em; }
<div> <p>- Foo Bar Zan -</p> <p class="spacing">- Foo Bar Zan -</p> <p class="spacing-large">- Foo Bar Zan -</p> </div>
The logical explanation I came up with is - since it is the first letter, spacing on the left side will not apply.我想出的合乎逻辑的解释是 - 因为它是第一个字母,所以左边的间距不适用。
Using padding would be safer incase the text goes onto two lines.使用填充会更安全,以防文本分成两行。 text-indent would only indent the first line of text.
text-indent 只会缩进文本的第一行。
p {
padding-left: 0.9em;
}
If you look closely, the top one without letter spacing is not properly centered as well.如果仔细观察,没有字母间距的顶部也没有正确居中。 The only thing I can think of is to monkey patch it with
margin-left: 15px
like so:我唯一能想到的是用
margin-left: 15px
像这样修补它:
p { margin-left: 15px; }
Set a negative margin the same amount as the letter spacing.将负边距设置为与字母间距相同的量。 So if your letter spacing is 10px, set
margin-right:-10px
.因此,如果您的字母间距为 10px,请设置
margin-right:-10px
。
Using text-indent
will leave that space in front of the first letter, even when the parent element is too small to fit the content, causing it to not look centered.使用
text-indent
会在第一个字母前面留下该空间,即使父元素太小而无法容纳内容,导致它看起来不居中。
This behavior is because there is the extra space (as a result of increased letter spacing) added to the right side of the right most letter.这种行为是因为在最右边的字母的右侧添加了额外的空间(由于增加了字母间距)。
There is a simpler method to achieve center alignment in such cases.在这种情况下,有一种更简单的方法可以实现中心对齐。 Just remove the letter spacing property from the last alphabet
只需从最后一个字母中删除字母间距属性
Here is a solution.这是一个解决方案。
div { width: 400px; height:400px; background-color: #3b0d3b; text-align:center; margin:auto; } p { color:#fff; margin-top: 40px; text-align:center; padding-top:30px; font-size: 1.2em; } .spacing { letter-spacing:.4em; } .spacing-large { letter-spacing:.9em; }
<div> <p>- Foo Bar Zan -</p> <p class="spacing">- Foo Bar Zan <span style="letter-spacing:0">-</span></p> <p class="spacing-large">- Foo Bar Zan <span style="letter-spacing:0">-</span></p> </div>
Notice the span element nesting the last character.请注意嵌套最后一个字符的 span 元素。
The best answer I have found to solve a letter spacing word being off center is to simply put
我发现解决字母间距单词偏离中心的最佳答案是简单地将
in front of the word.在这个词的前面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.