CSS3制作气泡对话框的实例好代码教程

夏天,一束束剑麻,开出的花,如一串串玲珑的 "铃铛 ",在陽光下争艳斗丽地开放。花期最长的要数百日红了,它在烈日下顽强地开出硕大的、玖瑰红的花朵。每天早晨,树下的石凳上,都坐满了晨读的大哥哥大姐姐,这也成了校园的一处风景。

创建一个100%CSS的气泡,我们从下面的标记考试。

XML/HTML Code复制内容到剪贴板
  1. <divclass="speech-bubble">Hithere!</div>

接下来,应用一些基本样式。

CSS Code复制内容到剪贴板
  1. .speech-bubble{
  2. position:relative;
  3. background-color:#292929;
  4. width:200px;
  5. height:150px;
  6. line-height:150px;/*垂直居中*/
  7. color:white;
  8. text-align:center;
  9. }

箭头将通过after伪元素实现。

CSS Code复制内容到剪贴板
  1. .speech-bubble:after{
  2. content:'';
  3. }
  4. :before和:after伪元素可以用来在元素内容之前或之后插入生成内容。接下来,只是简单复制箭头,并定位到适当的位置。我们开始通过绝对定位的内容,重置宽度和高度,并应用边界颜色。
  5. .speech-bubble:after{
  6. content:'';
  7. position:absolute;
  8. width:0;
  9. height:0;
  10. border:10pxsolid;
  11. border-color:redgreenblueyellow;
  12. }

因为我们知道我们想要向下的箭头,上面的图片表明,除了红色(或上)边境其他的都应该被省略,或者设置为透明。

CSS Code复制内容到剪贴板
  1. .speech-bubble:after{
  2. content:'';
  3. position:absolute;
  4. width:0;
  5. height:0;
  6. border:10pxsolid;
  7. border-top-color:red;
  8. }

当创建CSS形状是,因为我们不能使用width属性来指定箭头的宽度,而是应该使用border-width属性。在这种情况下,箭头应该更大点;所以border-width可以增加到15px。我们将箭头定位到容器的底部居中,通过利用top和left属性。

CSS Code复制内容到剪贴板
  1. .speech-bubble:after{
  2. content:'';
  3. position:absolute;
  4. width:0;
  5. height:0;
  6. border:15pxsolid;
  7. border-top-color:red;
  8. top:100%;
  9. left:50%;
  10. }

到这里就差不多了;最后一个步骤是更新箭头的颜色和容器的背景颜色相同。定位也需要修改,根据边界的宽度(15 px)。当我们在这里,我们还将应用一个微妙border-radius属性来使容器更像气泡。

CSS Code复制内容到剪贴板
  1. .speech-bubble{
  2. /*…其他样式*/
  3. border-radius:10px;
  4. }
  5. .speech-bubble:after{
  6. content:'';
  7. position:absolute;
  8. width:0;
  9. height:0;
  10. border:15pxsolid;
  11. border-top-color:#292929;
  12. top:100%;
  13. left:50%;
  14. margin-left:-15px;/*调整边框宽度*/
  15. }

不错,不是吗?将这代码抽象为几个可重用的类,好应用到你将来的项目。

CSS Code复制内容到剪贴板
  1. /*
  2. 对话气泡
  3. 用法:使用.speech-bubble和.speech-bubble-DIRECTION类
  4. <divclass="speech-bubblespeech-bubble-top">Hithere</div>
  5. */
  6. .speech-bubble{
  7. position:relative;
  8. background-color:#292929;
  9. width:200px;
  10. height:150px;
  11. line-height:150px;/*垂直居中*/
  12. color:white;
  13. text-align:center;
  14. border-radius:10px;
  15. font-family:sans-serif;
  16. }
  17. .speech-bubble:after{
  18. content:'';
  19. position:absolute;
  20. width:0;
  21. height:0;
  22. border:15pxsolid;
  23. }
  24. /*箭头的位置*/
  25. .speech-bubble-top:after{
  26. border-bottom-color:#292929;
  27. left:50%;
  28. bottombottom:100%;
  29. margin-left:-15px;
  30. }
  31. .speech-bubble-rightright:after{
  32. border-left-color:#292929;
  33. left:100%;
  34. top:50%;
  35. margin-top:-15px;
  36. }
  37. .speech-bubble-bottombottom:after{
  38. border-top-color:#292929;
  39. top:100%;
  40. left:50%;
  41. margin-left:-15px;
  42. }
  43. .speech-bubble-left:after{
  44. border-right-color:#292929;
  45. top:50%;
  46. rightright:100%;
  47. margin-top:-15px;
  48. }

补充:更好的垂直居中
使用line-height实现垂直居中的一个缺点是仅限于一行。当文本需要两行或两行以上时,每一行的高度将会太大。一个聪明的解决办法是设置气泡的display属性为table,和包装段落文本的display为table-cell。这就允许我们将文本设为垂直居中。

XML/HTML Code复制内容到剪贴板
  1. <divclass="speech-bubblespeech-bubble-top">
  2. <p>Textgoeshere.</p>
  3. </div>

接下来,修改CSS。

CSS Code复制内容到剪贴板
  1. .speech-bubble{
  2. /*其他样式*/
  3. display:table;
  4. }
  5. .speech-bubblep{
  6. display:table-cell;
  7. vertical-align:middle;
  8. }

如果引用display: table 带来可怕的表格布局的老式回忆,别担心。这些属性是指显示一个元素的样式。
我们不局限于三角形;CSS能产生各种各样的形状,甚至心和生物危害标志!

CSS Code复制内容到剪贴板
  1. .biohazard{
  2. width:0;height:0;
  3. border:60pxsolid;
  4. border-radius:50%;
  5. border-top-color:black;
  6. border-bottom-color:black;
  7. border-left-color:yellow;
  8. border-right-color:yellow;
  9. }

到此这篇关于CSS3制作气泡对话框的实例好代码教程就介绍到这了。生活中,有人给予帮助,那是幸运,没人给予帮助,那是命运。我们要学会在幸运青睐自己的时候学会感恩,在命运磨练自己的时候学会坚韧。更多相关CSS3制作气泡对话框的实例好代码教程内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!