CSS 3形状:“逆圆”或“剪切圆” [英] CSS 3 Shape: "Inverse Circle" or "Cut Out Circle"

查看:145
本文介绍了CSS 3形状:“逆圆”或“剪切圆”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个形状,我将其描述为反转圆:





该图像有点不准确,因为黑线应该沿着div元素的外边界继续。 / p>

这里是我目前的演示: http: //jsfiddle.net/n9fTF/



没有图片, CSS / p>

解决方案

更新:CSS3径向背景渐变选项



支持它 - 在FF和Chrome - IE10中测试,Safari也应该工作。)



我的原始答案的一个问题是那些没有他们正在反对的坚实背景。此更新创建相同的效果,允许圆形和它的反切割之间的透明间隙。



查看示例小提琴



CSS

  .inversePair {
border:1px solid black;
display:inline-block;
position:relative;
height:100px;
text-align:center;
line-height:100px;
vertical-align:middle;
}

#a {
width:100px;
border-radius:50px;
background:gray;
z-index:1;
}

#b {
width:200px;
/ *需要使用margin / padding调整
基于你想要的gap* /
padding-left:30px;
margin-left:-30px;
/ *真正的边界* /
border-left:none;
-webkit-border-top-right-radius:20px;
-webkit-border-bottom-right-radius:20px;
-moz-border-radius-topright:20px;
-moz-border-radius-bottomright:20px;
border-top-right-radius:20px;
border-bottom-right-radius:20px;
/ *反圆cut* /
background-image:-moz-radial-gradient(
-23px 50%,/ * -23px左位置因 * /
圆最近角,/ *保持半径到一半高度* /
透明0,/ *透明中心* /
透明55px,/ * /
black 56px,/ *开始圈border* /
grey 57px / *结束圆圈边框和背景其余部分的开始颜色* /
);
background-image:-webkit-radial-gradient(-23px 50%,circle closest-corner,rgba(0,0,0,0)0,rgba(0,0,0,0)55px,black 56px,gray 57px);
background-image:-ms-radial-gradient(-23px 50%,circle closest-corner,rgba(0,0,0,0)0,rgba(0,0,0,0)55px,black 56px,gray 57px);
background-image:-o-radial-gradient(-23px 50%,circle closest-corner,rgba(0,0,0,0)0,rgba(0,0,0,0)55px,black 56px,gray 57px);
background-image:radial-gradient(-23px 50%,circle closest-corner,rgba(0,0,0,0)0,rgba(0,0,0,0)55px,black 56px,gray 57px);
}



原始回答



比我预期的z-indexing工作更多的努力(这似乎忽略负z-index ),但是, 这给了一个干净的外观 (测试在IE9,FF,Chrome):



HTML

 code>< div id =aclass =inversePair> A< / div> 
< div id =bclass =inversePair> B< / div>

CSS



< pre class =lang-css prettyprint-override> .inversePair {
border:1px solid black;
background:gray;
display:inline-block;
position:relative;
height:100px;
text-align:center;
line-height:100px;
vertical-align:middle;
}

#a {
width:100px;
border-radius:50px;
}

#a:before {
content:'';
left:-6px;
top:-6px;
position:absolute;
z-index:-1;
width:112px; / * 5px gap * /
height:112px;
border-radius:56px;
background-color:white;
}

#b {
width:200px;
z-index:-2;
padding-left:50px;
margin-left:-55px;
overflow:hidden;
-webkit-border-top-right-radius:20px;
-webkit-border-bottom-right-radius:20px;
-moz-border-radius-topright:20px;
-moz-border-radius-bottomright:20px;
border-top-right-radius:20px;
border-bottom-right-radius:20px;
}

#b:before {
content:'';
left:-58px;
top:-7px;
position:absolute;
width:114px; / * 5px gap,1px border * /
height:114px;
border-radius:57px;
background-color:black;
}


I want to create a shape, which i would describe as "inverse circle":

The image is somehow inaccurate, because the black line should continue along the outer border of the div element.

Here is a demo of what i have at the moment: http://jsfiddle.net/n9fTF/

Is that even possible with CSS without images?

解决方案

Update: CSS3 Radial Background Gradient Option

(For those browsers supporting it--tested in FF and Chrome--IE10, Safari should work too).

One "problem" with my original answer is those situations where one does not have a solid background that they are working against. This update creates the same effect allowing for a transparent "gap" between the circle and it's inverse cutout.

See example fiddle.

CSS

.inversePair {
    border: 1px solid black;
    display: inline-block;    
    position: relative;    
    height: 100px;
    text-align: center;
    line-height: 100px;
    vertical-align: middle;
}

#a {
    width: 100px;
    border-radius: 50px;
    background: grey;
    z-index: 1;
}

#b {
    width: 200px;
    /* need to play with margin/padding adjustment
       based on your desired "gap" */
    padding-left: 30px;
    margin-left: -30px;
    /* real borders */
    border-left: none;
    -webkit-border-top-right-radius: 20px;
    -webkit-border-bottom-right-radius: 20px;
    -moz-border-radius-topright: 20px;
    -moz-border-radius-bottomright: 20px;
    border-top-right-radius: 20px;
    border-bottom-right-radius: 20px;
    /* the inverse circle "cut" */
    background-image: -moz-radial-gradient(
        -23px 50%, /* the -23px left position varies by your "gap" */
        circle closest-corner, /* keep radius to half height */
        transparent 0, /* transparent at center */
        transparent 55px, /*transparent at edge of gap */
        black 56px, /* start circle "border" */
        grey 57px /* end circle border and begin color of rest of background */
    );
    background-image: -webkit-radial-gradient(-23px 50%, circle closest-corner, rgba(0, 0, 0, 0) 0, rgba(0, 0, 0, 0) 55px, black 56px, grey 57px);
    background-image: -ms-radial-gradient(-23px 50%, circle closest-corner, rgba(0, 0, 0, 0) 0, rgba(0, 0, 0, 0) 55px, black 56px, grey 57px);
    background-image: -o-radial-gradient(-23px 50%, circle closest-corner, rgba(0, 0, 0, 0) 0, rgba(0, 0, 0, 0) 55px, black 56px, grey 57px);
    background-image: radial-gradient(-23px 50%, circle closest-corner, rgba(0, 0, 0, 0) 0, rgba(0, 0, 0, 0) 55px, black 56px, grey 57px);
}

Original Answer

Took more effort than I expected to get the z-indexing to work (this seems to ignore the negative z-index), however, this gives a nice clean look (tested in IE9, FF, Chrome):

HTML

<div id="a" class="inversePair">A</div>
<div id="b" class="inversePair">B</div>

CSS

.inversePair {
    border: 1px solid black;
    background: grey;
    display: inline-block;    
    position: relative;    
    height: 100px;
    text-align: center;
    line-height: 100px;
    vertical-align: middle;
}

#a {
    width: 100px;
    border-radius: 50px;
}

#a:before {
    content:' ';
    left: -6px;
    top: -6px;
    position: absolute;
    z-index: -1;
    width: 112px; /* 5px gap */
    height: 112px;
    border-radius: 56px;
    background-color: white;
} 

#b {
    width: 200px;
    z-index: -2;
    padding-left: 50px;
    margin-left: -55px;
    overflow: hidden;
    -webkit-border-top-right-radius: 20px;
    -webkit-border-bottom-right-radius: 20px;
    -moz-border-radius-topright: 20px;
    -moz-border-radius-bottomright: 20px;
    border-top-right-radius: 20px;
    border-bottom-right-radius: 20px;
}

#b:before {
    content:' ';
    left: -58px;
    top: -7px;
    position: absolute;
    width: 114px; /* 5px gap, 1px border */
    height: 114px;
    border-radius: 57px;
    background-color: black;
} 

这篇关于CSS 3形状:“逆圆”或“剪切圆”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆