用CSS画一个带阴影的三角形

1. 思路

怎么用CSS画一个带阴影的三角形呢 ? 有童鞋说, 这还不简单吗 网上有不少解决方案, 但其实大多都是实现不太完美的, 存在一些问题css

假设咱们作一个向下的三角形箭头 常见的方法大体有两种html

  1. 经过边框控制, border-left和border-right设为透明, border-top设为预约的颜色便可
  2. 经过 transform 旋转盒子

2. 设计

先把三角形画出来 前端

在这里插入图片描述
html结构

<body>
    <div class="box"></div>
</body>
复制代码

css样式前端工程师

.box {
    position: relative;
    width: 200px;
    height: 100px;
    background: #ff8605;
    box-shadow: 2px 2px 2px rgba(0, 0, 0, .2);
}
.box::after {
    content: '';
    position: absolute;
    bottom: -9px;
    left: 45px;
    border-left: 10px solid transparent;
    border-right: 10px solid transparent;
    border-top: 10px solid #ff8605;
}
复制代码

缺点很明显, 咱们不能经过box-shadow的方式来设置阴影, 阴影会是一个盒子ui

2.1 边框法

若是对阴影要求不那么高,咱们能够再定义一个伪类元素三角形,只是它的颜色和阴影颜色相近, 把这个影子三角形覆盖到咱们原来的三角形的下面便可spa

.box::before {
    position: absolute;
    bottom: -10px;
    left: 45px;
    content: '';
    border-left: 10px solid transparent;
    border-right: 10px solid transparent;
    border-top: 10px solid rgba(0, 0, 0, .1);
}
复制代码

如图所示 设计

在这里插入图片描述

优势是兼容性比较好,要求不严格彷佛也够用了3d

但做为一个严格的前端工程师! 咱们仍是不能容忍这种实现方法code

2.2 滤镜法

正确的姿式是使用滤镜(filter)中的drop-shadow()orm

filter中的drop-shadow属性才是真正意义上的投影,它只会投影出真实图形的阴影

box-shadow只会投影盒模型的阴影

.box::after {
    position: absolute;
    bottom: -9px;
    left: 45px;
    content: '';
    border-left: 10px solid transparent;
    border-right: 10px solid transparent;
    border-top: 10px solid #ff8605;
    filter: drop-shadow(2px 2px 2px rgba(0, 0, 0, .2));
}
复制代码

很完美的实现了效果,缺点是兼容性可能比较差

filter属性兼容性

(滤镜方法是评论区的童鞋提出来的, 感谢~~)

2.3 transform方法

这种方法的思路,三角形的阴影使用盒模型阴影 咱们不画三角形,直接画一个正方形的盒子,再经过transform属性旋转45度便可

.box::before {
    position: absolute;
    bottom: -5px;
    left: 45px;
    content: '';
    width: 10px;
    height: 10px;
    background: #ff8605;
    transform: rotate(135deg);
    box-shadow: 1px -2px 2px rgba(0, 0, 0, .2);
}
复制代码

在这里插入图片描述

咱们彷佛实现了咱们想要的结果, 可是, 实际上是存在一个问题的, 但由于咱们的阴影面积不够大, 因此图片上看起来不明显

当咱们把box-shadow的阴影面积扩大时, 咱们发现到问题的所在了

在这里插入图片描述

盒子突出来了, 那怎么解决呢

咱们再定义一个与容器颜色相同的盒子, 将上半部分盖住就能够啦.

/* transform方法 */
.box::before {
    position: absolute;
    bottom: -5px;
    left: 45px;
    content: '';
    width: 10px;
    height: 10px;
    background: #ff8605;
    transform: rotate(135deg);
    box-shadow: 1px -2px 5px rgba(0, 0, 0, .2);
}
.box::after {
    position: absolute;
    bottom: 0px;
    left: 40px;
    content: '';
    width: 20px;
    height: 20px;
    background: #ff8605;
}
复制代码

要注意三角形应该用before定义, 覆盖的盒子应该用after定义, 这样盒子才能覆盖到三角形上面

实现效果:

在这里插入图片描述

固然这种方法有可能影响盒子内的内容

3. 最终解决方案代码

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width" />
        <title>CSS实现带阴影效果的三角形</title>
        <style> .box { position: relative; width: 200px; height: 100px; background: #ff8605; box-shadow: 2px 2px 2px rgba(0, 0, 0, .2); } /* border法 */ .box::before { position: absolute; bottom: -10px; left: 45px; content: ''; border-left: 10px solid transparent; border-right: 10px solid transparent; border-top: 10px solid rgba(0, 0, 0, .1); } /* drop-shadow */ .box::after { position: absolute; bottom: -9px; left: 45px; content: ''; border-left: 10px solid transparent; border-right: 10px solid transparent; border-top: 10px solid #ff8605; filter: drop-shadow(1px 3px 1px rgba(0, 0, 0, .2)); } /* tranform */ .box::before { position: absolute; bottom: -5px; left: 45px; content: ''; width: 10px; height: 10px; background: #ff8605; transform: rotate(135deg); box-shadow: 1px -2px 5px rgba(0, 0, 0, .2); } .box::after { position: absolute; bottom: 0px; left: 40px; content: ''; width: 20px; height: 20px; background: #ff8605; } </style>
    </head>
    <body>
        <div class="box"></div>
    </body>
</html>
复制代码

若是你有更好的实现办法, 欢迎给我留言

相关文章
相关标签/搜索