溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

CSS3怎么制作氣泡對話框

發(fā)布時(shí)間:2021-08-10 21:19:19 來源:億速云 閱讀:156 作者:chen 欄目:web開發(fā)

這篇文章主要講解了“CSS3怎么制作氣泡對話框”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“CSS3怎么制作氣泡對話框”吧!

創(chuàng)建一個(gè)100%CSS的氣泡,我們從下面的標(biāo)記考試。

XML/HTML Code復(fù)制內(nèi)容到剪貼板

  1. <div class="speech-bubble">Hi there!</div>  

接下來,應(yīng)用一些基本樣式。

CSS Code復(fù)制內(nèi)容到剪貼板

  1. .speech-bubble {   

  2.     positionrelative;   

  3.     background-color#292929;   

  4.     

  5.     width200px;   

  6.     height150px;   

  7.     line-height150px/* 垂直居中 */  

  8.     

  9.     colorwhite;   

  10.     text-aligncenter;   

  11. }  

CSS3怎么制作氣泡對話框

箭頭將通過after偽元素實(shí)現(xiàn)。

CSS Code復(fù)制內(nèi)容到剪貼板

  1. .speech-bubble:after {   

  2.     content'';      

  3. }   

  4. :before和:after偽元素可以用來在元素內(nèi)容之前或之后插入生成內(nèi)容。 接下來,只是簡單復(fù)制箭頭,并定位到適當(dāng)?shù)奈恢?。我們開始通過絕對定位的內(nèi)容,重置寬度和高度,并應(yīng)用邊界顏色。   

  5. .speech-bubble:after {   

  6.   content'';   

  7.   positionabsolute;   

  8.     

  9.   width: 0;   

  10.   height: 0;   

  11.     

  12.   border10px solid;   

  13.   border-colorred green blue yellow;   

  14. }  

CSS3怎么制作氣泡對話框

因?yàn)槲覀冎牢覀兿胍蛳碌募^,上面的圖片表明,除了紅色(或上)邊境其他的都應(yīng)該被省略,或者設(shè)置為透明。

CSS Code復(fù)制內(nèi)容到剪貼板

  1. .speech-bubble:after {   

  2.   content'';   

  3.   positionabsolute;   

  4.     

  5.   width: 0;   

  6.   height: 0;   

  7.     

  8.   border10px solid;   

  9.   border-top-colorred;   

  10. }  

CSS3怎么制作氣泡對話框

當(dāng)創(chuàng)建CSS形狀是,因?yàn)槲覀儾荒苁褂脀idth屬性來指定箭頭的寬度,而是應(yīng)該使用border-width屬性。在這種情況下,箭頭應(yīng)該更大點(diǎn);所以border-width可以增加到15px。我們將箭頭定位到容器的底部居中,通過利用top和left屬性。

CSS Code復(fù)制內(nèi)容到剪貼板

  1. .speech-bubble:after {   

  2.   content'';   

  3.   positionabsolute;   

  4.     

  5.   width: 0;   

  6.   height: 0;   

  7.     

  8.   border15px solid;   

  9.   border-top-colorred;   

  10.     

  11.   top: 100%;   

  12.   left: 50%;   

  13. }  

CSS3怎么制作氣泡對話框

到這里就差不多了;最后一個(gè)步驟是更新箭頭的顏色和容器的背景顏色相同。定位也需要修改,根據(jù)邊界的寬度(15 px)。當(dāng)我們在這里,我們還將應(yīng)用一個(gè)微妙border-radius屬性來使容器更像氣泡。

CSS Code復(fù)制內(nèi)容到剪貼板

  1. .speech-bubble {   

  2.    /* &hellip; 其他樣式 */  

  3.    border-radius: 10px;   

  4. }   

  5.     

  6. .speech-bubble:after {   

  7.   content'';   

  8.   positionabsolute;   

  9.     

  10.   width: 0;   

  11.   height: 0;   

  12.     

  13.   border15px solid;   

  14.   border-top-color#292929;   

  15.     

  16.   top: 100%;   

  17.   left: 50%;   

  18.   margin-left: -15px/* 調(diào)整邊框?qū)挾?nbsp;*/  

  19. }  

CSS3怎么制作氣泡對話框

不錯(cuò),不是嗎?將這代碼抽象為幾個(gè)可重用的類,好應(yīng)用到你將來的項(xiàng)目。

CSS Code復(fù)制內(nèi)容到剪貼板

  1. /*  

  2.    對話氣泡  

  3.    用法:使用.speech-bubble和.speech-bubble-DIRECTION類  

  4.    <div class="speech-bubble speech-bubble-top">Hi there</div>  

  5. */  

  6.     

  7. .speech-bubble {   

  8.   positionrelative;   

  9.   background-color#292929;   

  10.     

  11.   width200px;   

  12.   height150px;   

  13.   line-height150px/* 垂直居中 */  

  14.     

  15.   colorwhite;   

  16.   text-aligncenter;   

  17.   border-radius: 10px;   

  18.     

  19.   font-familysans-serif;   

  20. }   

  21.     

  22. .speech-bubble:after {   

  23.   content'';   

  24.   positionabsolute;   

  25.     

  26.   width: 0;   

  27.   height: 0;   

  28.     

  29.   border15px solid;   

  30. }   

  31.     

  32.     

  33. /* 箭頭的位置 */  

  34.     

  35. .speech-bubble-top:after {   

  36.   border-bottom-color#292929;   

  37.     

  38.   left: 50%;   

  39.   bottombottom: 100%;   

  40.   margin-left: -15px;     

  41. }   

  42. .speech-bubble-rightright:after {   

  43.   border-left-color#292929;   

  44.     

  45.   left: 100%;   

  46.   top: 50%;   

  47.   margin-top: -15px;      

  48. }   

  49.     

  50. .speech-bubble-bottombottom:after {   

  51.   border-top-color#292929;   

  52.     

  53.   top: 100%;   

  54.   left: 50%;   

  55.   margin-left: -15px;     

  56. }   

  57.     

  58. .speech-bubble-left:after {   

  59.   border-right-color#292929;   

  60.     

  61.   top: 50%;   

  62.   rightright: 100%;   

  63.   margin-top: -15px;      

  64. }  

CSS3怎么制作氣泡對話框

補(bǔ)充:更好的垂直居中
使用line-height實(shí)現(xiàn)垂直居中的一個(gè)缺點(diǎn)是僅限于一行。當(dāng)文本需要兩行或兩行以上時(shí),每一行的高度將會太大。一個(gè)聰明的解決辦法是設(shè)置氣泡的display屬性為table,和包裝段落文本的display為table-cell。這就允許我們將文本設(shè)為垂直居中。

XML/HTML Code復(fù)制內(nèi)容到剪貼板

  1. <div class="speech-bubble speech-bubble-top">  

  2.     <p>Text goes here.</p>  

  3. </div>  

接下來,修改CSS。

CSS Code復(fù)制內(nèi)容到剪貼板

  1. .speech-bubble {   

  2.  /* 其他樣式 */  

  3.     

  4.   display: table;   

  5. }   

  6.     

  7. .speech-bubble p {   

  8.   displaytable-cell;   

  9.   vertical-alignmiddle;   

  10. }  

CSS3怎么制作氣泡對話框

如果引用display: table 帶來可怕的表格布局的老式回憶,別擔(dān)心。這些屬性是指顯示一個(gè)元素的樣式。
我們不局限于三角形;CSS能產(chǎn)生各種各樣的形狀,甚至心和生物危害標(biāo)志!
CSS3怎么制作氣泡對話框

CSS Code復(fù)制內(nèi)容到剪貼板

  1. .biohazard {   

  2.   width: 0; height: 0;   

  3.     

  4.   border60px solid;   

  5.   border-radius: 50%;   

  6.     

  7.   border-top-colorblack;   

  8.   border-bottom-colorblack;   

  9.   border-left-coloryellow;   

  10.   border-right-coloryellow;   

  11. }  

感謝各位的閱讀,以上就是“CSS3怎么制作氣泡對話框”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對CSS3怎么制作氣泡對話框這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI