首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

::first-line

::first-line伪元素将样式应用于块级元素的第一行。请注意,第一行的长度取决于许多因素,包括元素的宽度,文档的宽度以及文本的字体大小。

代码语言:javascript
复制
::first-line {
  color: blue;
  text-transform: uppercase;
}

只有一小部分css属性可以与::first-line伪元素一同使用:

  • 所有字体相关的属性:fontfont-kerningfont-stylefont-variantfont-variant-numericfont-variant-positionfont-variant-east-asianfont-variant-capsfont-variant-alternatesfont-variant-ligaturesfont-synthesisfont-feature-settingsfont-language-overridefont-weightfont-sizefont-size-adjustfont-stretch,和font-family
  • 所有的背景相关的属性:background-colorbackground-clipbackground-imagebackground-originbackground-positionbackground-repeatbackground-sizebackground-attachment,和background-blend-mode
  • color属性
  • word-spacingletter-spacingtext-decorationtext-transform,和line-height
  • text-shadowtext-decorationtext-decoration-colortext-decoration-linetext-decoration-style,和vertical-align

CSS3引入了::first-line符号(用两个冒号)来区分伪类和伪元素。在CSS2中引入后,浏览器也接受:first-line

语法

代码语言:javascript
复制
/* CSS3 syntax */
::first-line

/* CSS2 syntax */
:first-line

实例

HTML

代码语言:javascript
复制
<p>Styles will only be applied to the first line of this paragraph.
After that, all text will be styled like normal. See what I mean?</p>

<span>The first line of this text will not receive special styling
because it is not a block-level element.</span>

CSS

代码语言:javascript
复制
::first-line {
  color: blue;
  text-transform: uppercase;


  /* WARNING: DO NOT USE THESE */
  /* Many properties are invalid in ::first-line pseudo-classes */
  margin-left: 20px;
  text-indent: 20px;
}

结果

规范

Specification

Status

Comment

CSS Pseudo-Elements Level 4The definition of '::first-line' in that specification.

Working Draft

Defines more strictly where ::first-letter can occur. Generalizes allowed properties to typesetting, text decoration, and inline layout properties and opacity. Defines the inheritance of ::first-letter.

CSS Text Decoration Module Level 3The definition of 'text-shadow with ::first-line' in that specification.

Candidate Recommendation

Allows the use of text-shadow with ::first-letter.

Selectors Level 3The definition of '::first-line' in that specification.

Recommendation

Introduction of the two-colon syntax.

CSS Level 2 (Revision 1)The definition of '::first-line' in that specification.

Recommendation

No change.

CSS Level 1The definition of '::first-line' in that specification.

Recommendation

Initial definition, using the one-colon syntax.

浏览器兼容性

Feature

Chrome

Edge

Firefox (Gecko)

Internet Explorer

Opera

Safari (WebKit)

Basic support

1.01

(Yes)

1.0 (1.7 or earlier)

9.0

7.02

1.03

Old one-colon syntax (:first-line)

1.01

(Yes)

1.0 (1.7 or earlier)

5.5

3.52

1.03

Feature

Android

Edge

Firefox Mobile (Gecko)

IE Phone

Opera Mobile

Safari Mobile

Basic support

(Yes)1

(Yes)

1.0 (1)

No support

?

?

Old one-colon syntax (:first-line)

(Yes)1

(Yes)

1.0 (1)

?

?

?

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com