2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > html中after伪类原理 css伪类before跟after原理与使用(原)

html中after伪类原理 css伪类before跟after原理与使用(原)

时间:2024-06-11 00:51:41

相关推荐

html中after伪类原理 css伪类before跟after原理与使用(原)

伪类before和after用于在被选元素的内容前面或后面插入内容。

请使用 content 属性来指定要插入的内容。(只能插入内容)

可以设置所插入内容的样式。

直接上代码:

div {width:200px; border:1px solid #000; line-height:50px; margin:10px;}

.after,.before {border:1px solid #330;}

.before:before {content:'超帅的'; color:#F00;}

.after:after {content:'的教程'; color:#F00;}

窗外赏雪窗外赏雪

before伪类会在被选的span元素的内容前面插入:before内容

after伪类会在被选的span元素的内容后面插入:after内容

该伪类主要用于清除浮动,现在来看看例子

.frame{background-color:#3FF; width:300px;}

.left{background-color:red; width:100px; height:200px; float:left}

.right{background-color:green; width:100px; height:150px; float:right}

.bottom{background-color:gray; width:300px; height:50px;}

这样做比较麻烦,需要在浮动div后面新增一个div来清除浮动,我们可以通过after伪类来实现。

.frame{background-color:#3FF; width:300px;}

.left{background-color:red; width:100px; height:200px; float:left}

.right{background-color:green; width:100px; height:150px; float:right}

.bottom{background-color:gray; width:300px; height:50px;}

.clearfix {display: block;}

.clearfix:after {content: "."; display: block; height: 0; clear: both; visibility: hidden;}

注意:

必须在浮动的div后面添加clean的div,不能在浮动的div里面添加。

因此,不能通过

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。