CSS 伪元素

本文介绍了CSS伪元素的基础语法及各种伪元素的应用实例,包括:first-line、:first-letter、:before和:after等,并列举了常见伪类和伪元素的选择器。

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

语法

   伪元素的语法:

selector:pseudo-element {property:value;}

   CSS类也可以使用伪元素:

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

 

:first-line 伪元素

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

   在下面的例子中,浏览器会根据 "first-line" 伪元素中的样式对 p 元素的第一行文本进行格式化:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8"> 
		<title>自学教程(如约智惠.com)</title> 
		<style>
			p:first-line
			{
				color:#ff0000;
				font-variant:small-caps;
			}
		</style>
	</head>
	<body>
		<p>你可以使用 "first-line" 伪元素向文本的首行设置特殊样式。</p>
	</body>
</html>

注意:"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"伪元素用于向文本的首字母设置特殊样式:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8"> 
		<title>自学教程(如约智惠.com)</title> 
		<style>
			p:first-letter
			{
				color:#ff0000;
				font-size:xx-large;
			}
		</style>
	</head>
	<body>
		<p>你可以使用 "first-letter" 伪元素向文本的首字母设置特殊样式。</p>
	</body>
</html>

 

注意: "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">文章段落</p>.article:first-letter {color:#ff0000;}

<p class="article">文章段落</p>

 

多个伪元素

 

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

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8"> 
		<title>自学教程(如约智惠.com)</title> 
		<style>
			p:first-letter
			{
				color:#ff0000;
				font-size:xx-large;
			}
			p:first-line
			{
				color:#0000ff;
				font-variant:small-caps;
			}
		</style>
	</head>
	<body>
		<p>你可以结合使用"first-line"和"first-letter"伪元素向文本的首行和首字母设置特殊样式。</p>
	</body>
</html>

 

CSS-:before 伪元素

 

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

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8"> 
		<title>自学教程(如约智惠.com)</title> 
		<style>
			h1:before {content:url("logocss.gif");}
		</style>
	</head>
	<body>
		<h1>This is a heading</h1>
		<p>The :before pseudo-element inserts content before an element.</p>
		<h1>This is a heading</h1>
		<p><b>注意:</b>仅当 !DOCTYPE 已经声明 IE8 支持这个内容属性</p>
	</body>
</html>

CSS- :after 伪元素

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

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8"> 
		<title>自学教程(如约智惠.com)</title> 
		<style>
			h1:after {content:url("logocss.gif");}
		</style>
	</head>
	<body>
		<h1>This is a heading</h1>
		<p>The :after pseudo-element inserts content after an element.</p>
		<h1>This is a heading</h1>
		<p><b>注意:</b>仅当 !DOCTYPE 已经声明 IE8 支持这个内容属性</p>
	</body>
</html>

所有CSS伪类/元素

选择器示例示例说明
:linka:link选择所有未访问链接
:visiteda:visited选择所有访问过的链接
:activea:active选择正在活动链接
:hovera:hover把鼠标放在链接上的状态
:focusinput:focus选择元素输入后具有焦点
:first-letterp:first-letter选择每个<p> 元素的第一个字母
:first-linep:first-line选择每个<p> 元素的第一行
:first-childp:first-child选择器匹配属于任意元素的第一个子元素的 <]p> 元素
:beforep:before在每个<p>元素之前插入内容
:afterp:after在每个<p>元素之后插入内容
:lang(language)p:lang(it)为<p>元素的lang属性选择一个开始值

 

参考:

https://www.yuque.com/docs/share/a615db62-cf36-43f5-ab77-12196da7da78

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值