CSS部分边框带有圆角端点

huangapple go评论49阅读模式
英文:

CSS partial border with rounded caps

问题

我正在尝试创建一个带有圆角端部的部分边框矩形,就像下面的图片一样:

CSS部分边框带有圆角端点

我已经做到了移除边框的一部分。但我无法想出如何轻松地获得图片中的结果:

body {
    position: relative;
}

div.inner {
    position: absolute;
    top: 10px;
    left: 10px;
    display: inline-block;
    width: 420px;
    height: 320px;
    background-color: #f80;
    border-radius: 40px;
}

div.border {
    position: absolute;
    display: inline-block;
    width: 400px;
    height: 300px;
    background-color: transparent;
    border-radius: 40px;
    border: 20px solid black;
    clip-path: polygon(0% 0%, 0% 100%, 100% 100%, 100% 0%, 60% 0%, 60% 75%, 25% 75%, 25% 0%);
}
<body>
  <div class="inner"></div>
  <div class="border"></div>
</body>
英文:

I am trying to create a rectangle with partial borders with rounded caps. Like the image below:

CSS部分边框带有圆角端点

I have come this far to remove a part of the border. But I can't think of an easy way to get the result in the picture:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

body {
    position: relative;
}

div.inner {
    position: absolute;
    top: 10px;
    left: 10px;
    display: inline-block;
    width: 420px;
    height: 320px;
    background-color: #f80;
    border-radius: 40px;
}

div.border {
    position: absolute;
    display: inline-block;
    width: 400px;
    height: 300px;
    background-color: transparent;
    border-radius: 40px;
    border: 20px solid black;
    clip-path: polygon(0% 0%, 0% 100%, 100% 100%, 100% 0%, 60% 0%, 60% 75%, 25% 75%, 25% 0%);
}

<!-- language: lang-html -->

&lt;body&gt;
  &lt;div class=&quot;inner&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;border&quot;&gt;&lt;/div&gt;
&lt;/body&gt;

<!-- end snippet -->

答案1

得分: 1

这是一个使用一个元素的想法。它可能看起来复杂,但你只需要调整CSS变量。

.box {
  --t: 20px; /* 边框厚度 */
  --c: #000; /* 边框颜色 */
  --x: 30%; /* 间隙的起始位置 */
  --y: 60%; /* 间隙的结束位置 */

  margin: 50px;
  width: 300px;
  height: 200px;
  background: orange; /* 背景颜色 */
  position: relative;
  border-radius: 40px; /* 圆角半径 */
}
/* 以下部分无需修改 */
.box:before {
  content: "";
  position: absolute;
  inset: calc(var(--t)/-2);
  border-radius: inherit;
  padding: var(--t);
  --g: calc(var(--t)/2), var(--c) 49%, #0000 51%;
  background: 
   radial-gradient(var(--t) at var(--x) var(--g)),
   radial-gradient(var(--t) at var(--y) var(--g)),
   linear-gradient(90deg,var(--c) var(--x),#0000 0 var(--y),var(--c) 0),
   linear-gradient(var(--c) 0 0) 0 var(--t) no-repeat;
  -webkit-mask:
    linear-gradient(#000 0 0),
    linear-gradient(#000 0 0) content-box;
  -webkit-mask-composite: xor;
          mask-composite: exclude;
}
<div class="box"></div>

<div class="box" style="--t: 26px; --x: 50px; --y: 80%"></div>
英文:

Here is an idea using one element. It may look complex but all you have to do is to adjust the CSS variables

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.box {
  --t: 20px; /* border thickness */
  --c: #000; /* border color */
  --x: 30%; /* the start of the gap */
  --y: 60%; /* the end of the gap */

  margin: 50px;
  width: 300px;
  height: 200px;
  background: orange; /* background color */
  position: relative;
  border-radius: 40px; /* radius */
}
/* you don&#39;t need to touch the below*/
.box:before {
  content:&quot;&quot;;
  position: absolute;
  inset: calc(var(--t)/-2);
  border-radius: inherit;
  padding: var(--t);  
  --g: calc(var(--t)/2), var(--c) 49%,#0000 51%;
  background: 
   radial-gradient(var(--t) at var(--x) var(--g)),
   radial-gradient(var(--t) at var(--y) var(--g)),
   linear-gradient(90deg,var(--c) var(--x),#0000 0 var(--y),var(--c) 0),
   linear-gradient(var(--c) 0 0) 0 var(--t) no-repeat;
  -webkit-mask:
    linear-gradient(#000 0 0),
    linear-gradient(#000 0 0) content-box;
  -webkit-mask-composite: xor;
          mask-composite: exclude;
}

<!-- language: lang-html -->

&lt;div class=&quot;box&quot;&gt;&lt;/div&gt;

&lt;div class=&quot;box&quot; style=&quot;--t: 26px;--x: 50px;--y: 80%&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月13日 22:14:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680365.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定