如何将文本缩进一个字符宽度
前提是我使用 monospace 作为我的 font-family,但它似乎不能正常工作,我尝试了一些解决方案,但其中一些都有效,我的HTML结构如下:
<!DOCTYPE html>
<style>
body {
font-family: monospace;
letter-spacing: 0;
word-spacing: 0;
font-size: 32px; /* large enough to see the effect */
}
div:last-of-type {
padding-left: 1em; /* what's the value? */
}
</style>
<div>123456</div>
<div>abcdef</div>
使用em
em 应等于计算的字体大小值,但 padding-left: 1em; 不起作用:
使用px
padding-left: 32px; 的输出与 padding-left: 1em; 相同。
使用ex
ex 应该是计算出的字母“x”的高度,但它也不起作用:
使用ch
好吧,webkit 不支持 ch 作为 CSS 单元。
那么我该如何编写css来精确缩进第二个div一个字符宽度,即第一个'0'应该与字母'b'左对齐,没有任何偏差。
The precondition is that I use monospace as my font-family, but it doesn't seem to work properly, I've tried some solution but neight of them work, my HTML structure is as below:
<!DOCTYPE html>
<style>
body {
font-family: monospace;
letter-spacing: 0;
word-spacing: 0;
font-size: 32px; /* large enough to see the effect */
}
div:last-of-type {
padding-left: 1em; /* what's the value? */
}
</style>
<div>123456</div>
<div>abcdef</div>
use em
em should be equals to the computed font-size value, but padding-left: 1em; doesn't work:
use px
padding-left: 32px; makes the same output as padding-left: 1em;.
use ex
ex should be the computed height of the letter 'x', and it doesn't work either:
use ch
OK, webkit doesn't support ch as a css unit.
So how can I write the css to exactly indent the second div one character width, that is, the first '0' should be left-aligned to the letter 'b', without any deviation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种可能的方法,虽然有点 hacky,是使用
:before
伪选择器和content
在行前插入一个空格:我不知道哪些浏览器支持这个,但我假设所有现代浏览器都会。
http://jsfiddle.net/cavqM/
One possible way, although a bit hacky, would be to insert a space before the row using the
:before
pseudo selector withcontent
:I have no idea as to which browsers support this, but I'd assume all modern browsers would.
http://jsfiddle.net/cavqM/
基于 Tatu 的方法,您可以使用不可破坏空间的 unicode 表示,例如
Based on the Tatu's approach you can use a unicode representation of a none breakable space like