Cod*_*fee 5 html css css-selectors pseudo-element
我有以下代码:
p::first-line {
color: #ff0000;
font-variant: small-caps;
}
span::first-line {
color: #ff0000;
font-variant: small-caps;
}
Run Code Online (Sandbox Code Playgroud)
<span> This is to check span.This is to check spanThis is to check spanThis is to check spanThis is to check spanThis is to check spanThis is to check spanThis is to check spanThis is to check spanThis is to check span</span>
<p>This is to check p.This is to check p.This is to check p.This is to check p.This is to check p.This is to check p.This is to check p.This is to check p.This is to check p.This is to check p.This is to check p.This is to check p.This is to check p.</p>
Run Code Online (Sandbox Code Playgroud)
问题是伪元素用于p
标记并将第一行更改为指定的颜色,但同样不适用于span
标记.
根据MDN:
第一行仅在块容器框中有意义,因此:: first-line伪元素仅对具有block,inline-block,table-cell或table-caption的显示值的元素产生影响.在所有其他情况下,:: first-line无效.
以下是W3C规范的摘录:( 第7.1.1节CSS中第一个格式化的行定义)
在CSS中,:: first-line伪元素只有在附加到块状容器(如块框,内联块,表格标题或表格单元格)时才能生效.
由于span
要素是display: inline
默认的::first-line
选择上有没有影响.如果我们更改display
为span
to inline-block
或block
,它将起作用.
p::first-line {
color: #ff0000;
font-variant: small-caps;
}
span::first-line {
color: #ff0000;
font-variant: small-caps;
}
span.block {
display: block;
}
span.inline-block {
display: inline-block;
}
Run Code Online (Sandbox Code Playgroud)
<h3>Default Span</h3>
<span> This is to check span.This is to check spanThis is to check spanThis is to check spanThis is to check spanThis is to check spanThis is to check spanThis is to check spanThis is to check spanThis is to check span</span>
<h3>Span with display as block</h3>
<span class='block'> This is to check span.This is to check spanThis is to check spanThis is to check spanThis is to check spanThis is to check spanThis is to check spanThis is to check spanThis is to check spanThis is to check span</span>
<h3>Span with display as inline-block</h3>
<span class='inline-block'> This is to check span.This is to check spanThis is to check spanThis is to check spanThis is to check spanThis is to check spanThis is to check spanThis is to check spanThis is to check spanThis is to check span</span>
<p>This is to check p.This is to check p.This is to check p.This is to check p.This is to check p.This is to check p.This is to check p.This is to check p.This is to check p.This is to check p.This is to check p.This is to check p.This is to check p.</p>
Run Code Online (Sandbox Code Playgroud)