line-height 不适用于 ::before 伪元素

发布于 2024-12-12 01:52:06 字数 1308 浏览 0 评论 0原文

似乎我用 ::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;   
}

http://jsfiddle.net/ThinkingStiff/weGGn/

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;   
}

http://jsfiddle.net/ThinkingStiff/weGGn/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

菩提树下叶撕阳。 2024-12-19 01:52:06

您必须在 div 上定义 line-height,因为 div:before 采用 div line-高度,而不是:div之前。所以,这样写:

div:before {
    color: red;
    content: "before \a before \a before \a";
    font-family: courier new;
    font-size: 10px;
    letter-spacing: 10px;
    white-space: pre;
}

div {
    line-height: 10px;
}

检查一下: http://jsfiddle.net/sandeep/weGGn/ 3/'

You have to define line-height on the div, because div:before takes div line-height, not :before div. So, write like this:

div:before {
    color: red;
    content: "before \a before \a before \a";
    font-family: courier new;
    font-size: 10px;
    letter-spacing: 10px;
    white-space: pre;
}

div {
    line-height: 10px;
}

Check this: http://jsfiddle.net/sandeep/weGGn/3/'

岁月静好 2024-12-19 01:52:06

@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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文