html伪元素::first-letter用法

51次阅读
没有评论

HTML 伪元素 ::first-letter 可以选中元素的第一个字母或字母序列,并应用样式,通常用于添加特殊效果,如首字下沉等。下面是一个使用 ::first-letter 的示例 HTML 页面:

<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>::first-letter Demo</title>
	<style>
		.text::first-letter {
			font-size: 24px;
			font-weight: bold;
			color: red;
		}
		.text {
			font-size: 16px;
			line-height: 1.5;
		}
	</style>
</head>
<body>
	<p class="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis semper, quam sed sagittis vulputate, massa enim aliquam enim, vel malesuada metus lorem eu ipsum.</p>
</body>
</html>

上述示例中,我们定义了一个类名为 text 的段落元素,并使用 ::first-letter 伪元素选中了它的第一个字母,并为这个字母设置了特殊样式。我们为 ::first-letter 伪元素设置了 font-size 为 24px,font-weight 为 bold,color 为 red,这会使选中的第一个字母变得更大、加粗、变成红色。我们同时为 text 元素设置了 font-size 和 line-height 用于设置整个段落的样式。

运行上述示例,我们将会看到一个包含一段文字的段落,其中第一个字母被选中,并且字母变得更大、加粗、变成红色。这是因为我们使用了 ::first-letter 伪元素,选中了段落的第一个字母,并为它设置了特殊样式。

正文完
 
评论(没有评论)