试一试
p::first-letter {
font-size: 1.5rem;
font-weight: bold;
color: brown;
}
<p>
Scientists exploring the depths of Monterey Bay unexpectedly encountered a
rare and unique species of dragonfish. This species is the rarest of its
species.
</p>
<p>
When Robison and a team of researchers discovered this fish, they were aboard
a week-long expedition.
</p>
识别元素的首字母并非总是那么简单
允许的属性
只有一小部分 CSS 属性可以与 ::first-letter 伪元素一起使用
- 所有字体属性:
font、font-style、font-feature-settings、font-kerning、font-language-override、font-stretch、font-synthesis、font-variant、font-variant-alternates、font-variant-caps、font-variant-east-asian、font-variant-ligatures、font-variant-numeric、font-variant-position、font-weight、font-size、font-size-adjust、line-height和font-family - 所有背景属性:
background、background-color、background-image、background-clip、background-origin、background-position、background-repeat、background-size、background-attachment和background-blend-mode - 所有外边距属性:
margin、margin-top、margin-right、margin-bottom、margin-left - 所有内边距属性:
padding、padding-top、padding-right、padding-bottom、padding-left - 所有边框属性:简写属性
border、border-style、border-color、border-width、border-radius、border-image,以及长写属性 color属性text-decoration、text-shadow、text-transform、letter-spacing、word-spacing(在适当的时候)、line-height、text-decoration-color、text-decoration-line、text-decoration-style、box-shadow、float、vertical-align(仅当float为none时)CSS 属性
语法
css
::first-letter {
/* ... */
}
示例
基本首字下沉
在此示例中,我们将使用 ::first-letter 伪元素对紧随 <h2> 之后的段落的首字母创建首字下沉效果。
HTML
html
<h2>My heading</h2>
<p>
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita
kasd gubergren, no sea takimata sanctus est.
</p>
<p>
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie
consequat.
</p>
CSS
css
p {
width: 500px;
line-height: 1.5;
}
h2 + p::first-letter {
color: white;
background-color: black;
border-radius: 2px;
box-shadow: 3px 3px 0 red;
font-size: 250%;
padding: 6px 3px;
margin-right: 6px;
float: left;
}
结果
对特殊标点符号和非拉丁字符的影响
此示例说明了 ::first-letter 对特殊标点符号和非拉丁字符的影响。
HTML
html
<p>
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie
consequat.
</p>
<p>-The beginning of a special punctuation mark.</p>
<p>_The beginning of a special punctuation mark.</p>
<p>"The beginning of a special punctuation mark.</p>
<p>'The beginning of a special punctuation mark.</p>
<p>*The beginning of a special punctuation mark.</p>
<p>#The beginning of a special punctuation mark.</p>
<p>「特殊的汉字标点符号开头。</p>
<p>《特殊的汉字标点符号开头。</p>
<p>"特殊的汉字标点符号开头。</p>
CSS
css
p::first-letter {
color: red;
font-size: 150%;
}
结果
SVG 文本元素中首字母的样式
在此示例中,我们使用 ::first-letter 伪元素对 SVG <text> 元素的第一个字母进行样式设置。
注意:在撰写本文时,此功能支持有限。
HTML
html
<svg viewBox="0 0 300 40">
<text y="30">First letter in <text> SVG</text>
</svg>
CSS
css
text {
font-family: sans-serif;
}
text::first-letter {
font-family: serif;
font-size: 2rem;
font-weight: 600;
fill: tomato;
stroke: indigo;
}
结果
规范
| 规范 |
|---|
| CSS 伪元素模块 Level 4 # first-letter-pseudo |
浏览器兼容性
加载中…