:after伪类+content 清除浮动的影响
浮动元素会让此div的高度塌陷。如下例子:
.box{padding:10px; background:gray;}
.l{float:left;}
.box{padding:10px; background:gray;}
.l{float:left;}
<div class="box fix">
<img class="l" src="http://image.zhangxinxu.com/image/study/s/s256/mm1.jpg" />
</div>
可以使用:after+content方法清除浮动造成的塌陷。使用如下:
.fix{*zoom:1;}
.fix:after{content:"clear"; display:block; height:0; clear:both; visibility:hidden;}
:after伪类+content 让大小不固定图片垂直居中:
.pic_box{width:300px; height:300px; font-size:0; *font-size:200px; text-align:center;}
.pic_box img{vertical-align:middle;}
.pic_box:after{display:inline-block; width:0; height:100%; content:"center"; vertical-align:middle; overflow:hidden;}
相应的HTML代码如下:
<div class="pic_box">
<img src="http://image.zhangxinxu.com/image/study/s/s256/mm1.jpg" />
</div>