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

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

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

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

画一个梯形的方法

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

遮盖

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

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 高阶技巧,不规则边框解决方案

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

相关推荐
教练、我想打篮球10 分钟前
123 safari 浏览器中下载 URLEncoder.encode 的中文名称的文件, safari 未进行解码, 其他浏览器正常
前端·http·safari
小白x33 分钟前
Echarts常用配置
前端
hello_Code36 分钟前
css和图片主题色“提取”
前端
小杨梅君36 分钟前
Vue3与iframe通信方案详解:本地与跨域场景
前端·vue.js
IT_陈寒38 分钟前
Redis高频踩坑实录:5个不报错但会导致性能腰斩的'隐秘'配置项
前端·人工智能·后端
CoolerWu41 分钟前
2025 · 我与 AI / Vibe Coding 的一年
前端·后端
张风捷特烈43 分钟前
AI 四格笑话爆火,我做了什么?
前端·aigc
东方-教育技术博主43 分钟前
IDEA 配置electron开发环境
前端·javascript·electron
AC赳赳老秦1 小时前
DeepSeek-Coder vs Copilot:嵌入式开发场景适配性对比实战
java·前端·后端·struts·mongodb·copilot·deepseek
一只专注api接口开发的技术猿1 小时前
智能决策数据源:利用 1688 商品详情 API 构建实时比价与供应链分析系统
大数据·前端·数据库