[英]CSS3 - special border-radius
我正在尝试使用CSS3制作独特的形状。 我正在使用Bootstrap 3网格系统进行元素的布局。 我已经充分利用了这种形状,但是我无法使border-radius
属性在element内部变圆。
我已经做了什么:
CSS:
.test-midbox{
height: 170px;
background-color: white ;
padding-left: 0px;
padding-right: 0px;
margin-right: 0;
margin-left: 0;
border: solid black 1px;
top: 20px;
}
.test-inbox{
margin-right: 0;
margin-left: 0;
background-image: url("../images/linux.jpg") !important;
background-repeat: no-repeat;
background-position: center;
background-size: auto 80%;
height: 170px;
padding-top:10px;
padding-left:10px;
padding-right:10px;
padding-bottom:40px;
}
.monitor-name:before{
content: "";
position: absolute;
left: 0;
right: 0;
z-index: -2;
display: block;
height: 100%;
background: black;
opacity: 0.7;
border-bottom-right-radius: 20px;
border-top-right-radius: 20px;
}
.monitor-name{
z-index: 100;
position: absolute;
height: 40px;
bottom: 20px;
text-align: center;
color: white;
border:black 1px solid;
border-left: none;
border-bottom-right-radius: 20px;
border-top-right-radius: 20px;
}
.monitor-name-text{
top:1px;
text-align: center;
}
.test-puls{
position: absolute;
height: 20px;
padding-left: 0px;
padding-right: 0px;
margin-right: 0;
margin-left: 0;
border-top: 1px black solid;
bottom: 15px;
}
.btn-plus{
border-radius: 0;
}
.btn-cir{
border: 1px solid black;
position: absolute;
height: 40px;
bottom: 20px;
padding-left: 0px;
padding-right: 0px;
}
HTML:
<div class="col-lg-3 test-outerbox">
<div class="test-midbox col-lg-12">
<div class="col-lg-12 col-lg-offset-1 test-inbox">
</div>
<div class="col-lg-12 col-lg-offset-1 test-puls">
</div>
<div class="monitor-name col-lg-10 col-lg-push-2">
<h4 class="monitor-name-text">tsdsds</h4>
</div>
<div class="col-lg-2 btn-cir">k</div>
</div>
</div>
border-radius
将始终仅产生向外弯曲,而永远不会向内弯曲。 因此,仅凭它就不能产生所需的效果。 以下是几种产生所需效果的方法。 您可以选择最适合您的情况的一种。
我没有使用基于Twitter Bootstrap的标记,但是转换起来应该很简单。
使用z-index:
如果所有项目的背景都不透明(即没有alpha值)且否。 元素的个数是一个有限的固定数,然后使用z-index
将元素放置在另一个元素的顶部,以使第一个元素在第二个元素的顶部,然后依次在第三个元素的顶部,依此类推。 还为所有元素设置了负的margin-right
,以产生重叠效果。 由于背景是不透明的,因此它们在第一个元素上的边框看起来像第二个元素具有向内弯曲的边框,依此类推。
注意:根据提供的图像,这似乎不适合您的情况,但为了完整性起见添加了该图像。
.items { position: relative; float: left; height: 50px; width: 200px; margin-right: -60px; line-height: 50px; text-align: center; border: 1px solid brown; } .elongated-border-curve .items { border-radius: 0% 35% 35% 0% / 0% 50% 50% 0%; } .shorter-border-curve .items { border-radius: 0% 20% 20% 0% / 0% 50% 50% 0%; } .items:nth-of-type(n+2) { border-left: 0; background: wheat; } .items:first-of-type { background: sandybrown; color: white; z-index: 3; } .items:nth-of-type(2) { z-index: 2; /* lower z-index than first */ } .items:nth-of-type(3) { z-index: 1; /* lower z-index than second */ } .items:last-of-type { /* default z-index 0 */ border-radius: 0%; } /* just for demo */ h3 { clear: both; } .elongated-border-curve, .shorter-border-curve { padding-bottom: 50px; } body { background: radial-gradient(circle at 50% 50%, aliceblue, mediumslateblue); min-height: 500px; }
<h3>With Elongated Border Curves</h3> <div class='elongated-border-curve'> <div class='items'>Item 1</div> <!-- give each item a z-index lower than the previous one --> <div class='items'>Item 2</div> <!-- give each item a z-index lower than the previous one --> <div class='items'>Item 3</div> <!-- give each item a z-index lower than the previous one --> <div class='items'>Item 4</div> <!-- give each item a z-index lower than the previous one --> </div> <h3>With Shorter Border Curves</h3> <div class='shorter-border-curve'> <div class='items'>Item 1</div> <!-- give each item a z-index lower than the previous one --> <div class='items'>Item 2</div> <!-- give each item a z-index lower than the previous one --> <div class='items'>Item 3</div> <!-- give each item a z-index lower than the previous one --> <div class='items'>Item 4</div> <!-- give each item a z-index lower than the previous one --> </div>
具有动态宽度的样本:
.items { position: relative; float: left; height: 50px; width: 32%; margin-right: -10%; line-height: 50px; text-align: center; border: 1px solid brown; } .elongated-border-curve .items { border-radius: 0% 35% 35% 0% / 0% 50% 50% 0%; } .shorter-border-curve .items { border-radius: 0% 20% 20% 0% / 0% 50% 50% 0%; } .items:nth-of-type(n+2) { border-left: 0; background: wheat; } .items:first-of-type { background: sandybrown; color: white; z-index: 3; } .items:nth-of-type(2) { z-index: 2; /* lower z-index than first */ } .items:nth-of-type(3) { z-index: 1; /* lower z-index than second */ } .items:last-of-type { /* default z-index 0 */ border-radius: 0%; } /* just for demo */ h3 { clear: both; } .elongated-border-curve, .shorter-border-curve { padding-bottom: 50px; } body { background: radial-gradient(circle at 50% 50%, aliceblue, mediumslateblue); min-height: 500px; }
<h3>With Elongated Border Curves</h3> <div class='elongated-border-curve'> <div class='items'>Item 1</div> <!-- give each item a z-index lower than the previous one --> <div class='items'>Item 2</div> <!-- give each item a z-index lower than the previous one --> <div class='items'>Item 3</div> <!-- give each item a z-index lower than the previous one --> <div class='items'>Item 4</div> <!-- give each item a z-index lower than the previous one --> </div> <h3>With Shorter Border Curves</h3> <div class='shorter-border-curve'> <div class='items'>Item 1</div> <!-- give each item a z-index lower than the previous one --> <div class='items'>Item 2</div> <!-- give each item a z-index lower than the previous one --> <div class='items'>Item 3</div> <!-- give each item a z-index lower than the previous one --> <div class='items'>Item 4</div> <!-- give each item a z-index lower than the previous one --> </div>
带框阴影:
如果所有项目均具有纯色背景(不透明或透明),但否。 元素的数量不是有限的,或者不是很大(因此很难采用以前的方法),请在伪元素上使用具有较大散布半径的box-shadow
,如下面的代码片段所示。
当元素具有渐变或图像作为背景时,此方法将不起作用。
.items { float: left; height: 50px; width: 200px; margin-right: -60px; line-height: 50px; text-align: center; border: 1px solid brown; } .elongated-border-curve .items { border-radius: 0% 35% 35% 0% / 0% 50% 50% 0%; } .shorter-border-curve .items { border-radius: 0% 20% 20% 0% / 0% 50% 50% 0%; } .items:first-of-type { z-index: 3; } .items:nth-of-type(n+2) { position: relative; border-left: 0; overflow: hidden; } .items:nth-of-type(n+2):after { position: absolute; content: ''; height: 100%; width: 100%; top: 0px; left: -140px; /* -(parent's margin-right) - width of element */ z-index: -1; } .elongated-border-curve .items:nth-of-type(n+2):after { border-radius: 0% 35% 35% 0% / 0% 50% 50% 0%; } .shorter-border-curve .items:nth-of-type(n+2):after { border-radius: 0% 20% 20% 0% / 0% 50% 50% 0%; } .items:last-of-type { border-radius: 0%; } .opaque-bg .items:first-of-type { background: sandybrown; color: white; } .semi-transparent-bg .items:first-of-type { background: rgba(0, 0, 0, 0.5); color: white; } .opaque-bg .items:nth-of-type(n+2):after { box-shadow: 0px 0px 0px 200px wheat; } .semi-transparent-bg .items:nth-of-type(n+2):after { box-shadow: 0px 0px 0px 200px rgba(127, 127, 127, 0.5); } /* just for demo */ h3 { clear: both; } .opaque-bg, .semi-transparent-bg { padding-bottom: 50px; } body { background: radial-gradient(circle at 50% 50%, aliceblue, mediumslateblue); min-height: 500px; }
<h3>Items have a solid opaque background + Elongated Border Curves</h3> <div class='opaque-bg elongated-border-curve'> <div class='items'>Item 1</div> <div class='items'>Item 2</div> <div class='items'>Item 3</div> <div class='items'>Item 4</div> </div> <h3>Items have a solid semi-transparent background + Elongated Border Curves</h3> <div class='semi-transparent-bg elongated-border-curve'> <div class='items'>Item 1</div> <div class='items'>Item 2</div> <div class='items'>Item 3</div> <div class='items'>Item 4</div> </div> <h3>Items have a solid opaque background + Shorter Border Curves</h3> <div class='opaque-bg shorter-border-curve'> <div class='items'>Item 1</div> <div class='items'>Item 2</div> <div class='items'>Item 3</div> <div class='items'>Item 4</div> </div> <h3>Items have a solid semi-transparent background + Shorter Border Curves</h3> <div class='semi-transparent-bg shorter-border-curve'> <div class='items'>Item 1</div> <div class='items'>Item 2</div> <div class='items'>Item 3</div> <div class='items'>Item 4</div> </div>
具有动态宽度的样本:
.items { float: left; height: 50px; width: 32%; margin-right: -10%; line-height: 50px; text-align: center; border: 1px solid brown; } .elongated-border-curve .items { border-radius: 0% 35% 35% 0% / 0% 50% 50% 0%; } .shorter-border-curve .items { border-radius: 0% 20% 20% 0% / 0% 50% 50% 0%; } .items:first-of-type { z-index: 3; } .items:nth-of-type(n+2) { position: relative; border-left: 0; overflow: hidden; } .items:nth-of-type(n+2):after { position: absolute; content: ''; height: 100%; width: 100%; top: 0px; left: -69%; /* -(parent's margin-right) - width of element */ z-index: -1; } .elongated-border-curve .items:nth-of-type(n+2):after { border-radius: 0% 35% 35% 0% / 0% 50% 50% 0%; } .shorter-border-curve .items:nth-of-type(n+2):after { border-radius: 0% 20% 20% 0% / 0% 50% 50% 0%; } .items:last-of-type { border-radius: 0%; } .opaque-bg .items:first-of-type { background: sandybrown; color: white; } .semi-transparent-bg .items:first-of-type { background: rgba(0, 0, 0, 0.5); color: white; } .opaque-bg .items:nth-of-type(n+2):after { box-shadow: 0px 0px 0px 999px wheat; } .semi-transparent-bg .items:nth-of-type(n+2):after { box-shadow: 0px 0px 0px 999px rgba(127, 127, 127, 0.5); } /* just for demo */ h3 { clear: both; } .opaque-bg, .semi-transparent-bg { padding-bottom: 50px; } body { background: radial-gradient(circle at 50% 50%, aliceblue, mediumslateblue); min-height: 500px; }
<h3>Items have a solid opaque background + Elongated Border Curves</h3> <div class='opaque-bg elongated-border-curve'> <div class='items'>Item 1</div> <div class='items'>Item 2</div> <div class='items'>Item 3</div> <div class='items'>Item 4</div> </div> <h3>Items have a solid semi-transparent background + Elongated Border Curves</h3> <div class='semi-transparent-bg elongated-border-curve'> <div class='items'>Item 1</div> <div class='items'>Item 2</div> <div class='items'>Item 3</div> <div class='items'>Item 4</div> </div> <h3>Items have a solid opaque background + Shorter Border Curves</h3> <div class='opaque-bg shorter-border-curve'> <div class='items'>Item 1</div> <div class='items'>Item 2</div> <div class='items'>Item 3</div> <div class='items'>Item 4</div> </div> <h3>Items have a solid semi-transparent background + Shorter Border Curves</h3> <div class='semi-transparent-bg shorter-border-curve'> <div class='items'>Item 1</div> <div class='items'>Item 2</div> <div class='items'>Item 3</div> <div class='items'>Item 4</div> </div>
我认为box-shadow
方法应该适合您的情况。 如果没有,那么唯一的选择就是使用SVG 。 clip-path
可以在不使用SVG的情况下帮助实现这一目标,但是目前它对浏览器的支持非常差。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.