This question already has answers here:
Why does z-index not work?
(10 answers)
Closed 4 years ago.
I am trying to make my middle div under my background_top div. This is my first time using clip-path, and it seems like it ignores the z-index(?). Down below I have added a codepen.
The background_top div does have a clip-path, giving it a slanted bottom, and I am trying to get it ontop of the middle div.
I have given my background_top a z-index of -100, and my middle a z-index of -250
Here is my code:
<div class="background_top">
<h1>we build<span>futures.<span></h1>
<div class="top_quote">
<h2>“</h2>
</div>
<div class="top_p">
<p></p>
</div>
</div>
<div class="middle">
</div>
</body>
<footer>
<div class="footer">
<div class="">
<p></p>
</div>
</div>
</footer>
.middle {
margin-top: -45vh;
height: 150vh;
width: 100vw;
-webkit-clip-path: polygon(0 0, 100% 0, 100% 100%, 0 80%);
clip-path: polygon(0 0, 100% 0, 100% 100%, 0 80%);
background-color: #B2DFEE;
z-index: -250;
}
.background_top {
height: 95vh;
width: 100vw;
-webkit-clip-path: polygon(0 0, 100% 0, 100% 60%, 0 100%);
clip-path: polygon(0 0, 100% 0, 100% 60%, 0 100%);
z-index: -100;
background: #232323;
background-image: url("images/placeholder.jpg");
background-size: cover;
}
.footer {
width: 100vw;
background-color: #232323;
height: 100vh;
position: fixed;
bottom: 0;
z-index: -230;
}
you need to set the position for z-index to work
Just add position:relative;
.middle {
margin-top: -45vh;
height: 150vh;
width: 100vw;
-webkit-clip-path: polygon(0 0, 100% 0, 100% 100%, 0 80%);
clip-path: polygon(0 0, 100% 0, 100% 100%, 0 80%);
background-color: #B2DFEE;
z-index: 1;
position: relative;
}
.background_top {
height: 95vh;
width: 100vw;
-webkit-clip-path: polygon(0 0, 100% 0, 100% 60%, 0 100%);
clip-path: polygon(0 0, 100% 0, 100% 60%, 0 100%);
z-index: 2;
background: #232323;
background-image: url("images/placeholder.jpg");
background-size: cover;
position: relative;
}
.footer {
width: 100vw;
background-color: #232323;
height: 100vh;
position: fixed;
bottom: 0;
z-index: -230;
}
<div class="background_top">
<h1>we build<span>futures.</span></h1>
<div class="top_quote">
<h2>“</h2>
</div>
<div class="top_p">
<p></p>
</div>
</div>
<div class="middle">
</div>
<footer>
<div class="footer">
<div class="">
<p></p>
</div>
</div>
</footer>
Related
I have two DIVs over the top of a single background image. I want to darken all areas around the two DIVs and keep the image unaffected within the two DIVS, creating a kind of spotlight effect.
I tried using shadows around the DIVs but both cast a shadow over each other.
Is there any way to darken all areas outside of the 2 DIVS?
E.g.
<div class="background-image">
<div class="light-area-1"></div>
<div class="light-area-2"></div>
</div>
If there are just two 'spotlights' and the effect is to be purely visual you could use pseudo elements and clip-paths to overlay the spotlights:
.spotlit {
--bg: url(https://picsum.photos/id/1015/200/300);
background-image: linear-gradient(rgba(0, 0, 0, 0.6), rgba(0, 0, 0, 0.6)), var(--bg);
background-size: cover;
width: 80vmin;
height: 70vmin;
position: relative;
}
.spotlit::before,
.spotlit::after {
content: '';
position: absolute;
width: 100%;
height: 100%;
display: inline-block;
top: 0;
left: 0;
background-image: var(--bg);
background-size: cover;
}
.spotlit::before {
clip-path: polygon(20% 20%, 50% 20%, 50% 40%, 20% 40%);
}
.spotlit::after {
clip-path: polygon(70% 60%, 90% 60%, 90% 95%, 70% 95%);
}
<div class="spotlit">
</div>
However, if the spotlights are needed as actual elements a similar approach lays them above the main, shadowed element using clip-path as above. This way you can have as many spotlights as required.
.background-image {
--bg: url(https://picsum.photos/id/1015/200/300);
background-image: linear-gradient(rgba(0, 0, 0, 0.6), rgba(0, 0, 0, 0.6)), var(--bg);
background-size: cover;
width: 80vmin;
height: 70vmin;
position: relative;
}
.light-area-1,
.light-area-2 {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-image: var(--bg);
background-size: cover;
}
.light-area-1 {
clip-path: polygon(20% 20%, 50% 20%, 50% 40%, 20% 40%);
}
.light-area-2 {
clip-path: polygon(70% 60%, 90% 60%, 90% 95%, 70% 95%);
}
<div class="background-image">
<div class="light-area-1"></div>
<div class="light-area-2"></div>
</div>
The shapes can be changed given clip-path's various options such as circle which may be useful to make them look more like an actual spotlight.
I wrote some JavaScript code to generate Pie Charts for me, but when one Slice is bigger than half, the whole thing breaks. Using clip to make half-circles, it is not possible to display more than half, but I cannot understand, why the big part becomes smaller with white on either site.
I assume it would be easiest to change the code to recognize the biggest slice and jest make it a full circle in the bag of the others, but I really would prefer if there were another way.
.pie_container {
width: 500px;
height: 500px;
position: relative;
}
.inner-slice {
position: absolute;
width: 500px;
height: 500px;
border-radius: 50%;
clip: rect(0px, 250px, 500px, 0px);
}
.outer-slice {
position: absolute;
width: 500px;
height: 500px;
border-radius: 50%;
clip: rect(0px, 500px, 500px, 250px);
}
<div class="pie_container">
<div class="outer-slice" style="transform: rotate(0deg);">
<div class="inner-slice" style="background-color: blue;transform: rotate(40deg);"></div>
</div>
<div class="outer-slice" style="transform: rotate(40deg);">
<div class="inner-slice" style="background-color: green;transform: rotate(40deg);"></div>
</div>
<div class="outer-slice" style="transform: rotate(80deg);">
<div class="inner-slice" style="background-color: yellow;transform: rotate(40deg);"></div>
</div>
<div class="outer-slice" style="transform: rotate(120deg);">
<div class="inner-slice" style="background-color: orange;transform: rotate(40deg);"></div>
</div>
<div class="outer-slice" style="transform: rotate(160deg);">
<div class="inner-slice" style="background-color: red;transform: rotate(200deg);"></div>
</div>
</div>
You could use a conic-gradient background image for the pie chart div instead.
This diagram
was produced with CSS:
.pie_container {
width: 500px;
height: 500px;
position: relative;
border-radius: 50%;
background-repeat: no-repeat no-repeat;
background-size: 100% 100%;
background-position: center center;
}
and a conic gradient background-image in the element itself:
<div class="pie_container" style="background-image: conic-gradient(blue, blue 40deg, green 40deg, green 80deg, yellow 80deg, yellow 120deg, orange 120deg, orange 160deg, red 160deg, red 360deg);">
No need for other elements.
.pie_container {
width: 500px;
height: 500px;
position: relative;
border-radius: 50%;
background-repeat: no-repeat no-repeat;
background-size: 100% 100%;
background-position: center center;
}
<div class="pie_container" style="background-image: conic-gradient(blue, blue 40deg, green 40deg, green 80deg, yellow 80deg, yellow 120deg, orange 120deg, orange 160deg, red 160deg, red 360deg);"></div>
I rewrote my code to give me points on a clip-path, as suggested in this answer to a different post: How to draw a circle sector in CSS?
I took the clip-path, made the background-color transparent and added a box-shadow to make it a ring peace instead of a pie slice.
.slice {
width: 100%;
height: 100%;
border-radius: 50%;
position: absolute;
background-color: transparent;
}
.slice:hover {
transform: scale(1.1);
}
.ring {
position: relative;
width: 400px;
height: 400px;
margin: 25px;
}
.middleText {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
font-size: 18pt;
}
<div id="ring_diagram">
<div class="ring">
<span title="Data 1: 34 (52.31%)"><div class="slice slice1" style="box-shadow: red 0px 0px 0px 67px inset; clip-path: polygon(50% 50%, 50% 0%, 100% 0%, 100% 100%, 100% 100%, 100% 100%, 39.1633% 124.213%);"></div></span>
<span title="Data 2: 26 (40%)"> <div class="slice slice2" style="box-shadow: blue 0px 0px 0px 67px inset; clip-path: polygon(50% 50%, 39.1633% 124.213%, 0% 100%, 0% 0%, 0% 0%, 0% 0%, 15.1458% -16.4092%);"></div></span>
<span title="Data 3: 3 (4.62%)"> <div class="slice slice3" style="box-shadow: yellow 0px 0px 0px 67px inset; clip-path: polygon(50% 50%, 15.1458% -16.4092%, 35.5905% -23.6028%, 35.5905% -23.6028%, 35.5905% -23.6028%, 35.5905% -23.6028%, 35.5905% -23.6028%);"></div></span>
<span title="Data 4: 2 (3.08%)"> <div class="slice slice4" style="box-shadow: green 0px 0px 0px 67px inset; clip-path: polygon(50% 50%, 35.5905% -23.6028%, 50% -25%, 50% -25%, 50% -25%, 50% -25%, 50% -25%);"></div></span>
<div class="middleText"><h3>Text</h3>65</div>
</div>
</div>
This question already has answers here:
position:relative leaves an empty space
(13 answers)
Closed 2 years ago.
I have a page with an angled DIV at the top and a DIV immediately below that which is angled. I've had to offset the position of both top DIVs so that they join together. The only issue is that the next DIV below these has a space the size of the 120px offset.
I could apply this to every DIV but doing this to the footer means that there's empty white space at the bottom of the page.
.top {
min-width: 100%;
min-height: 400px;
background: linear-gradient(45deg, #FF0000, #00FF00);
clip-path: polygon(0 0, 100% 0, 100% 100%, 0 80%);
}
.main {
min-width: 100%;
min-height: 600px;
background: #0000FF;
clip-path: polygon(0 0, 100% 20%, 100% 100%, 0 100%);
position: relative;
top: -120px;
}
.next {
min-width: 100%;
min-height: 600px;
background: #000000;
}
<div class="top"></div>
<div class="main"></div>
<div class="next"></div>
Any ideas to make the black DIV begin at the bottom of the blue one and the rest of the page not be affected would be appreciated, you can't simply use top: -120px; on every DIV because then there's 120px of empty space at the bottom of the page.
You can change top by margin-top, it will have the same effect for the element, and the normal flow after it will be the same
.top {
min-width: 100%;
min-height: 400px;
background: linear-gradient(45deg, #FF0000, #00FF00);
clip-path: polygon(0 0, 100% 0, 100% 100%, 0 80%);
}
.main {
min-width: 100%;
min-height: 600px;
background: #0000FF;
clip-path: polygon(0 0, 100% 20%, 100% 100%, 0 100%);
position: relative;
margin-top: -120px;
}
.next {
min-width: 100%;
min-height: 600px;
background: #000000;
}
<div class="top"></div>
<div class="main"></div>
<div class="next"></div>
There's a lot of questions similar to this but they're for edges or have answers incompatible with some browsers. I'd thought of using a gradient image for the background but can achieve the same effect using a background gradient and I'd guess this may be easier to implement with minimal code.
I currently have this, which has flat edges;
.top {
min-width: 100%;
min-height: 400px;
background: linear-gradient(45deg, #FF0000, #00FF00);
}
.main {
min-width: 100%;
min-height: 600px;
background: #0000FF;
}
<div class="top"></div>
<div class="main"></div>
It uses minimal code but I'd be aiming for an angled edge either on the bottom of one and on the top of the other or just the bottom of the top one so that the DIVs match up.
I'd be aiming for something like this…
Of course I could rotate the DIV but then there's overflow. I want something clean so that both DIVs match up. Something using clip-path: polygon could work but I can't figure out the angles or implementation. Any ideas or resources for where to start would be apprecited.
UPDATE
I've figured out how to angle both so that they match up but the DIVs need to be touching for it to look proper.
.top {
min-width: 100%;
min-height: 400px;
background: linear-gradient(45deg, #FF0000, #00FF00);
clip-path: polygon(0 0, 100% 0, 100% 100%, 0 80%);
}
.main {
min-width: 100%;
min-height: 600px;
background: #0000FF;
clip-path: polygon(0 0, 100% 20%, 100% 100%, 0 100%);
}
<div class="top"></div>
<div class="main"></div>
UPDATE 1
Would this work? I added position: relative; and top: -150px; to move it up.
.top {
min-width: 100%;
min-height: 400px;
background: linear-gradient(45deg, #FF0000, #00FF00);
clip-path: polygon(0 0, 100% 0, 100% 100%, 0 80%);
}
.main {
min-width: 100%;
min-height: 600px;
background: #0000FF;
clip-path: polygon(0 0, 100% 20%, 100% 100%, 0 100%);
position: relative;
top: -120px;
}
<div class="top"></div>
<div class="main"></div>
Please help me to solve this issue, image not working z-index when div on clip-path. how i z-index to image easily thanks
Please check my code :-
.mymap {
background-image: url("https://images.pexels.com/photos/257360/pexels-photo-257360.jpeg?auto=compress&cs=tinysrgb&h=350");
background-repeat: no-repeat;
-webkit-clip-path: polygon(100% 0, 100% 80%, 0 80%, 0 23%);
clip-path: polygon(100% 0, 100% 80%, 0 80%, 0 23%);
}
.mymap {
height: 220px;
}
.men img {
width: 20%;
}
.men img {
z-index: 999;
position: relative;
}
<div class="mymap">
<div class="men">
<img src="https://i.pinimg.com/originals/1c/01/27/1c0127d19cdd75efb5a3eca4384658d5.png">
</div>
</div>
Here is the codepen link :- https://codepen.io/anon/pen/JaQoOX
Please check and tell me how to fix this.
You need to to wrap .mymap and .men class into parent container and set position:relative to .container class and position:absolute to image.
.container{
position:relative;
}
.mymap {
background-image: url("https://images.pexels.com/photos/257360/pexels-photo-257360.jpeg?auto=compress&cs=tinysrgb&h=350");
background-repeat: no-repeat;
-webkit-clip-path: polygon(100% 0, 100% 80%, 0 80%, 0 23%);
clip-path: polygon(100% 0, 100% 80%, 0 80%, 0 23%);
}
.mymap {
height: 220px;
}
.men img {
width: 20%;
}
.men img {
z-index: 999;
position: absolute;
top:0;
}
<div class="container">
<div class="mymap"></div>
<div class="men">
<img src="https://i.pinimg.com/originals/1c/01/27/1c0127d19cdd75efb5a3eca4384658d5.png">
</div>
</div>