我怎样才能让我的 border-radius 曲线向外?
How can I make my border-radius curve outward?
我正在尝试制作 div 具有如下图所示的边框:
这是我试过的:
div {
height: 100px;
width: 100px;
border-bottom-right-radius:100px 10px;
border:1px solid #000;
}
<div></div>
实现这个效果的方法是什么?
我可以使用 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>
使用:before
and :after
上边框使用 :before
:
创建
其高度与边框半径相同
由于left
,它位于 top
的外侧并与左边框对齐
它的宽度是用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>
虽然 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>
我正在尝试制作 div 具有如下图所示的边框:
这是我试过的:
div {
height: 100px;
width: 100px;
border-bottom-right-radius:100px 10px;
border:1px solid #000;
}
<div></div>
实现这个效果的方法是什么?
我可以使用 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>
使用:before
and :after
上边框使用 :before
:
创建
其高度与边框半径相同
由于
,它位于left
top
的外侧并与左边框对齐它的宽度是用
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>
虽然 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>