问题
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