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>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章