CSS 消息氣泡實(shí)現(xiàn)

前端頁(yè)面開(kāi)發(fā)中經(jīng)常需要實(shí)現(xiàn)消息氣泡樣式,比如:

messagebubble3.png

實(shí)際上上述兩種效果實(shí)現(xiàn)起來(lái),并不復(fù)雜,接下來(lái)我們就來(lái)實(shí)戰(zhàn)一下。

1. 構(gòu)建消息框

消息框主體很簡(jiǎn)單,一個(gè)div,設(shè)置一下背景顏色、border-radius等即可:

.message1,.message2 {
  width: 200px;
  height: 80px;
  margin: 100px auto;
  background-color: green;
  border-bottom-color:green;/*為了給after偽元素自動(dòng)繼承*/
  color: #fff;
  font-size: 12px;
  font-family: Arial;
  line-height: 18px;
  padding: 5px 12px 5px 12px;
  box-sizing: border-box;
  border-radius: 6px;
  position: relative;
  word-break: break-all;
}

<body>
  <div class="message1">
    Demos 代碼演示、代碼片段 - 讀你,歡迎來(lái)到讀你,http://dunizb.com/demo/
  </div>

  <div class="message2">
    Demos 代碼演示、代碼片段 - 讀你,歡迎來(lái)到讀你,http://dunizb.com/demo/
  </div>
</body>


messagebubble2.png

2 三角形箭頭

接下來(lái)我們實(shí)現(xiàn)一下圖一中第一個(gè)消息氣泡樣式,這個(gè)樣式相對(duì)簡(jiǎn)單:只需在消息框主體之前插入一個(gè)元素,然后再旋轉(zhuǎn)45度即可,在現(xiàn)有元素之前插入其他元素首選before

.message1::before {
  content: '';
  width: 20px; 
  height: 20px;
  background-color: inherit;
  left: -10px; /*向左側(cè)外部延伸箭頭box的一半寬度*/
  position: absolute;
  transform: rotate(45deg); /*旋轉(zhuǎn)45度*/
  top:50%; /*箭頭在數(shù)值方向上居中*/
  margin-top: -5px;
}
messagebubble4.png

3 圓弧型箭頭

圓弧型箭頭,稍微復(fù)雜些。由于涉及到弧度部分,可以考慮利用border來(lái)實(shí)現(xiàn)。首先我們通過(guò)after實(shí)現(xiàn)一個(gè)吸附在消息框右部的矩形框:

.message2::after {
  content: '';
  position: absolute;
  width: 20px;
  height: 20px;
  right: -24px;
  top: 0px;
  background-color: red; /*為顯示需要*/
} 
messagebubble5.png

前面已經(jīng)說(shuō)到,我們真正需要的是通過(guò)border來(lái)實(shí)現(xiàn)弧形效果,所以這里必然需要設(shè)置border,不過(guò)并不是所有方向都需要設(shè)置border,只需要設(shè)置底部和左部:

.message2::after {
    content: '';
    position: absolute;
    width: 20px;
    height: 20px;
    right: -24px;
    top: 0px;
    background-color: red;
    border-width: 0 0 20px 20px;
    border-style: solid;
    border-left-color: blue;
    border-bottom-color: yellow;
} 
messagebubble6.png

接下來(lái)再加上右下角弧度:

.message2::after {
  ...
  border-bottom-right-radius: 60px;
} 
messagebubble7.png

此時(shí)可以看到,基本的雛形已經(jīng)出來(lái)了,接下來(lái)就是重新設(shè)置顏色了。

.message2::after {
  content: '';
  position: absolute;
  width: 20px;
  height: 20px;
  right: -24px;
  top: 0px;
  border-width: 0 0 20px 20px;
  border-style: solid;
  border-left-color: transparent;
  border-bottom-color:inherit;
  border-bottom-right-radius: 60px;
} 
messagebubble9.png

大功告成!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 問(wèn)答題47 /72 常見(jiàn)瀏覽器兼容性問(wèn)題與解決方案? 參考答案 (1)瀏覽器兼容問(wèn)題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補(bǔ)...
    _Yfling閱讀 14,193評(píng)論 1 92
  • 各種純css圖標(biāo) CSS3可以實(shí)現(xiàn)很多漂亮的圖形,我收集了32種圖形,在下面列出。直接用CSS3畫(huà)出這些圖形,要比...
    劍殘閱讀 9,996評(píng)論 0 8
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,662評(píng)論 4 61
  • 一座城的美麗需要用你的雙眼去記錄; 一座城的味道需要用你的嗅覺(jué)去吸??; 一座城的話(huà)語(yǔ)需要用你的雙耳去傾聽(tīng); 一座城...
    BigPang仔閱讀 358評(píng)論 0 0
  • 第二位客戶(hù) 照片拍攝的五個(gè)角度:正面觀、背面觀、左右側(cè)面觀及上面觀。拍攝時(shí)鏡頭對(duì)著客戶(hù)的腰而不是胸進(jìn)行拍攝。 面部...
    厚_德_載_物閱讀 808評(píng)論 0 8

友情鏈接更多精彩內(nèi)容