You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SVG边框缩放问题:内容增长时保持边框固定尺寸

自定义SVG边框组件的自适应解决方案需求

我正在开发一个带有自定义SVG边框的UI组件,该边框包含深色背景与白色装饰边缘。核心需求是所有边框(上下左右)保持原始尺寸,仅内部内容区域随内容增长。

已尝试的方案

1. SVG方案

<div class="relative w-full max-w-[550px] min-h-[135px]">
     <svg
            class="absolute inset-0 w-full h-full pointer-events-none"
            viewBox="0 0 550 135"
            preserveAspectRatio="none"
            fill="none"
            xmlns="http://www.w3.org/2000/svg"
    >
        <rect x="37" y="5.8" width="476" height="123.525" fill="#0C1324" fill-opacity="0.75"/>
        <path d="M32.56 129.42V3.537L38.28 1.107V62.829C38.28 63.477 40.32 59.301 41.36 57.141L38.28 129.42H32.56Z" fill="white"/>
        <path d="M517.44 5.58V131.463L511.72 133.893V72.171C511.72 71.523 509.68 75.699 508.64 77.859L511.72 5.58H517.44Z" fill="white"/>
        <path d="M294.8 126.774L31.13 129.384L0 132.426L266.64 131.373L239.8 134.757C347.11 134.757 538.56 135 549.85 135C552.42 135 522.97 131.193 511.72 129.384H283.14L294.8 126.774Z" fill="white"/>
        <path d="M255.2 8.226L518.87 5.616L550 2.574L283.36 3.627L310.2 0.243C202.89 0.243 11.44 0 0.15 0C-2.42 0 27.03 3.807 38.28 5.616H266.86L255.2 8.226Z" fill="white"/>
    </svg>
    <section class="relative w-full h-full text-white">
        <div class="px-10 py-2">
            <!-- Dynamic content -->
        </div>
    </section>
</div>

2. 背景图方案

<div
    style="background-image: url('/border.png'); background-size: 100% 100%; background-position: center; background-repeat: no-repeat;"
    class="relative w-full max-w-[550px] min-h-[135px]"
>
    <section class="relative w-full h-full text-white">
        <div class="px-10 py-2">
            <!-- Dynamic content -->
        </div>
    </section>
</div>

现有问题

两种方案均存在相同问题:当容器随内容增长时,上下边框会垂直缩放变厚,左右边框会水平缩放变宽。我需要边框保持原始尺寸(侧边约10px宽,上下边约17px高)。

核心需求

  • 上下边框保持原始高度(约17px)
  • 左右边框保持原始宽度(约10px)
  • 仅深色背景随内容拉伸
  • 边框保持装饰性与比例
  • 方案适配动态内容高度

是否可以通过SVG、CSS或二者结合实现该效果?我接受任何能解决此缩放问题的方案。

补充说明

此问题不同于仅需固定描边宽度的场景,我处理的是由SVG路径构成的复杂装饰边框(非简单描边),vector-effect="non-scaling-stroke"无法解决该问题。


内容的提问来源于stack exchange,提问作者Flavio Moreno

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 14:47:18