css怎麼做出六邊形圖片

來源:互聯網
上載者:User
這次給大家帶來css怎麼做出六邊形圖片,css做出六邊形圖片的注意事項有哪些,下面就是實戰案例,一起來看一下。

用簡單的div配合虛擬元素,即可‘畫出’這幅六邊形圖片,原理是三個相同寬高的div,通過定位旋轉拼合成一個六邊形,再利用背景圖層疊,形成視覺上的一張整圖。下面咱們一步一步來實現。

(1)那麼第一步,當然是繪製容器,容器是一個有寬高的div。

繪製之前,必須明白一個問題,那就是,等邊六邊形是通過三個相同寬高的div拼合而成的(如所示,所以div的寬高必須滿足 √3 倍的條件才能拼成一個正六邊形,這裡就不帶大家計算這個值了,有興趣可以用三角函數私下自己計算一下。

在此處,我設定了外層容器寬為190px, 高為110px, 然後設定背景圖片 。代碼如下

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title></head><style>    .wrap{        height:110px;        width: 190px;        position: relative;        margin: 200px auto;        background: url('./eddie.jpg') 50% 50% no-repeat;        background-size: auto 220px;    }</style><body>    <div class='wrap'>    </div></body></html>

(2)第二步,繪製左側div及其虛擬元素圖片

這一步,利用新div定位旋轉拼合六邊形的左側,並給新div的虛擬元素設定寬高並設定與一致的背景圖片,注意新div虛擬元素的寬高為整個六邊形的寬高。然後旋轉虛擬元素使圖片垂直顯示(應為新div旋轉了,所以虛擬元素圖片也被旋轉,所以需要反向旋轉回正常角度)而且還要調整虛擬元素位置(新div旋轉了,影響虛擬元素定位位置),最後給這個新div設定超出隱藏,六邊形左邊就繪製好了

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title></head><style>    .wrap{        height:110px;        width: 190px;        position: relative;        margin: 200px auto;        background: url('./eddie.jpg') 50% 50% no-repeat;        background-size: auto 220px;    }    .common{        position: absolute;        height: 100%;        width: 100%;        overflow: hidden;        left:0;23    }    .common:before{        content:'';        position: absolute;        background:url('./eddie.jpg') 50% 50% no-repeat;        background-size: auto 220px;        width: 190px;        height: 220px;    }    .left{        transform: rotate(60deg);    }    .left:before{        transform: rotate(-60deg) translate(48px,-28px);    }</style><body>    <div class='wrap'>        <div class='left common'></div>         </div></body></html>

(3)第三步,繪製右側div及其虛擬元素圖片

這步原理和第二部一樣,只不過角度反過來了,所以就不贅述,直接上完整代碼

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title></head><style>    .wrap{        height:110px;        width: 190px;        position: relative;        margin: 200px auto;        background: url('./eddie.jpg') 50% 50% no-repeat;        background-size: auto 220px;    }    .common{        position: absolute;        height: 100%;        width: 100%;        overflow: hidden;        left:0;     }    .common:before{        content:'';        position: absolute;        background:url('./eddie.jpg') 50% 50% no-repeat;        background-size: auto 220px;        width: 190px;        height: 220px;    }    .left{        transform: rotate(60deg);    }    .left:before{        transform: rotate(-60deg) translate(48px,-28px);    }    .right{        transform: rotate(-60deg);    }    .right:before{         transform: rotate(60deg) translate(48px,28px);         bottom: 0;    }</style><body>    <div class='wrap'>        <div class='left common'></div>        <div class='right common'></div>    </div></body></html>

至此,就能展現出文章最開始的圖片,利用這一原理,還可以做出其他各種形狀的圖片展示效果。歡迎各位繼續研究。以後圖片展示不再是單一的磚塊行啦!!

相信看了這些案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!

相關閱讀:

HTML5的下拉框應該如何增加使用者體驗

用H5的WebGL如何在同一個介面做出json和echarts圖表

H5的語義化標籤新特性應該如何使用

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.