line-height 不适用于 ::before 伪元素
似乎我用 ::before 测试的每个属性都有效,除了行高。它在浏览器之间是一致的。我在规范中没有看到这一点。我做错了什么吗?如果没有,是否有一个干净的解决方法?我想要 div 主体和 ::before 的行高不同。编辑:经过进一步研究,如果行高大于 DIV 行高,但不小于,它似乎有效。这绝对看起来像一个错误。我添加了第四个 DIV 来演示这一点。
HTML:
<div id="content1">content<br />content<br />content<br /></div>
<div id="content2">content<br />content<br />content<br /></div>
<div id="content3">content<br />content<br />content<br /></div>
<div id="content4">content<br />content<br />content<br /></div>
CSS:
div {
display: inline-block;
height: 150px;
line-height: 20px;
width: 110px;
}
div::before {
color: red;
content: "before \a before \a before \a";
font-family: courier new;
font-size: 10px;
letter-spacing: 10px;
white-space: pre;
}
#content1::before {
line-height: 10px;
}
#content2::before {
line-height: 8px;
}
#content3::before {
line-height: 6px;
}
#content4::before {
line-height: 30px;
}
It seems every property I tested with ::before works except line-height. It's consistent across browsers. I don't see this in the spec. Am I doing something wrong? If not, is there a clean workaround for this? I would like a different line-height for the body of the div and the ::before. EDIT: After further research, it seems it works if the line-height is larger than the DIV line-height, but not smaller. This definitely seems like a bug. I added a fourth DIV to demonstrate this.
HTML:
<div id="content1">content<br />content<br />content<br /></div>
<div id="content2">content<br />content<br />content<br /></div>
<div id="content3">content<br />content<br />content<br /></div>
<div id="content4">content<br />content<br />content<br /></div>
CSS:
div {
display: inline-block;
height: 150px;
line-height: 20px;
width: 110px;
}
div::before {
color: red;
content: "before \a before \a before \a";
font-family: courier new;
font-size: 10px;
letter-spacing: 10px;
white-space: pre;
}
#content1::before {
line-height: 10px;
}
#content2::before {
line-height: 8px;
}
#content3::before {
line-height: 6px;
}
#content4::before {
line-height: 30px;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须在
div
上定义line-height
,因为div:before
采用div
line-高度
,而不是:div
之前。所以,这样写:检查一下: http://jsfiddle.net/sandeep/weGGn/ 3/'
You have to define
line-height
on thediv
, becausediv:before
takesdiv
line-height
, not:before div
. So, write like this:Check this: http://jsfiddle.net/sandeep/weGGn/3/'
@sandeep 回答的一些补充:
行高属性显然在大多数新浏览器中的工作方式相同。您可能会遇到 Windows Phone 浏览器的问题(即使用 Nokia Lumia 920) 选择元素的 line-height 属性并将其应用于两者元素及其伪元素(:before, :after)。
所以底线是,您在元素本身中分配 line-height 的值,并使用 line-height:inherit 来作为跨浏览器的伪元素。您可能还需要尝试一下,看看它是否适合您。
Some additions to @sandeep answer:
Line-height property is apparently working the same in most of the new browsers. You might face a problem with Windows Phone browser (i.e. using Nokia Lumia 920) which picks the element's line-height property and applies it to both the element and its pseudo (:before, :after).
So the bottom line is, you assign the line-height's value in the element itself and use line-height: inherit for the pseudo that works as a cross-browser thing. You may need to try it also and see if it works for you.