[置顶] css3 befor after 简单使用 制作时尚焦点图相框
:befor、:after是CSS的伪元素,什么是伪元素呢?伪元素用于向某些选择器设置特殊效果。
我们用CSS手册可以查询到其基本的用法:
- E:before/E::before 设置在对象前(依据对象树的逻辑结构)发生的内容。用来和content属性一起使用
- E:after/E::after 设置在对象后(依据对象树的逻辑结构)发生的内容。用来和content属性一起使用
- Ie6-7 不支持
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gbk" /> <title>css_brfor_after</title> <style type="text/css"> body { width: 300px; height: auto; display: block; margin: 0px auto; padding: 0px; text-align: left; text-decoration: none; background: #efeeea; background: linear-gradient(#f9f9f9, #cecbc4); background: -moz-linear-gradient(#f9f9f9, #cecbc4); background: -webkit-linear-gradient(#f9f9f9, #cecbc4); background: -o-linear-gradient(#f9f9f9, #cecbc4); } #box { width: 300px; height: 300px; } #box .relative { width: 288px; height: 288px; margin-left: 5px; margin-top: 5px; background-color: transparent; border-radius: 5px; -moz-border-radius: 5px; border: 2px solid red; position: absolute; } #box .relative:before { width: 288px; height: 248px; background-color: transparent; border-left: 2px solid #fff; border-right: 2px solid #fff; position: absolute; content: ''; left: -2px; top: 20px; } #box .relative:after { width: 248px; height: 288px; background-color: transparent; border-top: 2px solid #fff; border-bottom: 2px solid #fff; position: absolute; content: ''; top: -2px; left: 20px; } </style> </head> <body> <div id="box"> <div class="relative"></div> <img src="ba.png" width="300" height="300" alt="心" title="心" /> </div> </body> </html>