要实现在页面上点击指定元素时,弹出一个信息提示框。在前面的文章中,我们已经简单介绍了如何使用纯 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实现带小三角提示框的更多相关文章

  1. 使用纯CSS实现带箭头的提示框

    爱编程爱分享,原创文章,转载请注明出处,谢谢!http://www.cnblogs.com/fozero/p/6187323.html 1.全部代码 <!DOCTYPE html> < ...

  2. 纯css写带小三角对话框

    在实际样式中经常会遇到要写类似对话框的样式,而这种样式往往会有一个小三角,如下所示: 那么如何用css写出来呢,其实很简单,先让父元素相对定位,然后运用css的伪类before或after.就可以写个 ...

  3. 纯CSS实现带小角的对话框式下拉菜单

    最近公司首页样式重写,头部下拉菜单改为了带小角的对话框式下拉菜单: 很多人可能会用图片,事实上纯CSS就能够实现: HTML: <!DOCTYPE html> <html lang= ...

  4. CSS实现带箭头的提示框

    我们在很多UI框架中看到带箭头的提示框,感觉挺漂亮,但是之前一直不知道其原理,今天网上找了些资料算是弄清楚原理了: 先上效果图: 原理分析: 上面的箭头有没有觉得很像一个三角形,是的,它就是三角形:只 ...

  5. 【小技巧】只用css实现带小三角的对话框样式

    一个小小的技巧: 如图所示,这种小三角,不用图片,只用css怎么实现呢? 直接上代码吧: <!DOCTYPE html> <html> <head> <tit ...

  6. 利用CSS中的:after、: before制作的边三角提示框

    小颖昨天分享了一篇参考bootstrap中的popover.js的css画消息弹框今天给大家再分享一篇使用:before和:after伪元素画消息弹框的CSS. 画出来是介个酱紫的: 有没有觉得画的萌 ...

  7. 用CSS制作小三角提示符号

    今天在项目中遇到了如下图的切图要求. 对,重点就是那个小三角提示符号. html 结构如下 <div class="wrap"> <div class=" ...

  8. 圆角带箭头的提示框css实现

    css是一个很强大的东西,很多网页效果,我们可以通过css直接实现.今天给大家分享的是一个用css实现的圆角带箭头的提示框. 效果如下图: 这一个样式主要涉及到了css的边框样式border的运用和定 ...

  9. 自定义常用input表单元素一:纯css 实现自定义checkbox复选框

    最下面那个是之前写的  今天在做项目的时候发现,之前写的貌似还是有点多,起码增加的span标签可以去掉,这样保持和原生相同的结构最好的,仅仅是样式上的变化.今天把项目中的这个给更新上来.下面就直接还是 ...

  10. css border制作小三角形状及气泡框(兼容IE6)

    先看下CSS盒模型 一个盒子包括: margin+border+padding+content 上下左右边框交界处出呈现平滑的斜线. 利用这个特点, 通过设置不同的上下左右边框宽度或者颜色可以得到小三 ...

随机推荐

  1. inner join on 1=1 在查询中的高级用法

    最近在项目中看到一个查询语句,让我有兴趣去研究.研究.查询语句如下: 重点分析第二个INNER JOIN  ON 1 = 1 这个语句:内连接表示查询两个表的交集,而且ON的条件为 1=1 就表示连接 ...

  2. think about 和 think of 区别

    about 是 on by out 简称 about 在旁边 在外围 周边 think about you 想你有关的事 of 是 belong to 什么什么的 of指的是 这个人或者这个事本身相关 ...

  3. esp8266 I2C 实例解析及源码分析

    一  前言 作为一个方案商兼芯片开发者,研究芯片和功能实现除了基本的工作需要,还有一层就是也变成了一种职业习惯.从芯片到方案,发现很多方案公司的人水平都比较堪忧,只会调用api,根本不会看底层的代码实 ...

  4. Linux高级IO

    readv.writev API: #include <sys/uio.h> ssize_t readv(int fd, const struct iovec* vector, int c ...

  5. Linux 运维工程师面试真题-2-Linux 命令及文件操作

    Linux 运维工程师面试真题-2-Linux 命令及文件操作 1.在/tmp/目录下创建 test.txt 文件,内容为: Hello,World! ,用一个命令写出来. 2.给 test.txt ...

  6. 什么是3D可视化,为什么要使用3D可视化

    虽然许多设计师听说过为什么设计的可视化在他们的审批过程中是有益的,但并不是每个人都知道3D可视化到底是什么. 3D可视化与3D图形.3D渲染.计算机生成图像和其他术语同义使用.3D可视化是指使用计算机 ...

  7. 你是怎么理解ES6中 Generator的?使用场景?

    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 一.介绍 Generator 函数是 ES6 提供的一种异步编程解决方案,语法行为与传统函数完全不同 回顾下上文提到的解决异步的手段: 回 ...

  8. TP6框架--EasyAdmin学习笔记:项目上线

    这是我暂时写EasyAdmin的最后一章,给大家分享下项目上线的全过程,希望对大家有所帮助,废话不多说,直接上内容 服务器我选用的是阿里云,上线时我使用的是宝塔面板来进行部署,如果你是新手,并不熟练服 ...

  9. 记录-html-docs-js避坑指南

    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 前言 我们公司目前在做基于tiptap的在线协同文档,最近需要做导出 pdf.word 需求. 导出 word 文档使用的是html-do ...

  10. PowerDesigner操作要点

    一.PowerDesigner解决name和code同步问题 工具-常规选项-General  Options-Dialog-Name to Code mirroring√去掉 二.PowerDesi ...