【问题标题】:Border of rectangle with curved side带弯曲边的矩形边框
【发布时间】:2019-08-01 04:51:54
【问题描述】:

我有一个看起来像这样的形状

它是一个矩形,后面有一个圆圈。我需要在所有周围做一个边框。

  • 我尝试为矩形做边框,为弯曲部分做曲线 (based on this)。好像不够精确。曲线未与圆形部分 100% 对齐。我需要精确。
  • 将相同的形状放在后面稍大一点不符合我的需要。

代码 - jsfiddle

.template {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.rectangle {
  background: red;
  width: 91mm;
  height: 63mm;
  border-radius: 2mm;
}

.circle {
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  z-index: -999;
  background: red;
  width: 68mm;
  height: 68mm;
  border-radius: 100%;
}
<div class="template">
  <div class="rectangle"></div>
  <div class="circle"></div>
</div>

有什么想法可以实现这个甜蜜的边界吗?

【问题讨论】:

标签: css css-shapes


【解决方案1】:

使用带有径向渐变的伪元素:

.box {
  width:200px;
  height:150px;
  background:red;
  border-radius:10px;
  position:relative;
  margin-top:50px;
}
.box:before {
  content:"";
  position:absolute;
  bottom:100%;
  left:0;
  right:0;
  height:50px; /* Same as margin*/
  background:radial-gradient(circle,red 49.5%,transparent 50%) top/150% 400%;
  
}
<div class="box">

</div>

然后就可以添加边框了:

.box {
  width:200px;
  height:150px;
  background:red;
  border-radius:10px;
  position:relative;
  margin-top:50px;
  border:3px solid blue;
}
.box:before {
  content:"";
  position:absolute;
  bottom:100%;
  left:0;
  right:0;
  height:50px; /* Same as margin*/
  background:radial-gradient(circle,red 49.5%,blue 50% calc(50% + 3px),transparent calc(50% + 4px)) top/150% 400%;
  
}
<div class="box">

</div>

并且还要使用透明色:

.box {
  width:200px;
  height:150px;
  background:rgba(255,0,0,0.5) padding-box;
  border-radius:10px;
  position:relative;
  margin-top:50px;
  border:3px solid blue;
  border-top-color:transparent;
}
.box:before {
  content:"";
  position:absolute;
  bottom:100%;
  left:0;
  right:0;
  height:50px; /* Same as margin*/
  background:radial-gradient(circle,rgba(255,0,0,0.5) 49.5%,blue 50% calc(50% + 3px),transparent calc(50% + 4px)) top/150% 400%; 
}
.box:after {
  content:"";
  position:absolute;
  top:-3px;
  height:100%;
  left:-3px;
  right:-3px;
  border-top:3px solid blue;
  border-right:3px solid transparent;
  border-left:3px solid transparent;
  border-radius:inherit;
  clip-path:polygon(0 0,28px 0,28px 50px,calc(100% - 28px) 50px,calc(100% - 28px) 0, 100% 0,100% 100%,0 100%);
}

body {
 background:url(https://picsum.photos/id/1001/800/800) center/cover;
}
<div class="box">

</div>

【讨论】:

  • 我认为这里的重点是 OP 希望在这个形状周围有一个 border
  • 然后我们添加边框
  • 好多了! ☺☺
  • 谢谢!看起来它可以工作,需要改变我制作形状的方式,但结果似乎是我需要的。你介意解释一下径向梯度发生了什么吗?
  • @Ruben 我正在使用径向渐变来定义圆。然后我只需调整位置 (top) 和大小 (150% 400%) 即可获得所需的结果。玩价值,你会看到
猜你喜欢
  • 2015-05-05
  • 1970-01-01
  • 1970-01-01
  • 2018-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-31
  • 2010-09-26
相关资源
最近更新 更多