Can you prevent :first-line styles from applying to :after elements in Opera and Chrome?

↘锁芯ラ 提交于 2019-12-11 04:51:26

问题


In Chrome and Opera, it appears that :first-line rules apply to the :after pseudo-element as well, and cannot be overridden with !important or normal CSS weighting.

For example, I have a series of rules on an H2 element like so (jsfiddle here)

CSS

h2.dotted {
    position: relative;
    font-size: 20px;
}
h2.dotted.first:first-line {
    font-size: 30px;
}
h2.dotted:after {
    content: "............................................";
    font-size: 10px !important;
    position: absolute;
    bottom:-1em;
    left: 0;    
    width: 100%;
}

HTML

<h2 class="dotted first">This header has a first-line pseudo-element<br /> 
And its :first-line rules override its :after rules.</h2>
<h2 class="dotted">This header has no first-line pseudo-element<br /> 
And its dots are at the correct size.</h2>

What I would expect (and what happens in IE, FF, and Safari) is that the :after pseudo-element would have a font-size of 10px. Instead, it has a font-size of 30px. Is there a way to correct this behavior?


回答1:


I figured out a way:

h2.dotted:before {
    content: "\A............................................";
    font-size: 10px !important;
    position: absolute;
    bottom:-1em;
    left: 0;    
    width: 100%;
    white-space: pre;
}

The "\A" is the escaped character for a new line. And using white-space: pre forces the dots to be the second line.

fiddle



来源:https://stackoverflow.com/questions/19867655/can-you-prevent-first-line-styles-from-applying-to-after-elements-in-opera-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!