Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 632
nvc_cdr Avatar asked Jan 26 '15 01:01

nvc_cdr


People also ask

How do you make overlapping elements in CSS?

Use CSS grid and set all the grid items to be in the same cell. Adding the layered class to an element causes all it's children to be layered on top of each other. if the layers are not the same size you can set the justify-items and align-items properties to set the horizontal and vertical alignment respectively.

How do I make div elements overlap?

You can use the CSS position property in combination with the z-index property to overlay an individual div over another div element. The z-index property determines the stacking order for positioned elements (i.e. elements whose position value is one of absolute , fixed , or relative ).


1 Answers

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 :

enter image description here

like image 118
The Pragmatick Avatar answered Sep 28 '22 10:09

The Pragmatick