【问题标题】:How can I make my border-radius curve outward?如何使我的边界半径向外弯曲?
【发布时间】:2015-05-09 13:53:01
【问题描述】:

我试图让一个 div 有如下图的边框:

这是我尝试过的:

div {
    height: 100px;
    width: 100px;
    border-bottom-right-radius:100px 10px;
    border:1px solid #000;
}
<div></div>

实现这一点的有效方式是什么?

【问题讨论】:

  • 这将需要一些额外的标记,而不仅仅是边界半径。

标签: css css-shapes


【解决方案1】:

使用:before and :after

顶部边框是用:before创建的:

  • 它的高度与边框半径相同

  • 它位于top 的外侧,并且由于left 而与左边框对齐

  • 它的宽度是用calc计算的,以精确对齐曲线的顶部

  • 曲线可以用transform: skewX(-60deg)细化

左边框是用:after创建的:

  • 给定一个 100% 的高度减去之前的高度和带有calc 的边框的厚度

示例

数字 1 - 有点尖

div {
  border-bottom-right-radius: 100px 20px;
  border: 1px solid #000;
  border-top: none;
  height: 500px;
  width: 200px;
  position: relative;
  border-left: none;
}
div:before,
div:after {
  content: '';
  display: block;
  position: absolute;
  left: -1px;
}
div:before {
  height: 20px;
  width: 100%;
  width: calc(100% + 1px);
  border-bottom-right-radius: 100px 20px;
  border-bottom: 1px solid #000;
  border-right: solid 1px #000;
  top: -1px;
}
div:after {
  height: calc(100% - 18px);
  border-left: 1px solid #000;
  top: 19px;
}
<div></div>

数字 2 - 倾斜的平滑点

div {
  border-bottom-right-radius: 100px 20px;
  border: 1px solid #000;
  border-top: none;
  height: 200px;
  width: 200px;
  position: relative;
  border-left: none;
}
div:before,
div:after {
  content: '';
  display: block;
  position: absolute;
  left: -1px;
}
div:before {
  height: 20px;
  width: 100%;
  width: calc(100% - 36px);
  border-bottom-right-radius: 100px 20px;
  border-bottom: 1px solid #000;
  border-right: solid 2px #000;
  top: 0px;
  left: 17px;
  transform: skewX(-60deg);
}
div:after {
  height: calc(100% - 19px);
  border-left: 1px solid #000;
  top: 20px;
}
<div></div>

【讨论】:

  • 谢谢,太好了,但是为什么顶部边框看起来比图片更锐利和指向。
  • @user3741635 - 这是由于在 CSS 中绘制边框的方式。查看我刚刚添加到答案中的示例编号 2 - 可以使用 transform: skewX 来解决这个问题
  • 再问一个问题,例如,如何将绘图的颜色从透明更改为红色。
【解决方案2】:

我可以使用 DIV 来做到这一点,但我很确定存在一种更优雅的方式来做到这一点:

    #container {
       border:none;   
       height:100px;
       border-right: solid 1px #000;
    }
    
    #square_top {
       border-bottom-right-radius:100px 10px;
       border:none;   
       border-bottom: solid 1px #000;
       height:10px;
    }
    
    #square_bottom {
       border-bottom-right-radius:100px 10px;
       border:none;   
       border-bottom: solid 1px #000;
       border-right:solid 1px #000;
       border-left:solid 1px #000;
       height:10px;
    }
    
    #square {
       height: 90px;
       border-left:solid 1px #000;
    }
    <div id="container">
       <div id="square_top"></div>
       <div id="square">TEXT HERE</div>
    </div>   
    <div id="square_bottom"></div>

【讨论】:

    【解决方案3】:

    虽然 CSS 可以做到这一点,但还有另一种方法可以提供更大的灵活性:SVG

    这种方法允许:

    • 形状会根据内容的大小调整其大小
    • 响应式
    • 允许任何类型的背景(图像、渐变、半透明颜色...)
    • 允许对形状进行任何类型的填充(图像、渐变、半透明颜色...)
    • 更容易控制顶部和底部曲线:

    body {background: url('http://lorempixel.com/output/people-q-g-640-480-9.jpg');background-size: cover;}
    div {
      position: relative;
      width: 30%;
      padding: 5%;
      color: #fff;
      text-align: center;
    }
    svg {
      position: absolute;
      top: 0; left: 0;
      width: 100%; height: 100%;
      z-index: -1;
    }
    <div>
      <p>Some text</p>
      <p>Some text</p>
      <p>Some text</p>
      <p>Some text</p>
      <p>Some text</p>
      <p>Some text</p>
      <p>Some text</p>
      <p>Some text</p>
      <p>Some text</p>
      <svg viewbox="0 0 50 100" preserveAspectRatio="none">
        <path d="M1 9 C49 10 49 1 49 1 V90 C49 91 49 99 1 99z"  stroke-width="0.5" stroke="#000" fill-opacity="0.5" />
      </svg>
    </div>

    【讨论】:

    • 我要添加这个,直到我看到。人们也需要使用 SVG
    • 因为没有保留纵横比,所以形状发生了变化。这很好,但它会导致左右笔画的笔画宽度发生变化。有没有办法克服tat?
    • @TimKrul 在你问之前我并没有意识到这一点,但有一个 vector-effect="non-scaling-stroke" 属性允许保持笔画的宽度固定。但是IE好像不支持
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-28
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    相关资源
    最近更新 更多