语法
::first-letter { css declarations; }
::first-letter 伪元素的例子:
<!DOCTYPE html> <html> <head> <title>Title of the document</title> <style> p::first-letter { font-size: 35px; color: #1c87c9; } </style> </head> <body> <h2>::first-letter selector example</h2> <p>Lorem ipsum is simply dummy text...</p> </body> </html>
带有标点符号和数字的 ::first-letter 伪元素示例:
<!DOCTYPE html> <html> <head> <title>Title of the document</title> <style> p::first-letter { font-size: 35px; color: #1c87c9; } </style> </head> <body> <h2>::first-letter selector example</h2> <p>-把痛苦停在昨天 把微笑留给明天。</p> <p>1把痛苦停在昨天 把微笑留给明天。</p> </body> </html>
允许的属性
以下是可与 ::first-letter 伪元素一起使用的 CSS 属性:
字体属性:font、font-style、font-feature-settings、font-kerning、font-language-override、font-stretch、font-variant、font-variant-alternates、font-variant-caps、font-variant-east -asian, font-variant-ligatures, font-variant-numeric, font-weight, font-size, font-size-adjust, line-height and font-family。
背景属性:background, background-oroirn, background-position, background-repeat, background-size, background-attachment, background-blend-mode, background-color, background-image, and background-clip.
边距属性:margin, margin-top, margin-right, margin-bottom, margin-left.
填充属性:padding、padding-top、padding-right、padding-bottom、padding-left。
边框属性:shorthands, border-style, border-color, border-width, border-radius, border-image, longhands.
颜色属性。
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 (only float: none;).
::first-letter 伪元素将样式放在块级容器中第一行的第一个字母上。
它不选择内联级元素,例如图像或者内联表。
::first-letter 伪元素不识别第一个字母之前或者之后的标点符号。
如果第一个字母实际上是数字,它也适用。
CSS3 规范引入了 ::first-letter 来区分伪类和伪元素。