Alp
Alp

Reputation: 29739

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

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

CSS Shape

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?

Upvotes: 46

Views: 33429

Answers (7)

Chakib Salah
Chakib Salah

Reputation: 546

Using clip path this can be done .

let precision = 64;
let radius = 50;
let c = [...Array(precision)].map((_, i) => {
  let a = -i/(precision-1)*Math.PI*2;
  let x = Math.cos(a)*radius + 100;
  let y = Math.sin(a)*radius + 50;
  return `${x}% ${y}%`
})

document.querySelector('.circleContainer').style.clipPath = 
 `polygon(100% 50%, 100% 100%, 0 100%, 0 0, 100% 0, 100% 50%, ${c.join(',')})`;
.container{
  display: flex;
  position: relative;
  width: 200px;
}
.left{
  background: blue;
  width: 100px;
  height: 100px;
   border-top-left-radius: 50%;
  border-bottom-left-radius: 50%;
}
.circleContainer {
  background: blue;
  width: 100px;
  height: 100px;
}

.innerCircle{
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background: orange;
  position: absolute;
  top: 10px;
  right: -40px;
}
<div class='container'>
<div class='left'></div>
<div class='circleContainer'></div>
<div class='innerCircle'></div>
</div>
Using an approah which I found here

Upvotes: 1

web-tiki
web-tiki

Reputation: 103790

Different approach : Box-shadows

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

DEMO

Output :

CSS shape with inset curve using box-shadows

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

Upvotes: 8

Jon Mifsud
Jon Mifsud

Reputation: 183

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.

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.

Upvotes: 1

ScottS
ScottS

Reputation: 72261

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

Upvotes: 59

karim79
karim79

Reputation: 342635

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:

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

Demo.

Upvotes: 0

Chris Fletcher
Chris Fletcher

Reputation: 2387

I can't really tell from your drawing how rounded you want the points, but here's one possibility: 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.

Upvotes: 8

Rick Donohoe
Rick Donohoe

Reputation: 7271

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

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

and the question: CSS3 Inverted Rounded Corner

Hopefully that helps!

Upvotes: 0

Related Questions