CSS 3 形状:“反圆”或“切出圆”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10501488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 03:48:14  来源:igfitidea点击:

CSS 3 Shape: "Inverse Circle" or "Cut Out Circle"

csscss-shapes

提问by Alp

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

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

CSS Shape

CSS 形状

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

图像在某种程度上不准确,因为黑线应该沿着 div 元素的外边框继续。

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

这是我目前所拥有的演示:http: //jsfiddle.net/n9fTF/

Is that even possible with CSSwithout images?

CSS没有图像甚至可能吗?

回答by ScottS

Update: CSS3 Radial Background Gradient Option

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

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

(对于那些支持它的浏览器——在 FF 和 Chrome 中测试——IE10,Safari 也应该可以工作)。

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

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):

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

HTML

HTML

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

CSS

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;
} 

回答by Chris Fletcher

I can't really tell from your drawing how rounded you want the points, but here's one possibility: http://jsfiddle.net/n9fTF/6/

我真的无法从你的画中看出你想要分数有多圆,但这是一种可能性:http: //jsfiddle.net/n9fTF/6/

If the points need to be more rounded, you'll need to put some circles on the ends so they blend with the big scoop.

如果点需要更圆,你需要在末端放一些圆圈,以便它们与大勺混合。

回答by web-tiki

Different approach : Box-shadows

不同的方法:盒阴影

This approach uses CSS box shadows which are supported by IE9+(canIuse)

这种方法使用了 IE9+( canIuse)支持的CSS 框阴影

DEMO

演示

Output :

输出 :

CSS shape with inset curve using box-shadows

使用 box-shadows 插入曲线的 CSS 形状

HTML :

HTML :

<div id="a">
    <div id="b"></div>
</div>

CSS :

CSS :

#a{
    overflow:hidden;
    border-radius:20px;
    position:relative;
    display:inline-block;
}
#a:before, #a:after{
    content:'';
    width: 100px;
    border-radius: 50%;
}
#a:before {
    height: 100px;
    float:left;    
    border: 1px solid black;
    background: grey;
}
#a:after {
    position:absolute;
    left:14px; top:-6px;
    height:114px;
    box-shadow: 1px 0px 0px 0px #000, 110px 0px 0px 68px #808080;
    background:none;
    z-index:-1;
}
#b {
    width: 200px;
    height: 100px;
    background:none;
    margin-left:-15px;
    border: 1px solid black;
    border-left:none;
    float:left;
    border-top-right-radius: 20px;
    border-bottom-right-radius: 20px;
}

回答by Jon Mifsud

This is a very interesting question. I've recently posted a tutorial on how to make Inverse Border Radius in CSS (here)and I think this could easily be adapted for your case.

这是一个非常有趣的问题。我最近发布了一个关于如何在 CSS 中制作反向边框半径的教程(这里),我认为这可以很容易地适应您的情况。

The trick is to create a span that generates the inverse border using a very simple concept - very thick borders. And use the inside section by hiding them. What you would have to do in addition to my script provided is add another border-radius to the top-left corner as I am only using the top-right one. Make the span aligned to the left of the item you want by absolute positioning, and increase the height/width of span accordingly and voila you have your inverse border-radius.

诀窍是使用一个非常简单的概念创建一个生成反向边界的跨度 - 非常粗的边界。并通过隐藏它们来使用内部部分。除了我提供的脚本之外,您还需要在左上角添加另一个边框半径,因为我只使用右上角的边框。通过绝对定位使跨度与您想要的项目的左侧对齐,并相应地增加跨度的高度/宽度,瞧你有你的反向边界半径

回答by karim79

Introduce an absolutely positioned borderless white circle which sits behind the gray circle at an offset. You will need to set the z-index of the dark circle to ensure that it sits above the white circle:

引入一个绝对定位的无边界白色圆圈,它位于灰色圆圈后面的偏移处。您需要设置黑圈的 z-index 以确保它位于白圈上方:

#c {
    position: absolute;
    border: 0;
    left: 30px;
    width: 100px;
    height: 100px;
    border-radius: 50px;
    background: white;
}

Demo.?

演示。?

回答by Rick Donohoe

Someone else done it somewhere from what I found...

其他人从我发现的某处完成了...

JSFiddle: http://jsfiddle.net/ajeN7/

JSFiddle:http: //jsfiddle.net/ajeN7/

and the question: CSS3 Inverted Rounded Corner

和问题:CSS3 倒圆角

Hopefully that helps!

希望这有帮助!