纯CSS实现带小三角提示框
要实现在页面上点击指定元素时,弹出一个信息提示框。在前面的文章中,我们已经简单介绍了如何使用纯 CSS 创建一个三角形。本文在此基础上,记录如何使用 CSS 创建带三角形的提示框。
实现的原理是创建一个div提示框,然后再创建一个三角形,将三角形用绝对定位(absolute)到提示框对应的位置。
一、创建不带边框的提示框:
之前已介绍过怎么生成三角形,直接代码如下:
<style>
body {
margin: 0;
padding: 0;
background: grey;
}
/*提示框容器*/
.tip {
position: relative;
margin-left: 20px;
margin-top: 20px;
width: 200px;
background: #fff;
padding: 10px;
/*设置圆角*/
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
/*提示框-左三角*/
.tip-trangle-left {
position: absolute;
bottom: 15px;
left: -10px;
width: 0;
height: 0;
border-top: 15px solid transparent;
border-bottom: 15px solid transparent;
border-right: 15px solid #fff;
}
/*提示框-右三角*/
.tip-trangle-right {
position: absolute;
top: 15px;
right: -10px;
width: 0;
height: 0;
border-top: 15px solid transparent;
border-bottom: 15px solid transparent;
border-left: 15px solid #fff;
}
/*提示框-上三角*/
.tip-trangle-top {
position: absolute;
top: -10px;
left: 20px;
width: 0;
height: 0;
border-left: 15px solid transparent;
border-right: 15px solid transparent;
border-bottom: 15px solid #fff;
}
/*提示框-下三角*/
.tip-trangle-bottom {
position: absolute;
bottom: -10px;
left: 20px;
width: 0;
height: 0;
border-left: 15px solid transparent;
border-right: 15px solid transparent;
border-top: 15px solid #fff;
}
</style>
<div class="tip">
<div class="tip-trangle-left"></div>
我是提示框<br/>
内容可以自定义
</div>
<div class="tip">
<div class="tip-trangle-right"></div>
我是提示框<br/>
内容可以自定义
</div>
<div class="tip">
<div class="tip-trangle-top"></div>
我是提示框<br/>
内容可以自定义
</div>
<div class="tip">
<div class="tip-trangle-bottom"></div>
我是提示框<br/>
内容可以自定义
</div>
以上代码效果如下(我们实现了箭头在4个不同方向的提示框,在使用时可根据自身需要进行调整):

二、创建带边框的提示框:
第一步实现了不带边框的提示框,如果要实现带边框的提示框,原理是先把提示框容器加上边框,然后通过伪元素,在需要带箭头的边框上面生成2个三角形,最后改变最上面的三角形的颜色(和提示框的内容背景色相同),即可实现。代码如下:
<style>
body {
margin: 0;
padding: 0;
background: grey;
}
/*提示框容器-上三角形*/
.tip-top {
margin: 20px;
padding: 5px;
width: 300px;
height: 60px;
border: 2px solid #f99;
position: relative;
background-color: #FFF;
/*设置圆角*/
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
/*生成2个叠加的三角形*/
.tip-top:before, .tip-top:after {
content: "";
display: block;
border-width: 15px;
position: absolute;
top: -30px;
left: 100px;
border-style: solid dashed dashed solid;
border-color: transparent transparent #f99 transparent;
font-size: 0;
line-height: 0;
}
/*将上面的三角形颜色设置和容器背景色相同*/
.tip-top:after {
top: -27px;
border-color: transparent transparent #FFF transparent;
}
/*下三角*/
.tip-bottom {
margin: 20px;
padding: 5px;
width: 300px;
height: 60px;
border: 2px solid #f99;
position: relative;
background-color: #0FF;
/*设置圆角*/
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
.tip-bottom:before, .tip-bottom:after {
content: "";
display: block;
border-width: 15px;
position: absolute;
bottom: -30px;
left: 100px;
border-style: solid dashed dashed solid;
border-color: #f99 transparent transparent transparent;
font-size: 0;
line-height: 0;
}
.tip-bottom:after {
bottom: -27px;
border-color: #0FF transparent transparent transparent;
}
/*左三角*/
.tip-left {
margin: 20px;
padding: 5px;
width: 300px;
height: 60px;
border: 2px solid #f99;
position: relative;
background-color: #FFF;
/*设置圆角*/
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
.tip-left:before, .tip-left:after {
content: "";
display: block;
border-width: 15px;
position: absolute;
left: -30px;
top: 20px;
border-style: dashed solid solid dashed;
border-color: transparent #f99 transparent transparent;
font-size: 0;
line-height: 0;
}
.tip-left:after {
left: -27px;
border-color: transparent #FFF transparent transparent;
}
/*右三角*/
.tip-right {
margin: 20px;
padding: 5px;
width: 300px;
height: 60px;
border: 2px solid #f99;
position: relative;
background-color: #FFF;
/*设置圆角*/
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
.tip-right:before, .tip-right:after {
content: "";
display: block;
border-width: 15px;
position: absolute;
right: -30px;
top: 20px;
border-style: dashed solid solid dashed;
border-color: transparent transparent transparent #f99;
font-size: 0;
line-height: 0;
}
.tip-right:after {
right: -27px;
border-color: transparent transparent transparent #FFF;
}
</style>
<div class="tip-top">
我是提示框<br/>
内容可以自定义
</div>
<div class="tip-bottom">
我是提示框<br/>
内容可以自定义
</div>
<div class="tip-left">
我是提示框<br/>
内容可以自定义
</div>
<div class="tip-right">
我是提示框<br/>
内容可以自定义
</div>
</body>
以上代码效果如下(我们实现了箭头在4个不同方向的提示框,在使用时可根据自身需要进行调整):

通过以上两个例子,展示了如何生成简单的提示框。而对于有边框的提示框,如果边框颜色和内容区背景色相同,也会实现无边框的提示框效果。在使用时,可以根据自己需要,调整代码。
纯CSS实现带小三角提示框的更多相关文章
- 使用纯CSS实现带箭头的提示框
爱编程爱分享,原创文章,转载请注明出处,谢谢!http://www.cnblogs.com/fozero/p/6187323.html 1.全部代码 <!DOCTYPE html> < ...
- 纯css写带小三角对话框
在实际样式中经常会遇到要写类似对话框的样式,而这种样式往往会有一个小三角,如下所示: 那么如何用css写出来呢,其实很简单,先让父元素相对定位,然后运用css的伪类before或after.就可以写个 ...
- 纯CSS实现带小角的对话框式下拉菜单
最近公司首页样式重写,头部下拉菜单改为了带小角的对话框式下拉菜单: 很多人可能会用图片,事实上纯CSS就能够实现: HTML: <!DOCTYPE html> <html lang= ...
- CSS实现带箭头的提示框
我们在很多UI框架中看到带箭头的提示框,感觉挺漂亮,但是之前一直不知道其原理,今天网上找了些资料算是弄清楚原理了: 先上效果图: 原理分析: 上面的箭头有没有觉得很像一个三角形,是的,它就是三角形:只 ...
- 【小技巧】只用css实现带小三角的对话框样式
一个小小的技巧: 如图所示,这种小三角,不用图片,只用css怎么实现呢? 直接上代码吧: <!DOCTYPE html> <html> <head> <tit ...
- 利用CSS中的:after、: before制作的边三角提示框
小颖昨天分享了一篇参考bootstrap中的popover.js的css画消息弹框今天给大家再分享一篇使用:before和:after伪元素画消息弹框的CSS. 画出来是介个酱紫的: 有没有觉得画的萌 ...
- 用CSS制作小三角提示符号
今天在项目中遇到了如下图的切图要求. 对,重点就是那个小三角提示符号. html 结构如下 <div class="wrap"> <div class=" ...
- 圆角带箭头的提示框css实现
css是一个很强大的东西,很多网页效果,我们可以通过css直接实现.今天给大家分享的是一个用css实现的圆角带箭头的提示框. 效果如下图: 这一个样式主要涉及到了css的边框样式border的运用和定 ...
- 自定义常用input表单元素一:纯css 实现自定义checkbox复选框
最下面那个是之前写的 今天在做项目的时候发现,之前写的貌似还是有点多,起码增加的span标签可以去掉,这样保持和原生相同的结构最好的,仅仅是样式上的变化.今天把项目中的这个给更新上来.下面就直接还是 ...
- css border制作小三角形状及气泡框(兼容IE6)
先看下CSS盒模型 一个盒子包括: margin+border+padding+content 上下左右边框交界处出呈现平滑的斜线. 利用这个特点, 通过设置不同的上下左右边框宽度或者颜色可以得到小三 ...
随机推荐
- Git修改最近一次提交的日志信息
一.问题由来 当前自己所在的项目组中,每次发完一个版本后,就需要创建个人新版本的git提交凭证,其实就是系统自动 生成的一串编码,如果没有这个凭证,代码是提交不了的,这是公司制定的开发规范之一.这两天 ...
- [非常重要] 通过ssh的方式提交github
通过ssh的方式提交github - 重要文章!!vscode提交github 原因: github的https的clone项目报错,所以改用ssh的方式 1 本地创建ssh秘钥 目录是 .ssh 我 ...
- node开发命令行脚本 / commander
1. 脚本第一行添加 #!/usr/bin/env node // index.js #!/usr/bin/env node console.log('hello world') 2. package ...
- 基于python的密码生成器实例解析
一 概念 密码生成不复杂,可是它却涉及到了string的常用技巧和一些概念 记得python中的random模块,这是所有随机数的藏身之处 记得python中的string模块,这个是字符操作的盛 ...
- C++ memcpy、memmove
函数原型: void *memcpy(void *dest, const void* src, size_t count ); void *memmove(void *dest, const void ...
- Spring Boot 工程开发常见问题解决方案,日常开发全覆盖
本文是 SpringBoot 开发的干货集中营,涵盖了日常开发中遇到的诸多问题,通篇着重讲解如何快速解决问题,部分重点问题会讲解原理,以及为什么要这样做.便于大家快速处理实践中经常遇到的小问题,既方便 ...
- quartus之ram的IP测试
quartus之ram的IP测试 1.基本原理 ram,读取存储器,用于储存数据.基本的原理就是使用时钟驱动时序,利用地址区分位置,使用使能控制写入.输出的结果以写入的位宽输出. 2.实际操作 顶层代 ...
- verilog之时钟信号的编写2
verilog之时钟信号的编写2 1.时钟信号的特点 时钟信号除了可以根据时序一个个变化列举出来,还可以利用其循环的变化的特点,使用循环执行语句always来实现.这种方法实现的时钟信号可以一直执行且 ...
- wordpress自建博客站,为文章添加显示浏览次数功能
wordpress自建博客站,为文章添加显示浏览次数功能 笔者使用的主题是 GeneratePress 版本:3.1.3 1.后台文章管理列表添加浏览次数列 效果如图: 实现: 编辑funct ...
- 通过位运算修改指定bit位的值
通过位运算将指定位的值置0或1 问题样例 假如现在有一个8bit二进制数A,其可以为任何值,所以这里不妨先设A=(xxxxxxxx)2,{x|0,1} 现在需要你将A的几个指定位修改为1或0,例如将A ...