用css畫三角形(提示框三角形)

三角形使用情形

  • 常常用於提示框,下拉菜單等(csdn也不少用到,最後一圖),看圖:

三角形三角形這三角形

  • 因爲在網頁中常常要用到,因此特意研究
  • 圖片實現(感受low)、svg實現(小題大做了),因此最後仍是css畫比較不錯,兼容性也不錯

三角形畫法

  1. html結構
    <div class="triangle">
    </div>
  2. 三角形畫法
    • 用border畫出,當width、height均爲100px時
.triangle { width: 100px; height: 100px; border-left: 10px solid #7d7b7b; border-top: 10px solid #5851c3; border-right: 10px solid #21a4d6; border-bottom: 10px solid #4ed621; box-sizing: border-box; }

–>結果:border
改變{width:0; height:0}–>這裏寫圖片描述css

–>再去掉border-top—>這裏寫圖片描述html

–>能夠看見上面的一半已經沒有了markdown

–>設置左右兩邊border-color:transparent; –> 這裏寫圖片描述
–>就獲得了想要的三角形了,這是向上的,想要哪邊就畫哪邊的border,而且讓它相鄰兩邊的border-color:transparentsvg

–>代碼工具

.triangle { width: 0; height: 0; border-left: 10px solid transparent; /*border-top: 10px solid #5851c3;*/ border-right: 10px solid transparent; border-bottom: 10px solid #4ed621; box-sizing: border-box; }

畫提示框三角形(有邊緣的)

如圖:邊緣三角形ui

  • 原理:先畫一個三角形,再畫白色三角形的,調整幾像素位置,覆蓋掉一邊

–> 這裏寫圖片描述spa

  • 代碼
.triangle {
        position: relative;
        width: 100px;
        height: 50px;
        border: 2px solid #4ed621;
        border-radius: 5px;
    }

    .triangle:before {
        position: absolute;
        content: "";
        top: -10px;
        left: 25px;
        border-left: 10px solid transparent;
        border-right: 10px solid transparent;
        border-bottom: 10px solid #4ed621;
    }
    /* 白色覆蓋*/
    .triangle:after {
        position: absolute;
        content: "";
        /*減小兩像素*/
        top: -8px;
        left: 25px;
        border-left: 10px solid transparent;
        border-right: 10px solid transparent;
        border-bottom: 10px solid #fff;
    }
      <div class="triangle"></div>

結果:
–> hh–>h code

  • 至此,三角形畫完,只用到了css2的屬性,兼容性盡收眼底
  • 推薦一款優秀的在線畫圖工具,很不錯(不是廣告啊!)
相關文章
相關標籤/搜索