不规则形状 --- 边框、阴影

最近遇到了一个需求,元素是一个梯形。

梯形好办,但是还要沿着这个梯形有一个边框,如下图,那就没那么好办了。

这里总结 一下梯形元素或者说是这种不规则元素加边框的方法。

画一个梯形的方法

首先我们不看边框,先看一下画这种梯形的方法有哪些:

遮盖

首先是最容易想到的,我们直接画一个矩形,然后在右下角放一个白色的三角形,遮盖住。

html 复制代码
<html>
  <head>
    <style>
      .shape {
        width: 200px;
        height: 100px;
        background-color: rgb(102, 177, 255);
        position: relative;
      }
      .shape::after {
        content: '';
        position: absolute;
        width: 0;
        height: 0;
        bottom: 0px;
        right: 0px;
        border-left: 10px solid transparent;
        border-right: 30px solid white;
        border-top: 20px solid transparent;
      }
    </style>
  </head>
  <body>
    <div class="shape"></div>
  </body>
</html>

background渐变

我们也可以利用background的渐变来得到一个梯形的样子,让三角区域透明

html 复制代码
.shape {
        width: 200px;
        height: 100px;
        background: linear-gradient(
          150deg,
          rgb(102, 177, 255) 90%,
          transparent 90%,
          transparent 100%
        );
      }

mask 遮罩

除了直接用渐变色生成形状之外,我们还能反向操作,利用渐变对右下角进行遮罩

js 复制代码
.shape {
        width: 200px;
        height: 100px;
        background-color: rgb(102, 177, 255);
        -webkit-mask: linear-gradient(
          150deg,
          rgb(102, 177, 255) 90%,
          transparent 90%,
          transparent 100%
        );
      }

clip-path

我们还可以直接在矩形上裁剪出一个梯形出来

html 复制代码
.shape {
        width: 200px;
        height: 100px;
        background-color: rgb(102, 177, 255);
        clip-path: polygon(0% 0%, 100% 0%, 100% 85%, 85% 100%, 0% 100%);
      }

边框和阴影

下面看看如何在上面梯形的基础上加上边框

叠加法

所谓叠加法的原理就是,在已经得到一个梯形的基础上,再弄一个梯形,但是把它放大一点作为地图,背景颜色也设置为边框或者阴影的颜色或者渐变,两个梯形一叠加,就能得到需要的效果。

这个方法适合上面的background渐变法、 mask遮罩法、 clip-path方法,我们拿clip-path方法举例

html 复制代码
<!DOCTYPE html>
<html>
  <head>
    <style>
      .container {
        position: relative;
        width: 200px;
        height: 100px;
      }

      .shape {
        width: 100%;
        height: 100%;
        background-color: rgb(102, 177, 255);
        clip-path: polygon(0% 0%, 100% 0%, 100% 85%, 85% 100%, 0% 100%);
        position: absolute;
        z-index: 2;
      }

      .shadow1 {
        position: absolute;
        top: 50%;
        left: 50%;
        width: 102%;
        height: 104%;
        background-color: rgba(0, 0, 0, 0.5);
        clip-path: polygon(0% 0%, 100% 0%, 100% 85%, 85% 100%, 0% 100%);
        transform: translate(-50%, -50%);
        z-index: 1;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="shadow1"></div>
      <div class="shape"></div>
    </div>
  </body>
</html>

drop-shadow

我们还可以利用drop-shadpw来生成阴影

这个方法适用于background渐变

js 复制代码
<html>
  <head>
    <style>
      .shape {
        width: 200px;
        height: 100px;
        background: linear-gradient(
          150deg,
          rgb(102, 177, 255) 90%,
          transparent 90%,
          transparent 100%
        );
        filter: drop-shadow(0px 0px 0.5px #000);
      }
    </style>
  </head>
  <body>
    <div class="shape"></div>
  </body>
</html>

同时,多个drop-shadow的叠加,也有了边框的效果

这个方法是在Chokcoco老师那里看见的,最下方有链接

js 复制代码
filter: drop-shadow(0px 0px 0.5px #000) drop-shadow(0px 0px 0.5px #000)
          drop-shadow(0px 0px 0.5px #000);

svg滤镜法

这个方法也是在Chokcoco老师那里学到的

这里用svg的滤镜,借助了feMorphology的扩张能力,为我们的标签生成边框和阴影

这个方法适用于background渐变

html 复制代码
<html>
  <head>
    <style>
      .shape {
        width: 200px;
        height: 100px;
        background: linear-gradient(
          150deg,
          rgb(102, 177, 255) 90%,
          transparent 90%,
          transparent 100%
        );
        filter: url(#outline);
      }
    </style>
  </head>
  <body>
    <div class="shape"></div>
    <svg width="0" height="0">
      <filter id="outline">
        <feMorphology
          in="SourceAlpha"
          result="DILATED"
          operator="dilate"
          radius="1"
        ></feMorphology>
        <feMerge>
          <feMergeNode in="DILATED" />
          <feMergeNode in="SourceGraphic" />
        </feMerge>
      </filter>
    </svg>
  </body>
</html>

以上就是总结的生成梯形及其阴影边框的一些方法,那么推广到其他不规则图形,我们用上面的方法,还能画出其他很多形状。

其他形状

参考文章

现代 CSS 高阶技巧,不规则边框解决方案

有意思!不规则边框的生成方案

相关推荐
酒尘&1 小时前
JS数组不止Array!索引集合类全面解析
开发语言·前端·javascript·学习·js
学历真的很重要1 小时前
VsCode+Roo Code+Gemini 2.5 Pro+Gemini Balance AI辅助编程环境搭建(理论上通过多个Api Key负载均衡达到无限免费Gemini 2.5 Pro)
前端·人工智能·vscode·后端·语言模型·负载均衡·ai编程
用户47949283569153 小时前
"讲讲原型链" —— 面试官最爱问的 JavaScript 基础
前端·javascript·面试
用户47949283569153 小时前
2025 年 TC39 都在忙什么?Import Bytes、Iterator Chunking 来了
前端·javascript·面试
大怪v4 小时前
【Virtual World 04】我们的目标,无限宇宙!!
前端·javascript·代码规范
狂炫冰美式4 小时前
不谈技术,搞点文化 🧀 —— 从复活一句明代残诗破局产品迭代
前端·人工智能·后端
xw55 小时前
npm几个实用命令
前端·npm
!win !5 小时前
npm几个实用命令
前端·npm
代码狂想家5 小时前
使用openEuler从零构建用户管理系统Web应用平台
前端
dorisrv6 小时前
优雅的React表单状态管理
前端