1. 程式人生 > 實用技巧 >使用偽元素after畫三角形

使用偽元素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偽類實現小三角形氣泡框