您现在的位置是:网站首页> 编程资料编程资料

CSS3制作气泡对话框的实例教程纯CSS实现气泡对话框尖角处理方案JS+CSS3实现的确认框和对话框弹出并关闭特效源码CSS3实现可兼容移动端的超酷弹出对话框效果源码jQuery结合Css3制作的多风格轻量级弹出动感对话框插件特效源码用纯css3和html制作泡沫对话框实现代码纯CSS代码实现各类气球泡泡对话框效果CSS实现对话框小尾巴功能

2021-09-05 2537人已围观

简介 这篇文章主要介绍了CSS3制作气泡对话框的实例教程,同时讲到了对气泡的垂直居中的设定技巧,需要的朋友可以参考下

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

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

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

CSS Code复制内容到剪贴板
  1. .speech-bubble {   
  2.     positionrelative;   
  3.     background-color#292929;   
  4.     
  5.     width200px;   
  6.     height150px;   
  7.     line-height150px/* 垂直居中 */  
  8.     
  9.     colorwhite;   
  10.     text-aligncenter;   
  11. }  

2016510114442520.jpg (523×398)

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

CSS Code复制内容到剪贴板
  1. .speech-bubble:after {   
  2.     content'';      
  3. }   
  4. :before和:after伪元素可以用来在元素内容之前或之后插入生成内容。 接下来,只是简单复制箭头,并定位到适当的位置。我们开始通过绝对定位的内容,重置宽度和高度,并应用边界颜色。   
  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. }  

2016510114508238.jpg (529×406)

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

CSS Code复制内容到剪贴板
  1. .speech-bubble:after {   
  2.   content'';   
  3.   positionabsolute;   
  4.     
  5.   width: 0;   
  6.   height: 0;   
  7.     
  8.   border10px solid;   
  9.   border-top-colorred;   
  10. }  

2016510114530648.jpg (521×401)

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

CSS Code复制内容到剪贴板
  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. }  

2016510114554761.jpg (526×438)

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

CSS Code复制内容到剪贴板
  1. .speech-bubble {   
  2.    /* … 其他样式 */  
  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/* 调整边框宽度 */  
  19. }  

2016510114622476.jpg (526×431)

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

CSS Code复制内容到剪贴板
  1. /*  
  2.    对话气泡  
  3.    用法:使用.speech-bubble和.speech-bubble-DIRECTION类  
  4.    Hi there
 
  • */  
  •     
  • .speech-bubble {   
  •   positionrelative;   
  •   background-color#292929;   
  •     
  •   width200px;   
  •   height150px;   
  •   line-height150px/* 垂直居中 */  
  •     
  •   colorwhite;   
  •   text-aligncenter;   
  •   border-radius: 10px;   
  •     
  •   font-familysans-serif;   
  • }   
  •     
  • .speech-bubble:after {   
  •   content'';   
  •   positionabsolute;   
  •     
  •   width: 0;   
  •   height: 0;   
  •     
  •   border15px solid;   
  • }   
  •     
  •     
  • /* 箭头的位置 */  
  •     
  • .speech-bubble-top:after {   
  •   border-bottom-color#292929;   
  •     
  •   left: 50%;   
  •   bottombottom: 100%;   
  •   margin-left: -15px;     
  • }   
  • .speech-bubble-rightright:after { 

  • 相关内容

    -六神源码网