html5中content属性,详解css中的content属性的用法

本文详细介绍了CSS的content属性在::before和::after伪元素中的用法,包括如何插入纯字符、图片以及元素的属性。通过实例展示了如何利用content属性实现元素编号的动态插入,以及如何改变元素编号的样式。对于前端开发者理解并掌握CSS伪元素的高级用法具有指导意义。
摘要由CSDN通过智能技术生成

251cd18a9047339dc81a53e5431d1b87.png

content属性一般用于::before、::after伪元素中,用于呈现伪元素的内容。

用法详解

1、插入纯字符

c5bcc257d2d292c6833e893fd7aa9e81.png

*{margin: 0;padding: 0;box-sizing: border-box;}

li{list-style: none;}

.content{

position: relative;padding: 10px;

border: 1px solid #666;margin: 10px;

}

.content.only-text::before{

content: '插入纯字符';

}

1、插入纯字符

(视频教程推荐:css视频教程)

2、插入图片

b0a6cdf3a849138e9541490178abc8fb.png

*{margin: 0;padding: 0;box-sizing: border-box;}

li{list-style: none;}

.content{

position: relative;padding: 10px;

border: 1px solid #666;margin: 10px;

}

.content.fill-image::before{

content: url('https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo_top_86d58ae1.png');

}

2、插入图片

3、插入元素属性

96288eeb9e134bd74aca6f586a8c8fc0.png

*{margin: 0;padding: 0;box-sizing: border-box;}

li{list-style: none;}

.content{

position: relative;padding: 10px;

border: 1px solid #666;margin: 10px;

}

.content.fill-dom-attr::before{

content: attr(data-title);

}

3、插入元素属性

4、插入当前元素编号(即当前元素索引)

这个特性可用于活动页面的规则介绍。

af5780f14bd9e5e590a8f0b5006cc1f5.png

*{margin: 0;padding: 0;box-sizing: border-box;}

li{list-style: none;}

.content{

position: relative;padding: 10px;

border: 1px solid #666;margin: 10px;

}

.fill-dom-index li{

position: relative;

/* 给计数器加器起个名字,它只会累加 li 标签的索引,li元素中间的div并不会理会 */

counter-increment: my;

}

.fill-dom-index li div::before{

/* 使用指定名字的计算器 */

content: counter(my)'- ';

color: #f00;

font-weight: 600;

}

4、插入当前元素编号(即当前元素索引)

  • 我是第1个li标签
我是li标签中的第1个div标签
我是第2个li标签
我是第3个li标签
我是li标签中的第2个div标签
我是第4个li标签
我是第5个li标签

5、插入当前元素编号(指定种类)

9b3fde53c40d785be7d18ff6b0ec759a.png

*{margin: 0;padding: 0;box-sizing: border-box;}

li{list-style: none;}

.content{

position: relative;padding: 10px;

border: 1px solid #666;margin: 10px;

}

.fill-dom-index2 li{

position: relative;

counter-increment: my2;

}

.fill-dom-index2 li div::before{

/* 第二个参数为list-style-type,可用值见: http://www.w3school.com.cn/cssref/pr_list-style-type.asp*/

content: counter(my2,lower-latin)'- ';

color: #f00;

font-weight: 600;

}

5、插入当前元素编号(指定种类)

  • 我是第1个li标签
我是li标签中的第1个div标签
我是第2个li标签
我是第3个li标签
我是li标签中的第2个div标签
我是第4个li标签
我是第5个li标签
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值