純css製作三角形+常用的對話框實例

網頁中經常用到三角形的地方,用css寫三角形的方法很簡單

html:

<span class="u-tri"></span>
css:

 .u-tri{
          display: inline-block;
          border-top: 20px solid transparent;
          border-bottom: 20px solid transparent;
          border-left: 20px solid transparent;
          border-right: 20px solid red;
      }
效果圖:



想要三角形的尖角方向朝哪邊,對應的相反一邊設置顏色。比如尖角向上,則boder-bottom的color上色,其它三邊爲透明。

實例:對話框背景

先上最後的效果圖:


完整代碼:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        div{
            margin: 20px;
        }
        .box{
            position: relative;
            display: inline-block;
            max-width: 200px;
            padding: 4px 8px;
            border-radius: 2px;
            background-color: green;
            line-height: 20px;
            font-size: 14px;
        }
        .u-tri:before{
            position: absolute;
            left: -24px;
            top: 4px;
            content: '';
            border-top: 8px solid transparent;
            border-bottom: 8px solid transparent;
            border-left: 12px solid transparent;
            border-right: 12px solid green;
        }
    </style>
</head>
<body>
    <div>
        <p class="box u-tri">測試</p>
    </div>
    <div>
        <p class="box u-tri">nizahhhshs 哈哈哈哈哈哈我只是個則是的我只是個可愛的測試案列e</p>
    </div>
</body>
</html>



發佈了42 篇原創文章 · 獲贊 21 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章