【问题标题】:CSS 3 Shape: "Inverse Circle" or "Cut Out Circle"CSS 3 形状:“反圆”或“切出圆”
【发布时间】:2012-05-17 02:33:17
【问题描述】:

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

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

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

CSS 没有图片也可以吗?

【问题讨论】:

  • 我唯一的想法是使用 box-shadow 来模拟曲线,但由于 div b 的边框不遵循轮廓,因此它非常不完美。 jsfiddle.net/n9fTF/4

标签: css css-shapes


【解决方案1】:

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

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

我最初的答案的一个“问题”是那些没有坚实背景的情况。此更新创建了相同的效果,允许圆之间的透明“间隙”和它的反向切口。

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

原答案

为了让 z-indexing 工作 (this seems to ignore the negative z-index),付出了比我预期更多的努力,但是,this gives a nice clean look(在 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;
} 

【讨论】:

  • 这很棒,我喜欢这个解决方案
  • @Alp——太棒了!很高兴获得批准。
  • @Alp--我添加了一个更新,允许在支持径向渐变背景的浏览器上实现圆形和反向之间的透明间隙。
  • 太棒了,这是真正的 css 大师。几乎是魔法:)
  • 太棒了 - 多年来一直在寻找一种方法来从 div 中切出一个小圆圈,这正是我需要的技巧
【解决方案2】:

我无法从您的绘图中真正看出您希望这些点有多圆,但这里有一种可能性: http://jsfiddle.net/n9fTF/6/

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

【讨论】:

  • 迄今为止最好的解决方案,谢谢。然而,两个 div 元素之间的“空白间隙”应该在任何地方都具有完全相同的宽度,这里不是这种情况
【解决方案3】:

不同的方法:盒子阴影

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

DEMO

输出:

HTML:

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

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

【讨论】:

    【解决方案4】:

    这是一个非常有趣的问题。我最近发布了一个关于如何制作Inverse Border Radius in CSS (here) 的教程,我认为这可以很容易地适应您的情况。

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

    【讨论】:

      【解决方案5】:

      其他人在我发现的某处做了它......

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

      还有问题:CSS3 Inverted Rounded Corner

      希望对您有所帮助!

      【讨论】:

      • @alp 只需从border-radius 中删除-moz- 就可以了。不过,这并不是完全正确的效果。
      【解决方案6】:

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

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

      Demo.

      【讨论】:

      • 这不是最佳的,因为缺少黑色边框
      猜你喜欢
      • 1970-01-01
      • 2013-09-20
      • 1970-01-01
      • 2013-04-11
      • 1970-01-01
      • 1970-01-01
      • 2013-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多