This has been asked before but none of the answers seem to be working for me.
My issue is related to a lost z-index when a transformation is applied.
I have an overlay div with a defined z-index, it has a sibling with no z-index and this div contains a child with a z-index greater than the overlay. This child can be dragged around.
At some point I rotate this sibling and it's child loses the z-index.
How can I prevent this from happening?
I tried several solutions attemps like transform-style: flat; or transform-style: preserve-3d; but with no luck
This is the code
HTML
<div class="main">
<div class="some_container">
<div class="drag"></div>
</div>
</div>
<div class="overlay"></div>
<br><br><br>
<button>rotate!</button>
CSS
body {
padding: 20px
}
div {
border: 1px solid black;
width: 50px;
height: 50px;
}
.main {
border: 1px dashed blue;
padding: 15px;
}
.some_container {
position: relative;
background-color: green;
}
.overlay {
background-color: red;
position: absolute;
left: 35px;
top: 35px;
z-index: 5
}
.drag {
position: relative;
width: 30px;
height: 30px;
background-color: lime;
z-index: 10;
cursor: move;
}
.rotated {
transform: rotateZ(15deg);
}
.rotated .drag {
background-color: yellow;
transform: rotateZ(-15deg);
position: relative;
z-index: 100;
transform-style: flat;
}
JS
$(".drag").draggable();
$("button").click(function()
{
$(".some_container").addClass("rotated");
});
fiddle: https://jsfiddle.net/2zkn9dap/
The transform that you have in your .rotated class creates a new stacking context that is changing the order that the elements are layered. A great explanation with more detail can be found here: z-index is canceled by setting transform(rotate)
The best approach to solving this is to move the .drag div to be a sibling of the .overlay and .some_container div. Then update your JS to add the rotated class to the green and yellow squares so they are both rotated. Otherwise, you'll never be able to get the yellow square on top of the red one consistently, because the z-index of the parent, in this case the .some_container div takes precedence.
$("button").click(function(){
$(".green").addClass("rotated")
$(".lime").addClass("rotated").css({backgroundColor: 'yellow'});
});
body {
padding: 20px
}
div {
border: 1px solid black;
width: 50px;
height: 50px;
}
.container {
border: 1px dashed blue;
padding: 15px;
}
.green {
position: absolute;
background-color: green;
z-index: 2;
}
.red {
background-color: red;
position: absolute;
left: 35px;
top: 35px;
z-index: 3;
}
.lime {
position: absolute;
width: 30px;
height: 30px;
background-color: lime;
z-index: 4;
cursor: move;
}
.rotated {
transform: rotateZ(15deg);
}
<div class="container">
<div class="green">
</div>
<div class="lime"></div>
</div>
<div class="red"></div>
<br><br><br>
<button>rotate!</button>
<script src="http://code.jquery.com/jquery-3.3.1.js"></script>
Change the position: relative to absolute of .lime.
If you don't want to rotate the '.lime' div, then remove `.addClass("rotated") on the 4th line of the script.
Related
I have a problem with z-index property. I have some absoluted positioned elements (div in the code)one inside each other. The first div inside (red div) naturally would be in front of its parent (aliceblue element), thus I give a negative z-index to the red div inside and it's ok. But the aqua div (inside the red div) is in front of red element too (instead I would lay out the aqua element behind the red element), to lay out it behind I give it z-index:-5 for example but it doesn't work because I created a new stacking context different from stacking context of its father
div {
width: 300px;
height: 300px;
text-align: center;
position: absolute;
}
.a {
background-color: aliceblue;
position: relative;
}
.a1 {
background-color: red;
left: 20px;
top: 20px;
z-index: -1;
}
.a11 {
background-color: aqua;
left: 10px;
top: 10px;
/* z-index: -5; This doesn't work*/
}
.a2 {
background-color: yellow;
top: 40px;
z-index: -2;
}
<div class="a">
A
<div class="a1">
A1
<div class="a11">A11</div>
</div>
<div class="a2">A2</div>
</div>
So far we haven't found a way to do this using z-index.
The following method may or may not be suitable for your particular use case/overall context but it does solve the problem for the code given in the question.
This snippet uses 3d transforms rather than z-index to position the elements in relation to the viewer who is looking head on to the screen.
Of course we don't actually want to introduce perceptible perspective so the snippet uses a small shift back (0.1px). This seems enough to get the system to position elements on top of each other in the required order but small fractions were ignored (at least on my Windows10 Edge).
div {
width: 300px;
height: 300px;
text-align: center;
position: absolute;
transform-style: preserve-3d;
}
.a {
background-color: aliceblue;
position: relative;
transform: translate3d(0, 0, 0);
}
.a1 {
background-color: red;
left: 20px;
top: 20px;
transform: translate3d(0, 0, -0.1px);
}
.a11 {
background-color: aqua;
left: 10px;
top: 10px;
transform: translate3d(0, 0, -0.2px);
}
.a2 {
background-color: yellow;
top: 40px;
transform: translate3d(0, 0, -0.4px);
}
<div class="a">
A
<div class="a1">
A1
<div class="a11">A11</div>
</div>
<div class="a2">A2</div>
</div>
This question already has answers here:
Why can't an element with a z-index value cover its child?
(5 answers)
Closed 1 year ago.
I want to add after pseudo element for div. I did this in react. When I test it in codepen, it works in codepen. But in my local machine, it is not working. I applied z-index pseudo element only in codepen, But it works. In my project, Even I applied z-index for both parent div and pseudo element, it does not help. What mistake I did? Anyone, Please guide me. Thanks in Advance.
My code is:
HTML:
<div className='cards'>
<div className='card active'>Card</div>
<div className='card'>Card</div>
<div className='card'>Card</div>
</div>
CSS:
.card {
width: 300px;
height: 200px;
background-color: #fff;
border-radius: 5px;
&.active {
position: relative;
box-shadow: 10px 10px 60px rgba(0, 0, 0, 0.1);
z-index: 10;
&::after {
position: absolute;
content: "";
top: 0;
left: 0;
width: calc(100% + 5px);
height: calc(100% + 15px);
border-radius: 5px;
background-color: #923929;
z-index: -1000;
transform: rotateZ(-2deg);
transform-origin: top left;
}
}
}
.cards {
padding: 5rem;
display: flex;
gap: 20rem;
background-color: #92392920;
}
output is
without z-index for parent div output looks like this.
If you add z-index to parent you create new stacking context
Just remove z-index from parent element
UPD
Yes. also I need to add the background for section element
.what_we_do{
background-color: #fff6f6;
padding:5rem;
}
.card {
width: 300px;
height: 200px;
background-color: #fff;
border-radius: 5px
}
.card.active {
position: relative;
box-shadow: 10px 10px 60px rgba(0, 0, 0, 0.1);
}
.card.active::after {
position: absolute;
content: "";
top: 0;
left: 0;
width: calc(100% + 5px);
height: calc(100% + 15px);
border-radius: 5px;
background-color: #923929;
z-index: -1;
transform: rotateZ(-2deg);
transform-origin: top left;
}
.cards {
padding: 5rem;
display: flex;
gap: 20rem;
background-color: #92392920;;
position: relative;
z-index: 1;
}
<section class="what_we_do">
<div class="cards">
<div class="card active">Card</div>
<div class="card">Card</div>
<div class="card">Card</div>
</div>
</section>
UPD: from comment
I want to add effects for active card i.e. brown background for active card.
.card.active{
background-color:#923929;
}
Codepen link: https://codepen.io/lolcatBH/pen/OJbgLyd
I have the following html:
<div id="page">
<div class="overlay"></div>
<div class="click">SHOW POPUP</div>
<div class="popup">
<h1>Korean language</h1>
<button class ="close">X</button>
<div class="desc">Korean (North Korean: 조선말/朝鮮말, chosŏnmal; South Korean: 한국어/韓國語, hangugeo) is an East Asian language spoken by about 77 million people.</div>
</div>
</div>
And CSS:
#page {
position: absolute;
top: 50%;
left: 25%;
}
.popup {
width: 300px;
height: 300px;
border: 1px solid black;
z-index: 1;
position: absolute;
top: -10%;
transform: scale(0);
padding: 20px;
}
#page .overlay {
position: fixed;
top: 0px;
left: 0px;
width: 100vw;
height: 100vh;
background: rgba(0,0,0,0.7);
z-index: 1;
display: none;
}
#page.active .overlay {
display: block;
}
.popup.active {
transform: scale(1);
background-color: white;
}
.close {
position: absolute;
right: 0%;
top: 0%;
color: white;
background: black;
border-radius: 50%;
}
.click {
border: 1px solid black;
padding: 20px;
font-size: 20px;
text-align: center;
z-index: 0;
cursor: pointer;
}
h1, desc {
text-align: center;
}
JS:
const click = document.querySelector('.click');
const x = document.querySelector('.close');
const page = document.querySelector('#page');
const popup = document.querySelector('.popup');
const showPopup = () => {
page.classList.toggle('active');
popup.classList.add('active');
}
const hidePopup = () => {
page.classList.toggle('active');
popup.classList.remove('active');
}
click.addEventListener('click', showPopup);
x.addEventListener('click', hidePopup);
At first, what I tried to do is instead of creating a separate overlay div, I was going to put a background color on the #page element. However, in effect, the background only applies to the button element (.click). I don't actually understand why in this case, since the background-color doesn't seem to affect the .popup element.
Imgur: https://imgur.com/a/UEgXSlY
So my question is, why does the #page element not cover all of its children's width and height? In this case, I thought it would have cover the whole page. I've also tried putting width: 100vw and height:100vh but it in turn only applied the dimensions to the button.
Imgur: https://imgur.com/a/QOMlnTs
The reason is that most of your elements have position set to absolute or fixed, which makes them completely (fixed) or partly (absolute) independent from their parent, i.e. there is no space of its own reserved for them, therefore they typically overlap other elements.
So the parent doesn't span or cover them, but only those elements which don't have a set position or which have position: relative or static.
I'm trying to achieve the following:
A background circle with a smaller colored circle inside of it, which must be centered
A small centered image inside of both circles
All of these items needs to be placed in a single div
I'm trying to do this with the minimum amount of code. I want to avoid duplication as much as possible. I believe that all of this can be achieved using before and after selectors, but I'm not sure how to get this done
Here's what I have so far:
CSS:
.grid-container {
display: grid;
grid: 100px / 100px;
}
.circle {
border-radius: 50%;
width: 100%;
height: 100%;
background-color: #e4e4e7;
}
.circle:before {
content: "";
border-radius: 50%;
width: 80%;
height: 80%;
top: 10%;
left: 10%;
background-color: blue;
display: block;
position: relative;
}
.image-one:before {
content: url("https://stackoverflow.com/favicon.ico");
}
.circle-01 {
grid-column: 1 / 2;
grid-row: 1 / 2;
}
HTML:
<div class="grid-container">
<div class="circle-01 circle image-one"></div>
</div>
I need a structure whereby I can easily change the color of the inner circle and/or image
Example
<div class="grid-container">
<div class="circle-01 circle image-one yellow"></div>
<div class="circle-01 circle image-two blue"></div>
<div class="circle-01 circle image-three green"></div>
</div>
You can do it with a pseudo element like this, putting the pseudo element on top of the main element and using borders and a background-image. You can even use a background color behind the image if it doesn't fill the whole pseudo element (note the no-repeat, the size and position settings for the background):
.x1 {
width: 300px;
height: 300px;
position: relative;
border-radius: 50%;
border: 10px solid #22f;
margin: 30px;
background: yellow;
}
.x1:after {
content: '';
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 220px;
height: 220px;
border-radius: 50%;
border: 6px solid #f22;
background: #3d3 url(http://placehold.it/200x200/fa0/?text=this_is_an_image) center center no-repeat;
background-size: 100px 100px;
}
<div class="x1"></div>
Note: the orange square is an image, the green color around it is the background color, the red circle is the border of the pseudo element, the yellow area is the background color of the main element and the blue circle is the border of the main element. Each of these could as well be white or transparent.
ADDITION after additional question in comment:
You can also change the background-colors by adding seperate classes. In the following snippet I added two classes to the div, one that affects the background in the main element and one that affects the background-color of the pseudo element. In the latter case you have to make sure to use the background-color property, not background in the CSS rule - otherwise the background-image would disappear:
.x1 {
width: 300px;
height: 300px;
position: relative;
border-radius: 50%;
border: 10px solid #22f;
margin: 30px;
background: yellow;
}
.x1:after {
content: '';
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 220px;
height: 220px;
border-radius: 50%;
border: 6px solid #f22;
background: #3d3 url(http://placehold.it/200x200/fa0/?text=this_is_an_image) center center no-repeat;
background-size: 100px 100px;
}
.aqua-outer-bg {
background: aqua;
}
.pink-inner-bg:after {
background-color: pink;
}
<div class="x1 aqua-outer-bg pink-inner-bg"></div>
Note: The original CSS rules remained unchanged, their background colors are overwritten by the additional classes.
ONE MORE ADDITION after additional question in comment from OP on September 18th:
Yes, you can also split that in two classes as I did below (.x1a and .x1b). I simply added both classes to the HTML tag and split up the CSS from x1:after into two rules, one for .x1a:after and one for .x2a:after
.x1a {
width: 300px;
height: 300px;
position: relative;
border-radius: 50%;
border: 10px solid #22f;
margin: 30px;
background: yellow;
}
.x1a:after {
content: '';
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 220px;
height: 220px;
background: #3d3 url(http://placehold.it/200x200/fa0/?text=this_is_an_image) center center no-repeat;
background-size: 100px 100px;
}
.x1b:after {
border-radius: 50%;
border: 6px solid #f22;
}
.aqua-outer-bg {
background: aqua;
}
.pink-inner-bg:after {
background-color: pink;
}
<div class="x1a x1b aqua-outer-bg pink-inner-bg"></div>
Try running this snippet:
$(document).ready(function() {
var sourceIndex = 1;
var colorIndex = 1;
var colors = [
"rgb(0, 132, 203)",
"rgb(255, 192, 203)",
"rgb(50, 192, 103)",
"rgb(255, 165, 0)"
];
var sources = [
"https://www.linkedin.com/favicon.ico",
"https://www.google.com/favicon.ico",
"http://jsfiddle.net/favicon.ico",
"https://getbootstrap.com/favicon.ico",
"https://www.facebook.com/favicon.ico"
];
$("button").click(function() {
changeStuff($(this).hasClass("changeImage") ? sources : colors, $(this));
function changeStuff(list, selector) {
counter(list, selector);
if (list == sources) {
selector
.prev()
.prev(".outer-circle")
.find(".inner-circle")
.find("img")
.attr("src", list[sourceIndex]);
} else {
if (
selector
.prev(".outer-circle")
.find(".inner-circle")
.css("background-color") == colors[colorIndex]
) {
selector
.prev(".outer-circle")
.find(".inner-circle")
.css("background-color", "tan");
} else {
selector
.prev(".outer-circle")
.find(".inner-circle")
.css("background-color", colors[colorIndex]);
}
}
}
});
function counter(list, selector) {
if (list == sources) {
sourceIndex == list.length - 1 ? (sourceIndex = 0) : sourceIndex++;
} else {
colorIndex == list.length - 1 ? (colorIndex = 0) : colorIndex++;
}
}
});
.container {
display: flex;
align-items: center;
flex-direction: column;
}
.box {
display: flex;
}
.inner-circle {
border-radius: 50%;
width: 80%;
height: 80%;
display: flex;
align-items: center;
justify-content: center;
}
.box:first-child .inner-circle {
background-color: blue;
}
.box:nth-child(2) .inner-circle {
background-color: black;
}
.box:nth-child(3) .inner-circle {
background-color: maroon;
}
.outer-circle {
border-radius: 50%;
width: 100px;
height: 100px;
background-color: #e4e4e7;
display: flex;
justify-content: center;
align-items: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="box">
<div class="outer-circle">
<div class="inner-circle">
<img src="https://stackoverflow.com/favicon.ico" alt="">
</div>
</div>
<button class='changeColor'>Change Color</button>
<button class='changeImage'>Change Image</button>
</div>
<div class="box">
<div class="outer-circle">
<div class="inner-circle">
<img src="https://stackoverflow.com/favicon.ico" alt="">
</div>
</div>
<button class='changeColor'>Change Color</button>
<button class='changeImage'>Change Image</button>
</div>
<div class="box">
<div class="outer-circle">
<div class="inner-circle">
<img src="https://stackoverflow.com/favicon.ico" alt="">
</div>
</div>
<button class='changeColor'>Change Color</button>
<button class='changeImage'>Change Image</button>
</div>
</div>
Abracadabra
div {
border-radius: 50%
}
#a {
display: flex;
justify-content: center;
align-items: center;
height: 64px;
width: 64px;
border: 2px solid green;
}
img {
align-self: auto;
border: 2px solid blue;
border-radius: 50%;
padding:5%;
}
<div id="a">
<img src="https://rack.pub/media/janus.png" height="48">
</div>
I want to add some pizzazz to some banners... my banners are simply an h1 element with a background color property that stretches the legth of the containing element.
Here is my CSS:
.banner {
position: relative;
z-index: 1000;
padding: 20px;
}
.banner-blue {
background-color: #93DEFF;
color: #222222;
}
.banner-yellow {
background-color: #FFF072;
color: #777777;
}
.banner-red {
background-color: #FF356B;
color: white;
}
And I would apply it like this:
<h1 class="banner banner-yellow">I'm a banner!</h1>
My problem:
I want to overlay a copy of the banner background but change the color and rotate it slightly on the z-axis to get an effect like this.
However I can't work out how to do that using the ::before (or is it ::after) psuedo-elements to do that... here is what I have tried:
.banner-red::before {
display: block;
position: absolute;
padding: 20px;
content: "";
background-color: rgba(255,30,60,0.4);
transform: rotateZ(3deg);
width: 100%;
margin-left: -30px;
}
Here is a codepen of it running: not looking too good: https://codepen.io/jethazelhurst/pen/JyKqRB
Just rotate your box in the opposite direction: transform: rotateZ(-3deg);
You can set the top and left value in order to place your rotated box correctly.
.banner-red::before {
display: block;
position: absolute;
content: "";
background-color: rgba(255,30,60,0.4);
transform: rotateZ(-3deg);
width: 102%;
height: 97px;
margin-left: -30px;
top: 2px;
}
Of course you can change the colors: your horizontal box is #91c6ff and the rotated one is #91c6ff. Also, they are transparent.
Here's a fork of your project: https://codepen.io/anon/pen/zdBVGe
And with the colors:
Make a element with another child element for text, span for example. Then you can set z-index on span so that text is above pseudo element.
div {
background: #91C6FF;
padding: 25px;
margin: 40px;
position: relative;
}
div:after {
content: '';
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
background: rgba(135, 171, 255, 0.7);
transform: rotate(-4deg);
}
span {
position: relative;
z-index: 2;
font-size: 30px;
}
<div><span>Lorem ipsum dolor.</span></div>