透明的半圆切出一个div

use*_*974 15 css html5 css3 css-shapes

我想只使用CSS3制作一个透明切出的半圆形状.唯一的要求是形成形状的所有元素必须是黑色或透明的.

不能在它上面使用带有白色圆圈的黑色矩形,因为半圆必须是透明的,让背景显示出来.

所需形状:

矩形切出半圈

san*_*eep 20

也许可以用这样的CSS :after伪属性来做:

body {
  background: green;
}

.rect {
  height: 100px;
  width: 100px;
  background: rgba(0, 0, 0, 0.5);
  position: relative;
  margin-top: 100px;
  margin-left: 100px;
}

.circle {
  display: block;
  width: 100px;
  height: 50px;
  top: -50px;
  left: 0;
  overflow: hidden;
  position: absolute;
}

.circle:after {
  content: '';
  width: 100px;
  height: 100px;
  -moz-border-radius: 100px;
  -webkit-border-radius: 100px;
  border-radius: 100px;
  background: rgba(0, 0, 0, 0);
  position: absolute;
  top: -100px;
  left: -40px;
  border: 40px solid rgba(0, 0, 0, 0.5);
}
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/FcaVX/2/


web*_*iki 10

您可以使用框阴影来制作透明切割圆:

body {
  background: url(http://i.imgur.com/qi5FGET.jpg) no-repeat;
  background-size: cover;
}
div {
  display: inline-block;
  width: 300px; height: 300px;
  position: relative;
  overflow: hidden;
}
div:before {
  content: '';
  position: absolute;
  bottom: 50%;
  width: 100%; height: 100%;
  border-radius: 100%;
  box-shadow: 0px 300px 0px 300px #000;
}
.transparent {
  opacity: 0.5;
}
Run Code Online (Sandbox Code Playgroud)
<div></div>
<div class="transparent"></div>
Run Code Online (Sandbox Code Playgroud)

透明切出半圈

这可以响应百分比长度:

body {
  background: url(http://lorempixel.com/output/people-q-c-640-480-1.jpg) no-repeat;
  background-size: cover;
}
div {
  width: 40%; height: 300px;
  position: relative;
  overflow: hidden;
}
div:before {
  content: '';
  position: absolute;
  bottom: 50%;
  width: 100%; height: 100%;
  border-radius: 100%;
  box-shadow: 0px 300px 0px 300px #000;
}
.transparent {
  opacity: 0.5;
}
Run Code Online (Sandbox Code Playgroud)
<div class="transparent"></div>
Run Code Online (Sandbox Code Playgroud)


Har*_*rry 10

使用SVG:

这是使用SVG的替代解决方案(尽管您尚未对其进行标记).使用SVG的优点是:

  • 与径向渐变相比,它具有更好的浏览器支持.
  • 与盒阴影方法不同,SVG可以支持形状内的图像.

虽然<= IE8不支持SVG,而box-shadow不支持SVG,但可以提供回退.

svg {
  height: 150px;
  width: 150px;
}
polygon {
  fill: black;
}

/* Just for demo */

body {
  background-image: radial-gradient(circle, #3F9CBA 0%, #153346 100%);
}
Run Code Online (Sandbox Code Playgroud)
<!-- Sample 1 - Using Clip Path -->
<svg viewBox='0 0 100 100' preserveAspectRatio='none'>
  <defs>
    <clipPath id='clipper'>
      <path d='M0,0 a50,50 0 1,0 100,0 l 0,100 -100,0' />
    </clipPath>
  </defs>
  <polygon points='0,0 100,0 100,100 0,100' clip-path='url(#clipper)' />
</svg>

<!-- Sample 2 - Using Path -->
<svg viewBox='0 0 100 100' preserveAspectRatio='none'>
  <pattern id='bg' width='100' height='100' patternUnits='userSpaceOnUse'>
    <image xlink:href='http://lorempixel.com/100/100/nature/1' height='100' width='100' />
  </pattern>
  <path d='M0,0 a50,50 0 1,0 100,0 l 0,100 -100,0 0,-100' fill='url(#bg)' />
</svg>
Run Code Online (Sandbox Code Playgroud)


使用CSS:

CSS也有clip-path规格,我们可以尝试类似下面的代码片段.

.shape {
  position: relative;
  width: 100px;
  height: 100px;
  background-color: purple;
}
.shape:after {
  position: absolute;
  content: '';
  top: 0px;
  left: 0px;
  height: 100%;
  width: 100%;
  background: white;
  -webkit-clip-path: ellipse(50% 20% at 50% 0%);
  clip-path: ellipse(50% 20% at 50% 5%);
}

.shape.image{
  background: url(http://lorempixel.com/100/100);
}

#shape-2 {
  width: 100px;
  height: 100px;
  background-color: purple;
  -webkit-clip-path: ellipse(50% 20% at 50% 20%);
  clip-path: ellipse(50% 20% at 50% 20%);
}

/* Just for demo */

.shape{
  float: left;
  margin: 20px;
}
#shape-2 {
  margin: 150px 20px 0px;
}
Run Code Online (Sandbox Code Playgroud)
<div class="shape"></div>
<div class="shape image"></div>
<br/>

<div id="shape-2"></div>
Run Code Online (Sandbox Code Playgroud)

但与SVG剪辑路径不同,纯CSS版本(即不使用内联或外部SVG)似乎无法支持path.它只支持形状,所以在这种情况下,如果clip-path直接使用父对象,它只会产生一个椭圆(如代码片段所示).为了克服这个问题,我们必须将剪辑路径放在子节点(或伪元素)上,这意味着剪切区域不会透明.


使用Canvas:

使用Canvas也可以完成同样的操作.Canvas命令与SVG非常相似,它们的优点也非常相似.但是,Canvas是基于栅格的,因此不像SVG那样扩展.

window.onload = function() {
  /* Canvas example with path */
  var canvas = document.getElementById('canvas');
  if (canvas.getContext) {
    var ctx = canvas.getContext('2d');
    var img = new Image();
    img.src = 'http://lorempixel.com/150/300';
    ctx.beginPath();
    ctx.moveTo(110, 0);
    ctx.arc(60, 0, 50, 0, 3.14, false);
    ctx.lineTo(10, 145);
    ctx.lineTo(110, 145);
    ctx.closePath();
    ctx.fill();
    /* Use below for using image as a fill */
    /*img.onload = function(){
        var ptrn = ctx.createPattern(img,'no-repeat');
        ctx.fillStyle = ptrn;
        ctx.fill();
    }*/
  }

  /* Canvas example with clip path */
  var canvasClip = document.getElementById('canvas-clip');
  if (canvasClip.getContext) {
    var ctxClip = canvasClip.getContext('2d');
    ctxClip.beginPath();
    ctxClip.moveTo(10, 145);
    ctxClip.lineTo(10, 0);
    ctxClip.arc(60, 0, 50, 0, Math.PI * 2, true);
    ctxClip.lineTo(110, 145);
    ctxClip.lineTo(10, 145);
    ctxClip.clip();
    ctxClip.fillStyle = 'tomato';
    ctxClip.fill();
  }
}
Run Code Online (Sandbox Code Playgroud)
canvas {
  height: 150px;
  width: 300px;
}
/* Just for demo */

body {
  background-image: radial-gradient(circle, #3F9CBA 0%, #153346 100%);
}
Run Code Online (Sandbox Code Playgroud)
<canvas id='canvas'></canvas>
<canvas id='canvas-clip'></canvas>
Run Code Online (Sandbox Code Playgroud)


使用蒙版:

也可以使用CSS(或)SVG蒙版创建此形状.CSS掩码支持非常差,目前仅在Webkit驱动的浏览器中工作,但SVG掩码有更好的支持,应该在IE9 +中工作.

/* CSS Mask */

.shape {
  width: 150px;
  height: 150px;
  background-color: black;
  -webkit-mask-image: radial-gradient(circle closest-corner at 50% 0%, transparent 98%, white 99%);
  mask-image: radial-gradient(circle closest-corner at 50% 0%, transparent 98%, white 99%);
}

/* End of CSS Mask */

svg {
  height: 150px;
  width: 150px;
}
polygon#shape {
  fill: black;
  mask: url(#masker);
}
/* Just for demo */

body {
  background-image: radial-gradient(circle, #3F9CBA 0%, #153346 100%);
}
Run Code Online (Sandbox Code Playgroud)
<!-- CSS Mask -->

<div class='shape'></div>

<!-- SVG Mask -->
<svg viewBox='0 0 100 100' preserveAspectRatio='none'>
  <defs>
    <mask id='masker' x='0' y='0' width='100' height='100'>
      <polygon points='0,0 100,0 100,100 0,100' fill='#fff' />
      <circle r='50' cx='50' cy='0' fill='#000' />
    </mask>
  </defs>
  <polygon points='0,0 100,0 100,100 0,100' id='shape' />
</svg>
Run Code Online (Sandbox Code Playgroud)


Ana*_*Ana 8

使用径向渐变可以非常轻松地完成.

DEMO

结果:

形状

HTML:

<div class='shape'></div>
Run Code Online (Sandbox Code Playgroud)

相关CSS:

.shape {
  margin: 0 auto;
  width: 10em; height: 16em;
  /* WebKit browsers, old syntax */
  background: -webkit-radial-gradient(50% 0, circle, transparent 30%, black 30%);

  /* IE10, current versions of Firefox and Opera */
  background: radial-gradient(circle at 50% 0, transparent 30%, black 30%);
}
Run Code Online (Sandbox Code Playgroud)

有关兼容性的详细信息,请参见http://caniuse.com/#feat=css-gradients.