【问题标题】:Add rounded borders to selected corners of an element为元素的选定角添加圆角边框
【发布时间】:2016-10-13 06:44:52
【问题描述】:

我怎样才能用纯 CSS 构建这样的东西?

这是我到目前为止的进展:Fiddle

即使我继续添加额外的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;
}
<div class="container">
  <span class="top-right"></span>
  <div class="box">
    <h3>Content</h3>
  </div>
</div>

【问题讨论】:

    标签: html css css-shapes rounded-corners


    【解决方案1】:

    您可以通过使用.box 中的伪元素::before/::after 使用属性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;
    }
    <div class="container">
      <div class="box">
        <h3>Content</h3>
      </div>
    </div>

    【讨论】:

      【解决方案2】:

      使用pseudo-elements would be the ideal solution

      这个答案只是一个选择。虽然在语义上并不优雅,但它的效果却很粗糙。

      • 创建一个包含四个 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>
      

      使用绝对定位,.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;
      }
      <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>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-13
        • 1970-01-01
        • 1970-01-01
        • 2017-03-09
        • 1970-01-01
        • 2019-10-31
        • 2022-10-16
        • 1970-01-01
        相关资源
        最近更新 更多