css实现三角提示框

 一、创建不带边框的提示框:

    之前已介绍过怎么生成三角形,直接代码如下:

<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个不同方向的提示框,在使用时可根据自身需要进行调整):

    

参考:https://www.cnblogs.com/houxianzhou/p/14651537.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值