天天看點

值得把玩的各色CSS氣泡!【精通CSS100例】

最簡單的三角氣泡

實作思路:

使用一個::before和::after,對兩個設定邊框,最後邊框的效果就是三角形,一個三角形的邊框顔色div一緻,兩一個則是白色,整好覆寫在上方。

代碼

.bubble-box {
        position: relative;
        border: 2px solid #409eff;
        border-radius: 5px;
        width: 200px;
        height: 50px;
        line-height: 50px;
        text-align: center;
        
      }
      .bubble-box::before {
        position: absolute;
        right: 100%;
        top: 50%;
        margin: -5px 0 0px;
        border: 10px solid transparent;
        border-right-color: #409eff;
        content: "";
      }
      .bubble-box::after {
        position: absolute;
        right: 100%;
        top: 50%;
        margin-top: -3px;
        border: 8px solid transparent;
        border-right-color: #fff;
        content: "";
      }
      

斜三角形氣泡

值得把玩的各色CSS氣泡!【精通CSS100例】

實作邏輯

還是使用::after,使用它制作一個直角三角形

然後使用transform進行角度的傾斜達到效果。

.bubble-box {
        position: relative;
        border: 2px solid #409eff;
        border-radius: 5px;
        width: 200px;
        height: 50px;
        line-height: 50px;
        text-align: center;
      }
      .bubble-box::after {
        content: "";
        position: absolute;
        border:10px solid transparent;
        border-top-color: #409eff;
        border-right-color: #409eff;
        right: 100%;
        top: 10%;
        transform: skewY(10deg);
      }      

拖尾氣泡

值得把玩的各色CSS氣泡!【精通CSS100例】

實作思路

仔細觀察圖檔:首先可以看到右下角的拖尾是有弧度的,在CSS中最簡單實作弧度的方法:

就是對元素的圓角進行操作,隻需要對兩條邊同時操作,即可産生效果。

border-bottom-left-radius: 15px 15px;

值得把玩的各色CSS氣泡!【精通CSS100例】

然後的話,拖尾是比較小的,并沒有圖示的大,這個時候再簡單的方法,就是在上面加一層白色的div,覆寫掉其中的一部分。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .bubble-box {
        position: relative;
        text-align: center;
        width: 200px;
        height: 50px;
        line-height: 50px;
        background-color: #409eff;
        border-radius: 25px;
        
      }
      .bubble-box::before {
            content: "";
            position: absolute;
            z-index: -1;
            bottom: -2px;
            right: -8px;
            height: 20px;
            border-right: 20px solid #409eff;
            border-bottom-left-radius: 15px 15px;
            -webkit-transform: translate(0, -2px);
      }
      .bubble-box::after {
        content: "";
        position: absolute;
        z-index: 1;
        bottom: -2px;
        right: -56px;
        width: 26px;
        height: 20px;
        background: white;
        border-bottom-left-radius: 10px;
        -webkit-transform: translate(-30px, -2px);
      }
    </style>
  </head>
  <body style="padding: 100px">
    <div class="bubble-box">picker</div>
  </body>
</html>