纯css实现三角原理,兼容IE

css实现三角形的原理是:当元素的宽高为0,边框(border)不为0时,四个角边框交界重叠处分45度角平分。



如果4种颜色只保留一种颜色,余下3种颜色设置为透明(或者设置为和背景色相同的颜色),就出现一个小三角了。

只保留上面的颜色,如下图:


    display: inline-block;
    width: 0;
    height: 0;
    border-top: 20px solid #639;
    border-right: 20px solid transparent;
    border-left: 20px solid transparent;
    border-bottom:0px solid transparent;


IE6不兼容transparent, 左右border由 solid改为dashed.

 

    display: inline-block;
    width: 0;
    height: 0;
    border-top: 20px solid #639;
    border-right: 20px dashed transparent;
    border-left: 20px dashed transparent;
    border-bottom:0px solid transparent;

Bootstrap中三角也是使用css实现。


html代码:

<span class="caret"></span>

css代码:

.caret {
    display: inline-block;
    width: 0;
    height: 0;
    margin-left: 2px;
    vertical-align: middle;
    border-top: 4px dashed #333;
    border-right: 4px solid transparent;
    border-left: 4px solid transparent;
}

但是在IE8下显示为空白,dashed改为solid。

如果4种颜色只保留一种颜色,余下3种颜色设置为透明(或者设置为和背景色相同的颜色),就出现一个小三角了。

示例3:
只保留上面的红色,如下图:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值