CSS伪类选择器:before、:after使用:插入字符、插入图片、插入项目编号
before: 伪元素选择器用于在某个元素之前插入一些内容
伪类选择器:before使用content属性插入字符、属性插入图片
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta charset="utf-8"> <meta content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=0,minimal-ui"> <meta content="black-translucent"> <meta content="yes" /> <meta content="telephone=no"> <meta content="black"> <meta content="yes"> <meta content="webkit"> <title>css3</title> <style type="text/css"> .p_before:before { content: ‘H‘; color: #01B4EE; } .p_beforeImg { background: #eeeeee; width: 200px; height: 80px; border-radius: 6px; padding: 10px 20px; position: relative; } .p_beforeImg:before { content: ‘‘; background: url(‘../img/triangle_up.png‘) no-repeat top left /32px 16px;/*兼容没测*/ position: absolute; top: -15px; z-index: 2; width: 32px; height: 16px; } </style> </head> <body> <p>ello Word(H是通过before添加的文字)</p> <p>通过before添加三角尖图片</p> </body> </html>运行效果
伪类选择器:before使用content属性插入项目编号,,项目编号可以为数字编号、小写字母编号、大写字母编号、罗马字编号等
代码
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta charset="utf-8"> <meta content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=0,minimal-ui"> <meta content="black-translucent"> <meta content="yes" /> <meta content="telephone=no"> <meta content="black"> <meta content="yes"> <meta content="webkit"> <title>css3</title> <style type="text/css"> //counter-increment 属性设置某个拔取器每次呈现的计数器增量。默认增量是 1 mycounter为我指定的计数器名 .p_before_number>span,.p_before_letter>span { counter-increment: mycounter; display: block; } .p_before_number>span:before { content: ‘第‘counter(mycounter)‘种‘; } //upper-alpha为大写字母 //其它的编号类型:list-style-type属性下面将列出它所有的值 .p_before_letter>span:before { content: counter(mycounter,upper-alpha)‘.‘; } </style> </head> <body> <div> <span>苹果</span> <span>香蕉</span> <span>芒果</span> </div> <div> <span>跑步</span> <span>游泳</span> <span>登山</span> </div> </body> </html>运行效果
第1种苹果
第2种香蕉
第3种芒果
A.跑步
B.游泳
C.登山
以上list-style-type属性来源w3school
after: 伪元素选择器用于在某个元素之后插入一些内容把before的处所换成after,插入图片样式用一下的就行
.p_after:after { content: ‘d‘; color: #01B4EE; } .p_afterImg { background: #eeeeee; width: 200px; height: 80px; border-radius: 6px; padding: 10px 20px; position: relative; } .p_afterImg:after { content: ‘‘; background: url(‘../img/triangle_down.png‘) no-repeat bottom right /32px 16px;/*兼容没测*/ position: absolute; bottom: -15px; z-index: 2; width: 32px; height: 16px; }运行效果
CSS伪类选择器:before、:after使用:插入字符、插入图片、插入项目编号
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/31348.html