将圆角边框添加到元素的选定角上

dav*_*qet 5 html css rounded-corners css3 css-shapes

我怎么能用纯CSS来构建这样的东西呢?

SS

到目前为止,我已经走了多远:小提琴

即使我继续添加额外的spans ,我也在努力想要在那里获得圆角.


码:

body {
  background: #000;
}

.container {
  position: relative;
  width: 300px;
  height: 150px;
  margin: 10% auto;
}

.top-right {
  position: absolute;
  top: -10px;
  right: 0;
  width: 50px;
  height: 1px;
  background: white;
  border-radius: 5px;
}

.box {
  width: 100%;
  height: 100%;
  background: red;
  border-radius: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
}

h3 {
  color: white;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
  <span class="top-right"></span>
  <div class="box">
    <h3>Content</h3>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

dip*_*pas 11

您可以通过使用伪元素实现这一::before/ ::after.box使用性能borderborder-radius

body {
  background: #000;
}
.container {
  width: 300px;
  height: 150px;
  margin: 3% auto 0 /* changed for demo */
}
h3 {
  color: white;
}
.box {
  width: 100%;
  height: 100%;
  background: red;
  border-radius: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}
.box::before,
.box::after {
  content: "";
  position: absolute;
  border: solid white;
  width: 50px;
  height: 50px;
}
.box::before {
  top: -15px;
  left: -15px;
  border-radius: 15px 0; /* top-left */
  border-width: 5px 0 0 5px;
}
.box::after {
  bottom: -15px;
  right: -15px;
  border-radius: 0 0 15px; /* bottom-right */
  border-width: 0 5px 5px 0;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
  <div class="box">
    <h3>Content</h3>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)


Mic*_*l_B 5

使用伪元素将是理想的解决方案.

这个答案只是一个选择.虽然没有语义优雅,但它的效果非常好.

  • 创建一个包含四个div的容器.
  • 第一个div将是白色边框.
  • 最后一个div将是你的红盒子.
  • 中间的两个div将用于隐藏白色边框区域.

HTML非常简单:

<div class="container">
    <div class="box box1"></div>
    <div class="box box2"></div>
    <div class="box box3"></div>
    <div class="box box4">
        <h3>Content</h3>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

通过绝对定位,可以移动.box2(绿色)和.box3(蓝色)覆盖边框.

在此输入图像描述

源中框的顺序并不重要.但是使用上面的HTML,不需要该z-index属性.

现在,唯一剩下的就是将方框2和3的背景颜色更改为黑色.

在此输入图像描述

完整代码:

body {
  margin: 0;
  height: 100vh;
  background-color: black;
  display: flex;
}
.container {
  position: relative;
  width: 300px;
  height: 150px;
  margin: auto;
}
.box {
  position: absolute;
  width: 300px;
  height: 150px;
  border-radius: 15px;
}
.box1 {
  border: 5px solid white;
  width: 320px;
  height: 170px;
  top: -14px;
  left: -15px;
}
.box2 {
  background-color: black;
  top: -30px;
  left: 30px;
}
.box3 {
  background-color: black;
  top: 30px;
  left: -30px;
}
.box4 {
  background-color: red;
  border-radius: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
  <div class="box box1"></div>
  <div class="box box2"></div>
  <div class="box box3"></div>
  <div class="box box4">
    <h3>Content</h3>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)