I would like to make a transparent cut out half circle shape using only CSS3. The only requirement is that all the elements that form the shape must be black or transparent.
I cannot use a black rectangle with a white circle on top of it because the half circle has to be transparent and let the background show through.
Desired shape :
You can use box-shadows to make the transparent cut out circle :
This can be responsive with percentage lengths:
You can do it really easily with a radial gradient.
DEMO
Result:
HTML:
Relevant CSS:
See http://caniuse.com/#feat=css-gradients for detailed info on compatibility.
Using SVG:
Here is an alternate solution using SVG (though you haven't tagged it). Advantages of using SVG are:
While SVG is not supported by <= IE8 whereas box-shadow is, fallbacks can be provided.
Using CSS:
CSS also has
clip-path
specifications and we can try something like in the below snippet.But unlike SVG clip-path, the pure CSS version (that is, without using an inline or external SVG) doesn't seem to be able to support a
path
. It only supports shapes and so in this case, if you use theclip-path
on the parent directly it would just produce an ellipse (like shown in the snippet). To overcome this, we would have to put the clip-path on a child (or a pseudo element) and this would mean that the clipped area would not be transparent.Using Canvas:
The same can be done using Canvas also. Canvas commands are pretty similar to SVG and their advantages are also pretty similar. However, Canvas are raster based and hence doesn't scale as well as SVG does.
Using Masks:
This shape can be created by using CSS (or) SVG masks also. CSS masks have very poor support and work currently only in Webkit powered browsers but SVG masks have much better support and should work in IE9+.
Kyle Sevenokas did some good work. And I built off of that. Checkout the http://jsfiddle.net/FcaVX/1/
I basically collapsed the white div for the circle and gave it white borders. The OP question talked about the colors elements that make up the shape; nothing about its borders right?
Right now, the only way I can think of would be to use a lot of 1-pixel wide black divs next to eachother with varying height. It's technically possible this way but should be deeply frowned upon. Also; you won't have anti-aliassing unless you want to go through the trouble of adding 1x1 pixel divs and do the anti-aliassing manually.
It might be more helpful if you gave an example of how you wanted to use this. Why does it need to be black/transparent only? As stated by omarello, the best solution on most circumstances is probably a simple GIF or PNG image with transparency.
I needed rounded corners only on the bottom of a responsive image. I started from @sandeep fiddle and improved it for my needs:
https://jsfiddle.net/mop00j22/