CSS 伪元素

CSS 伪元素

CSS伪元素是用来添加一些选择器的特殊效果。

语法

伪元素的语法:

selector:pseudo-element {property:value;}

CSS类也可以使用伪元素:

selector.class:pseudo-element {property:value;}

:first-line 伪元素

"first-line" 伪元素用于向文本的首行设置特殊样式。

注意:"first-line" 伪元素只能用于块级元素。

注意: 下面的属性可应用于 "first-line" 伪元素:

font properties

color properties 

background properties

word-spacing

letter-spacing

text-decoration

vertical-align

text-transform

line-height

clear

:first-letter 伪元素

"first-letter" 伪元素用于向文本的首字母设置特殊样式

注意: "first-letter" 伪元素只能用于块级元素。

注意: 下面的属性可应用于 "first-letter" 伪元素: 

font properties

color properties 

background properties

margin properties

padding properties

border properties

text-decoration

vertical-align (only if "float" is "none")

text-transform

line-height

float

clear

伪元素和CSS类

伪元素可以结合CSS类: 

p.article:first-letter {color:#ff0000;}

<p class="article">A paragraph in an article</p>

上面的例子会使所有 class 为 article 的段落的首字母变为红色。

Multiple Pseudo-elements

可以结合多个伪元素来使用。

在下面的例子中,段落的第一个字母将显示为红色,其字体大小为 xx-large。第一行中的其余文本将为蓝色,并以小型大写字母显示。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>php中文网</title> 
<style>
p:first-letter
{
color:#ff0000;
font-size:xx-large;
}
p:first-line 
{
color:#0000ff;
font-variant:small-caps;
}
</style>
</head>
<body>
<p>You can combine the :first-letter and :first-line pseudo-elements to add a special effect to the first letter and the first line of a text!</p>
</body>
</html>

CSS - :before 伪元素

":before" 伪元素可以在元素的内容前面插入新内容。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>CSS ::before 伪元素示例</title>
<style type="text/css" media="all">
div::before
{
background: lightgreen;
content: "张三";
}
</style>
</head>
<body>
<div>今天来学习知识</div>
</body>
</html>

CSS - :after 伪元素

":after" 伪元素可以在元素的内容之后插入新内容。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>CSS ::after 伪元素示例</title>
<style type="text/css" media="all">
div::after
{
background: lightgreen;
content: "周末";
}
</style>
</head>
<body>
<div>今天是</div>
</body>
</html>


继续学习
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>CSS ::before 伪元素示例</title> <style type="text/css" media="all"> div::before { background: lightgreen; content: "张三"; } </style> </head> <body> <div>今天来学习知识</div> </body> </html>
提交重置代码
  • 推荐课程
  • 评论
  • 问答
  • 笔记
  • 课件下载
我是灰太狼

我是灰太狼

明白什么意思了

4年前    添加回复 0

回复
汪汪

汪汪

很多CSS 伪元素可以选择啊

4年前    添加回复 0

回复
大神,求带!

大神,求带!

添加各种特殊效果

4年前    添加回复 0

回复
看透不说透

看透不说透

这个不错啊,应该会有用,了解下!

4年前    添加回复 0

回复
男神

男神

"first-line" 伪元素用于向文本的首行设置特殊样式。

4年前 0

课件暂不提供下载,工作人员正在整理中,后期请多关注该课程~