渐变消息框

55 阅读2分钟

image.png


<!doctype html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>渐变边框卡片 - 带底部三角</title>
  <style>
    :root{
      /* 可调整项 */
      --width: 220px;
      --height: 160px;
      --radius: 12px;
      --border-thickness: 3px;
      --triangle-size: 14px; /* 外三角高度(含边框)*/
      /* 纵向渐变颜色(上 -> 下)*/
      --grad-top: #8b5cf6;
      --grad-bottom: #06b6d4;
      --bg: #ffffff; /* 卡片内部底色 */
      --content-padding: 18px;
       --triangle-offset: 0px; /* 新增变量:三角形上下偏移度,正数=往上移,负数=往下移 */
    }

    body{
      display: grid;
      place-items: center;
      min-height: 100vh;
      background: #f3f6fb;
      margin: 0;
      font-family: "PingFang SC","Helvetica Neue",Arial, sans-serif;
    }

    /* 外层作为渐变“边框”容器 */
    .grad-card {
      position: relative;
      width: var(--width);
      height: var(--height);
      padding: var(--border-thickness); /* 用于显示渐变边框宽度 */
      border-radius: calc(var(--radius) + var(--border-thickness));
      /* 上下渐变(纵向)作为边框颜色 */
      background: linear-gradient(180deg, var(--grad-top), var(--grad-bottom));
      /* 防止内层背景溢出圆角 */
      box-sizing: border-box;
    }

    /* 内层白色卡片实际显示内容 */
    .grad-card .inner {
      width: 100%;
      height: 100%;
      border-radius: var(--radius);
      background: var(--bg);
      box-sizing: border-box;
      padding: var(--content-padding);
      display: flex;
      flex-direction: column;
      justify-content: center;
      /* 可选阴影,让卡片更浮起 */
      box-shadow: 0 6px 18px rgba(12,20,40,0.08);
      position: relative;
      overflow: visible;
    }

    /* 底部外部渐变三角(作为边框) */
    .grad-card::before {
      content: "";
      position: absolute;
      left: 50%;
      transform: translateX(-50%);
      bottom: calc(-1 * (var(--triangle-size) - 2px)); /* 紧贴卡片底部外边缘 */
      width: calc(var(--triangle-size) * 2); /* 宽高比2:1 */
      height: var(--triangle-size);
      background: linear-gradient(180deg,var(--grad-bottom));
      clip-path: polygon(0% 0%, 50% 100%, 100% 0%);
      z-index: 1;
    }

    /* 底部内层白色三角,覆盖外三角中间部分,形成“带边框”的三角效果 */
    .grad-card::after {
      content: "";
      position: absolute;
      left: 50%;
      transform: translateX(-50%);
      bottom: calc(-1 * (var(--triangle-size) - var(--border-thickness)) - 4); /* 略微上移以对齐边框 */
      width: calc((var(--triangle-size) - var(--border-thickness)) * 2);
      height: calc(var(--triangle-size) - var(--border-thickness));
      background: var(--bg);
      clip-path: polygon(0% 0%, 50% 100%, 100% 0%);
      z-index: 2;
    }

    /* 内容样式:根据你的示例样式微调 */
    .title {
      font-size: 14px;
      color: #667085;
      margin: 0 0 6px 0;
      font-weight: 600;
    }
    .value {
      font-size: 40px;
      font-weight: 800;
      color: #0f172a;
      margin: 0;
      line-height: 1;
    }
    .muted-row {
      margin-top: 8px;
      display: flex;
      align-items: center;
      gap: 8px;
      color: #94a3b8;
      font-size: 14px;
    }
    .muted-row .up {
      color: #10b981; /* 绿色向上/向下示例 */
      font-weight: 700;
    }
  </style>
</head>
<body>
  <div class="grad-card" aria-hidden="false">
    <div class="inner" role="group" aria-label="指标卡片">
      <div class="title">新增评价数</div>
      <div class="value">208</div>
      <div class="muted-row">
        <div>较上期</div>
        <div class="up">↓ 20</div>
      </div>
    </div>
  </div>
</body>
</html>