使用偽元素after畫三角形


畫×圖標

代碼

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Static Template</title>
    <style>
      .close {
        /*將偽元素轉換為內聯塊狀元素,好設置寬高*/ 
        display: inline-block;           
        width: 14px;
        height: 1px;
        background: #95835e;
        /*旋轉角度*/
        transform: rotate(45deg);   
        -webkit-transform: rotate(45deg);
      }
      .close:after {
        content: "";
        display: block;
        width: 14px;
        height: 1px;
        background: #95835e;
        transform: rotate(-90deg);
        -webkit-transform: rotate(-90deg);
      }
    </style>
  </head>

  <body>
    <span class="close"></span>
  </body>
</html>

效果

原理就是用span元素和after偽元素畫兩條直線,利用css3的transform屬性分別進行旋轉達到交叉的效果。

空心三角箭頭

代碼

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Static Template</title>
    <style>
      .arrowUp:after {
        content: "";
        display: inline-block;
        width: 8px;
        height: 8px;
        border-top: 1px solid #656565;
        border-right: 1px solid #656565;
        transform: rotate(-45deg);
        -webkit-transform: rotate(-45deg);
      }
    </style>
  </head>

  <body>
    <span class="arrowUp"></span>
  </body>
</html>

效果

原理就是用after偽元素畫了一個矩形,只描繪了上邊框和右邊框,這樣就形成了一個箭頭的形狀,然后再用transform屬性調整角度實現不同的朝向。這里就舉了一個方向的例子,其他兩個方向只要改一下角度即可。

其他精選博客

css偽元素before/after和畫三角形的搭配應用

理解使用before,after偽類實現小三角形氣泡框


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM