纯CSS实现tooltip提示框,CSS箭头及形状之续篇--给整个tooltip提示框加个边框
在前面一篇中我们介绍了纯CSS实现tooltip提示框,通俗的讲也就是CSS箭头及形状
不过注意一点是,他始终是一个元素,只是通过CSS实现的,今天我们要说的是给这个“tooltip提示框”整体加一个边框,下面是是一篇完成的截图(不了解的可以看看:纯CSS实现tooltip提示框,CSS箭头及形状):

首先像:after一样我们介绍另外一个CSS中“ :before ”选择器
定义和用法:(参考w3school:before选择器)
:before 选择器在被选元素的内容前面插入内容,使用 content 属性来指定要插入的内容
例:
p:before
{
content:"台词:-";
background-color:yellow;
color:red;
font-weight:bold;
}
下面一步一步实现给该tooltip整体添加边框:
首先HTML代码:
<body>
<div class="demo"></div>
</body>
让我们来设置盒子的样式
<style>
.demo{
background-color: gray;
height: 100px;
position: relative;
width: 100px;
}
</style>
截图:

(其中具体的position的设定缘由大家看前一篇的解释,谢谢~~)
接着“引入”箭头,注意这里要同时用到“ :after ”和“ :before ”两个CSS选择器同时产生不同尺寸的箭头,基本样式:
<style>
.demo{
background-color: gray;
height: 100px;
position: relative;
width: 100px;
}
.demo:after,.demo:before{
content:'';
position:absolute;
}
.demo:after{
height:20px;
width:20px;
background:yellow;
}
.demo:before{
height:30px;
width:30px;
background:green;
}
</style>
截图:

继续,我们要给黄色方块和绿色方块设置边框,样式:
<style>
.demo{
background-color: gray;
height: 100px;
position: relative;
width: 100px;
}
.demo:after,.demo:before{
content:'';
position:absolute;
}
.demo:after{
height:20px;
width:20px;
background:yellow; border:5px solid lightgreen;
}
.demo:before{
height:30px;
width:30px;
background:green; border:5px solid red;
}
</style>
截图:

再者我们将黄色方块和绿色方块内容去掉,通过去掉:after和:before的height、width,仅剩下浅绿色方框和红色方框,分别是黄色方块、绿色方块的边框,样式:
<style>
.demo{
background-color: gray;
height: 100px;
position: relative;
width: 100px;
}
.demo:after,.demo:before{
content:'';
position:absolute;
}
.demo:after{
//height:20px;
//width:20px;
background:yellow; border:5px solid lightgreen;
}
.demo:before{
//height:30px;
//width:30px;
background:green; border:5px solid red;
}
</style>
截图:

这里注意红色的边框被覆盖了,所以我们需要给浅绿色方块和红色方块增加像素,但是增加的像素值彼此不相同,为后续做准备,样式:
<style>
.demo{
background-color: gray;
height: 100px;
position: relative;
width: 100px;
}
.demo:after,.demo:before{
content:'';
position:absolute;
}
.demo:after{
//height:20px;
//width:20px;
background:yellow; border:10px solid lightgreen;
}
.demo:before{
//height:30px;
//width:30px;
background:green; border:15px solid red;
}
</style>
截图:

注意,这里我们将浅绿色的边框像素值由5px改为了10px;而红色边框由5px改为了15px,如上图,但值得小心的是区分这里的浅绿色方框、红色方框与上面的黄色方块、绿色方块样子相同,但性质却截然不同,一种是边框,一种是方块~~而实现箭头是通过边框来实现的,原理参见上一篇纯CSS实现tooltip提示框,CSS箭头及形状
下面来实现箭头,样式:
<style>
.demo{
background-color: gray;
height: 100px;
position: relative;
width: 100px;
}
.demo:after,.demo:before{
content:'';
position:absolute;
}
.demo:after{
//height:20px;
//width:20px;
//background:yellow; //border:10px solid lightgreen;
border:10px solid transparent;
border-top-color:lightgreen;
}
.demo:before{
//height:30px;
//width:30px;
//background:green; //border:15px solid red;
border:15px solid transparent;
border-top-color:red;
}
</style>
截图:

然后我们通过position:absolute的top、right、bottom、left,以下简称TRBL来设置箭头及边框(这里指红色边框,即将成为箭头的边框)的位置,样式:
<style>
.demo{
background-color: gray;
height: 100px;
position: relative;
width: 100px;
}
.demo:after,.demo:before{
content:'';
position:absolute;
}
.demo:after{
//height:20px;
//width:20px;
//background:yellow; //border:10px solid lightgreen;
border:10px solid transparent;
border-top-color:lightgreen;
top:100px;
left:20px;
}
.demo:before{
//height:30px;
//width:30px;
//background:green; //border:15px solid red;
border:15px solid transparent;
border-top-color:red;
top:100px;
left:15px;
}
</style>
截图:

接着来设置灰色盒子的边框为红色,边框大小与红色相同,同时将箭头浅绿色改为灰色,使其看起来浑然一体,样式:
<style>
.demo{
background-color: gray;
height: 100px;
position: relative;
width: 100px;
border:2.75px solid red;
}
.demo:after,.demo:before{
content:'';
position:absolute;
}
.demo:after{
//height:20px;
//width:20px;
//background:yellow; //border:10px solid lightgreen;
border:10px solid transparent;
border-top-color:gray;
top:100px;
left:20px;
}
.demo:before{
//height:30px;
//width:30px;
//background:green; //border:15px solid red;
border:15px solid transparent;
border-top-color:red;
top:100px;
left:15px;
}
</style>
截图:

其中灰色盒子的边框2.75px与箭头边框红色部分的宽度计算,大家自己捉摸捉摸哈~~数学问题!
至此,我们的续写,给“tooltip提示框”添加个边框到此结束!具体颜色样式大家可以随自己要求自己做修改~~
更多知识分享:微笑空间站
纯CSS实现tooltip提示框,CSS箭头及形状之续篇--给整个tooltip提示框加个边框的更多相关文章
- 纯CSS实现tooltip提示框,CSS箭头及形状
本片介绍仅用CSS做出tooltip那样的提示框及箭头等形状! 首先介绍一下CSS:after选择器 定义和用法:(参考w3school:after选择器) :after选择器在被选元素的内容后面插入 ...
- 修改Tooltip 文字提示 的背景色 箭头颜色
3==>vue 鼠标右击<div @contextmenu.prevent="mouseRightClick">prevent是阻止鼠标的默认事件 4==> ...
- 点击盒子选中里面的单选框,并给盒子添加相应样式,美化单选框、复选框样式css用法,响应式滴
pc效果图: 移动端效果图: 代码直接上: <!DOCTYPE html> <html> <head> <meta http-equiv="Cont ...
- 【CSS】CSS画矩形、圆、半圆、弧形、半圆、小三角、疑问框
在网页中,常常会用到各种Icon,假设老是麻烦设计狮画出来不免有些不好意思,所以有时候我们也能够用CSS写出各种简单的形状.一来能够减轻他们的负担,二来也能够使用CSS替代图片.提高载入速度. 在网页 ...
- CSS画一个三角形,CSS绘制空心三角形,CSS实现箭头
壹 ❀ 引 这两天因为项目工作较少,闲下来去看了GitHub上关于面试题日更收录的文章,毕竟明年有新的打算.在CSS收录中有一题是 用css创建一个三角形,并简述原理 .当然对于我来说画一个三角形是 ...
- CSS画矩形、圆、半圆、弧形、半圆、小三角、疑问框
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- CSS制作小旗子与小箭头
CSS制作小旗子与小箭头 效果图如下: 小旗子效果图 小箭头效果图 小旗子效果 以下是具体实现代码: <div class="container"> <div c ...
- 清除Css中select的下拉箭头样式
select {/*Chrome和Firefox里面的边框是不一样的,所以复写了一下*/border: solid 1px #000; /*很关键:将默认的select选择框样式清除*/appeara ...
- Day46(列表标签,表格标签,表单标签,css的引入方式,css选择器)
一.列表标签 列表标签分为三种. 1.无序列表<ul>,无序列表中的每一项是<li> 英文单词解释如下: ul:unordered list,“无序列表”的意思. li:lis ...
随机推荐
- gridview自定义排序
效果如图: 首先允许排序:AllowSorting="True":开启gridview的排序事件onsorting="GridView1_Sorting",也可 ...
- ural 1075. Thread in a Space
1075. Thread in a Space Time limit: 1.0 secondMemory limit: 64 MB There are three points in a 3-dime ...
- Codeforces Round #363 Div.2[111110]
好久没做手生了,不然前四道都是能A的,当然,正常发挥也是菜. A:Launch of Collider 题意:20万个点排在一条直线上,其坐标均为偶数.从某一时刻开始向左或向右运动,速度为每秒1个单位 ...
- codeforces round #234B(DIV2) A Inna and Choose Options
#include <iostream> #include <string> #include <vector> using namespace std; ; ,,, ...
- js中继承的几种用法总结(apply,call,prototype)
一,js中对象继承 js中有三种继承方式 1.js原型(prototype)实现继承 <SPAN style="BACKGROUND-COLOR: #ffffff">& ...
- CF 346B. Lucky Common Subsequence(DP+KMP)
这题确实很棒..又是无想法..其实是AC自动机+DP的感觉,但是只有一个串,用kmp就行了. dp[i][j][k],k代表前缀为virus[k]的状态,len表示其他所有状态串,处理出Ac[len] ...
- Android -- TextView、button方法详解(2)
1. button按下状态的改变 Button bt1 = null; // 声明按钮对象 bt1 = (Button) findViewById(R.id.button1); // 获取按钮对象 b ...
- js小效果-全选
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...
- eclipse配置PHP开发环境
下载 http://www.oracle.com/technetwork/java/javase/downloads/index.html下载JDK,Eclipse 安装需要JDK环境:http:// ...
- 清除BOM头源码
BOM: Byte Order Mark UTF-8 BOM又叫UTF-8 签名,其实UTF-8 的BOM对UFT-8没有作用,是为了支援UTF-16,UTF-32才加上的BOM,BOM签名的意思就是 ...