html - 为什么::first-line 不适用于 p/div 标签之类的跨度?

标签 html css css-selectors pseudo-element

我有以下代码:

p::first-line {
  color: #ff0000;
  font-variant: small-caps;
}
span::first-line {
  color: #ff0000;
  font-variant: small-caps;
}
<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>

问题是伪元素适用于 p 标签并将第一行更改为指定的颜色,但同样不适用于 span 标签。

最佳答案

根据 MDN :

A first line has only meaning in a block-container box, therefore the ::first-line pseudo-element has only an effect on elements with a display value of block, inline-block, table-cell or table-caption. In all other cases, ::first-line has no effect.

以下是 W3C Spec 的摘录: (Section 7.1.1 First formatted line definition in CSS)

In CSS, the ::first-line pseudo-element can only have an effect when attached to a block-like container such as a block box, inline-block, table-caption, or table-cell.

span elements are display: inline by default ::first-line 选择器对其没有影响。如果我们将 spandisplay 更改为 inline-blockblock,它将起作用。

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

关于html - 为什么::first-line 不适用于 p/div 标签之类的跨度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35577974/

相关文章:

javascript - 具有重复背景的自定义高亮文本

css - Perfect Circle内部内容高度未知

css - Bootstrap 、网格和滚动

jquery - 更改 Google 自定义搜索上的输入背景颜色

java - 在使用 Selenium 进行测试自动化期间检查元素时,某些网页中的元素 "id"会发生变化(不是静态的)。如何处理这种情况?

javascript - putImageData(),如果新像素是透明的,如何保留旧像素?

javascript - 从 url 中删除 bootstrap 3 选项卡 href 哈希名称

css - 使用第 nth-child 表行的重复范围

html - 为什么 ul > li(选择器)不起作用

javascript - 当 select 标签具有 size 属性时,HTML Select change 事件在 Chrome 中的滚动条点击时触发