css+div打造三角形(箭头)

本文章转自:https://www.cnblogs.com/MirageFox/p/5877599.html

 

 

在很多网站都见过这样的箭头,之前我一直以为是图片,直到今天才知道原来可以用css做。开始看代码没太看懂,后来自己试了几遍才恍然大悟。贴出来分享下。(大神请直接忽略)

先看代码:

 

.sanjiao{
    width:0px;
    height:0px;
    overflow:hidden;
    border-width:10px;
    border-color:transparent transparent blue transparent;
    border-style:dashed dashed solid dashed;
}

 

 

咋一看我确实没明白原因。(看懂的后面可以忽略)

*****************************************************************************************************************

一步步分析:

1、画div

.sanjiao{
    width:30px;
    height:30px;
    background-color:black;
}

2、给边框,分开给

.sanjiao{
    width:30px;
    height:30px;
    background-color:black;

    border-top:solid red 20px;
    border-left:solid blue 20px;
    border-bottom:solid yellow 20px;
    border-right:solid green 20px;
}

(相信聪明的童靴马上就看明白了,我也是在这才明白)

3、去掉中间的div

.sanjiao{
    width:0;
    height:0;

    border-top:solid red 20px;
    border-left:solid blue 20px;
    border-bottom:solid yellow 20px;
    border-right:solid green 20px;
}

(是不是已经看懂了)

4、优化代码

.sanjiao{
    /* 设定div大小 */
    width:0;
    height:0;
    /* 防溢出,稳固兼容性 */
    overflow:hidden;
    /* 箭头尺寸 */
    border-width:10px;
    /* 给箭头着色,四个值分别是边框的四个方向,箭头的方向正好相反 */
    border-color:blue transparent transparent transparent;
    /* 为了兼容性,最好把四个值都补上,需要的方向设实线,其他方向虚线 */
    border-style:solid dashed dashed dashed;
}

搞定,最后的优化注释已经很详细了。

  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值