Cut circle on the corner of box - html

I want to remove most of the circle and only show the part of the circle that overlaps a square:
I need to cut the red area and leave the darker green area inside the box.
I have a class named circle with a style
position: absolute;
bottom: 0;
left: 0;
width: 100px;
height: 100px;
and a box with style:
width: 100px;
height: 100px;
How can I remove the red area?
My code: https://codepen.io/anon/pen/xpVJoL

You can use negative values for position and overflow:hidden to hide (cut) the area :
.box {
width: 100px;
height: 100px;
border: 1px solid;
position:relative;
overflow:hidden;
}
.circle {
position: absolute;
bottom: -50px;
left: -50px;
width: 100px;
height: 100px;
border-radius: 50%;
background: red;
}
<div class="box">
<div class="circle"></div>
</div>
UPDATE
And if you want a more fancy way you can use radial-gradient as background and you will have much less code to handle :
.box {
width: 100px;
height: 100px;
border: 1px solid;
background-image:radial-gradient(circle at bottom left, red 45%, transparent 0%);
}
<div class="box">
</div>

Just insert overflow:hidden; in the .container class.

You don't need another div, you can just do it with the :before or :after pseudo-elements:
div {
width: 100px;
height: 100px;
position: relative;
border: 1px solid;
overflow: hidden;
}
div:before {
content: "";
position: absolute;
width: 100%;
height: 100%;
top: 50%;
left: -50%;
border-radius: 50%;
background: red;
}
<div></div>

Related

Curve the element slightly until it becomes a straight line

Is it possible to create an a curved element like this with border radius ruler if so what is it?
.box {
width: 500px;
height: 100px;
border: solid 0 #000;
border-color: #000 transparent transparent transparent;
border-radius: 100%/0 100px 0 0px;
background-color: #FF7C07;
}
<div class="box"></div>
i tried doing it with this code but i cant seem to make it work.
You can use a radial-gradient as background for this:
.box {
width: 500px;
height: 200px;
background: radial-gradient(110% 50% at top right,#0000 99%,#FF7C07);
}
<div class="box"></div>
This was the closest I could get to your curved element. Sadly, there is no direct way of doing this using only border radius.
.box {
width: 500px;
height: 100px;
border: solid 0 #000;
border-color: #000 transparent transparent transparent;
background-color: #FF7C07;
}
.top-right {
margin-top: -125px;
margin-left: -35px;
width: 535px;
height: 80px;
border-bottom-left-radius: 100%;
background-color: #FFFFFF;
}
<div class="box"></div>
<div class="top-right"></div>
I would recommend the following method instead:
.box {
height: 200px;
overflow:hidden;
background-color: #FF7C07;
position:relative;
z-index:10;
}
.box:before {
content: "";
position: absolute;
left: -25%;
right: -125%;
top: -200%;
bottom: 30%;
background-color: #FFF;
border-radius: 100%;
z-index: -1;
}
<div class="box">123</div>

Preventing shapes from overflow + positioning?

I'm trying to create a component where there is a hollow circle that is cropped at the bottom-left corner:
I've attempted to do so with the pseudo classes below but am facing two issues after working with it for a while (dimensions of the circle are not the same, but I will address this later):
The circles overflow outside of the component
The positioning of the circles changes depending on where the element is used.
#element {
max-width: 750px;
height: 350px;
border-radius: 8px;
background-color: #008001;
}
#element:before {
content: '';
position: absolute;
width: 100px;
height: 100px;
z-index: 1;
background: #008001;
border-radius: 50%;
top: 290px;
right: 94%;
}
#element:after {
content: '';
position: absolute;
width: 150px;
height: 150px;
background: #fff;
opacity: 0.5;
border-radius: 50%;
top: 260px;
right: 92%;
}
<div id="element"></div>
Set overflow:hidden to #element. SO `we can hide overflowing content of ::before CSS. Also set #element to position:relative, which set's boundary for ::before when we try to set it after making it absolute.
Once above =e things are done, just apply #element::before to bottom:-50px and left:-50px, I have used 50px as height and width were set to 100px.
Then to get the border just set border:25px solid #yourolor.
#element {
max-width: 750px;
height: 350px;
border-radius: 8px;
background-color: #008001;
overflow: hidden;
position: relative;
}
#element:before {
content: '';
position: absolute;
width: 100px;
height: 100px;
z-index: 4;
background: #008001;
bottom: -50px;
left: -50px;
border-radius: 100%;
border: 25px solid #c5ffc6;
}
<div id="element"></div>

CSS: How to overlap circle element properly

What I want to do is to cover circle element with square. But I can still see circle border.
When I inspect the element, the child element size doesn't include the parent's border (118px x 118px) so I tried to remove box-sizing: border-box;. Even though child element size is 120px x 120px, the same thing still happens.
How can I cover the circle properly?
.circle {
position: relative;
width: 120px;
height: 120px;
border-radius: 50%;
border: 1px solid red;
box-sizing: border-box;
background-color: white;
}
.square {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: white;
}
/* added by editor for betetr visualization purpose */
body {
background: gray;
}
<div class="circle">
<div class="square"></div>
</div>
The content itself starts within the border, not at the end of the border. As such you have to position the element out of the content area. Instead of using top, right, bottom, left you could simply use inset:
.circle {
position: relative;
width: 120px;
height: 120px;
border-radius: 50%;
border: 1px solid red;
box-sizing: border-box;
background-color: white;
}
.square {
position: absolute;
inset: -1px;
background-color: white;
}
/* added by editor for betetr visualization purpose */
body {
background: gray;
}
<div class="circle">
<div class="square"></div>
</div>
You can cover the circle properly by adding a border also to the square. and moving it a bit.
.circle {
position: relative;
width: 120px;
height: 120px;
border-radius: 50%;
border: 1px solid red;
box-sizing: border-box;
background-color: white;
}
.square {
position: absolute;
top: -1px;
left: -1px;
width: 100%;
height: 100%;
background-color: rgba(255,255,255,0.5);
border: 1px solid white;
}
/* added by editor for betetr visualization purpose */
body {
background: gray;
}
<div class="circle">
<div class="square"></div>
</div>

Why does setting 50vw or 50% on an element with position absolute/fixed not set it exactly in the middle?

In fact, I sometimes find that setting 50vw just brings the element around half of the distance it's supposed to be.
Why does this happen? What am I missing out here?
You need to subtract half the width of the item as well. You can do that, for example, with margin. There are other options as well such as calc.
See this fiddle with an example: https://jsfiddle.net/cgsymvyg/1/
#box1 {
border: 1px solid red;
background: green;
color: white;
width: 100px;
height: 100px;
position: absolute;
left: 50vw;
}
#box2 {
top: 200px;
border: 1px solid green;
background: red;
color: white;
width: 100px;
height: 100px;
position: absolute;
left: 50vw;
margin-left: -50px;
}
#box3 {
top: 400px;
border: 1px solid yellow;
background: blue;
color: white;
width: 100px;
height: 100px;
position: absolute;
left: calc(50vw - 50px);
}
<div id="box1">
Box 1
</div>
<div id="box2">
Box 2
</div>
<div id="box3">
Box 3
</div>

Coloring Overlapping Div Shapes Using CSS

I am new to coding, and am trying to make the intersecting part of these div's a different color. My initial attempt was to create a third div with a border specification to mimic the shapes, but I cannot make it match perfectly. Below is the markup and styling, describing what I want to be a red square and blue circle overlapping, with the overlap section being purple.
.box {
width: 200px;
height: 200px;
background: red;
position: relative;
top: 40px;
left: -35px;
}
.shape {
width: 250px;
height: 250px;
background: navy;
border-radius: 50%;
position: absolute;
left: 50px;
top: 50px;
}
#top-left {
width: 148px;
height: 147px;
background: purple;
position: absolute;
top: 1px;
left:2px;
border-top-left-radius: 118px;
}
<div class="box">
<div class="shape">
<div id="top-left"></div>
</div>
</div>
Is there an easier way to do this, or a way to make the top-left-border perfectly round?
Add overflow: hidden; to .shape. Position top-left relatively. Done!
.box {
width: 200px;
height: 200px;
background: red;
position: relative;
top: 40px;
}
.shape {
width: 250px;
height: 250px;
background: navy;
border-radius: 50%;
position: absolute;
left: 75px;
top: 50px;
overflow: hidden;
}
#top-left {
width: 150px;
height: 150px;
background: purple;
position: relative;
left: -25px;
}
<div class="box">
<div class="shape">
<div id="top-left"></div>
</div>
</div>
Output :