平滑大小变化的边框

Border with smooth size change

我正在尝试实现平滑的边框大小过渡,如下图所示:

现在我怀疑纯 CSS/HTML 是否可行。我错了吗?

我会用它作为图像背景,但这个解决方案会很麻烦,因为会有很多不同长度的元素。

还有其他方法吗?

这是使用变换和透视的近似值(没有透明度)

.box {
  width: 200px;
  height: 100px;
  margin: 20px;
  position: relative;
  border: 20px solid red;
  border-width: 20px 3px 20px 20px;
  border-radius:55px;
  transform:perspective(112px) rotateY(5deg);
  z-index: 1;
}

.box:before {
  content: "";
  position: absolute;
  top: -5px;
  left: -5px;
  right: 35px;
  bottom: -5px;
  background: #fff;
  z-index: -1;
  border-radius: 40px;
  transform: perspective(112px) rotateY(-7deg);
  transform-origin: left;
}
<div class="box">

</div>

你可以用两个伪元素来实现能够在里面添加内容:

.box {
  width: 200px;
  height: 100px;
  margin: 20px;
  position: relative;
  z-index: 0;
  box-sizing: border-box;
  padding: 40px;
}

.box:before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  border: 20px solid red;
  border-width: 20px 3px 20px 20px;
  border-radius: 55px;
  transform: perspective(112px) rotateY(5deg);
  z-index: -1;
}

.box:after {
  content: "";
  position: absolute;
  top: 10%;
  left: 5%;
  right: 19%;
  bottom: 10%;
  background: #fff;
  z-index: 1;
  border-radius: 36px;
  transform: perspective(112px) rotateY(-7deg);
  transform-origin: left;
  z-index: -1;
}
<div class="box">
  some text
</div>


这是另一个 复杂 背景颜色的想法,您可以在其中获得透明度并更好地控制响应:

.box {
  max-width:200px;
  margin:10px;
  position:relative;
  padding:30px 20px;
  z-index:0;
  box-sizing:border-box;
  display:inline-block;
}
.box:before {
  content:"";
  position:absolute;
  z-index:-1;
  top:0;
  left:0;
  right:0;
  bottom:0;
  border:2px solid red;
  border-radius:40px;
  background:
    /*    gradient                                                    position       / size*/
    radial-gradient(circle at bottom right,transparent 23px,red 25px) top left       /34px 32px,  
    radial-gradient(circle at top right   ,transparent 23px,red 25px) bottom left    /34px 32px,
    linear-gradient(red,red)                                          left           /10px 100%,
    linear-gradient(to top left   ,transparent 45%,red 50%)           top left       /85% 10px,
    linear-gradient(to bottom left,transparent 45%,red 50%)           bottom left    /85% 10px;
  background-repeat:no-repeat;
  transform:perspective(100px) rotateY(5deg);
}

body {
 background:pink;
}
<div class="box">
  some text some text some text some text some text
</div>
<div class="box">
  some text some text
</div>
<div class="box">
  some text text
</div>
<div class="box">
  some text 
  some text 
  some text
</div>

为了更好地理解着色,更改每个渐变的颜色:

.box {
  max-width:200px;
  margin:10px;
  position:relative;
  padding:30px 20px;
  z-index:0;
  box-sizing:border-box;
  display:inline-block;
}
.box:before {
  content:"";
  position:absolute;
  z-index:-1;
  top:0;
  left:0;
  right:0;
  bottom:0;
  border:2px solid red;
  border-radius:40px;
  background:
    radial-gradient(circle at bottom right,transparent 23px,rgba(200,0,0,0.6) 25px) top left/34px 32px,  
    radial-gradient(circle at top right   ,transparent 23px,rgba(200,0,0,0.6) 25px) bottom left/34px 32px,
    linear-gradient(yellow,yellow) left/10px 100%,
    linear-gradient(to top left   ,white 45%,green 50%) top left   /85% 10px,
    linear-gradient(to bottom left,white 45%,black 50%) bottom left/85% 10px;
  background-repeat:no-repeat;
  transform:perspective(100px) rotateY(5deg);
}

body {
 background:pink;
}
<div class="box">
  some text some text some text some text some text
</div>