Why does green div come on top of red div? - css

https://codepen.io/anon/pen/dBdaWE
In the codepen above I have 2 divs red and blue. Using z-index we make sure red is above blue even if it comes before blue in markup.
green is a child of blue with z-index of 9999. Even though it's z-index is high it should be trapped inside blue which is clearly below red. As said in the CSS tricks article:
https://css-tricks.com/almanac/properties/z/z-index/
Also note that nesting plays a big role. If an element B sits on top of element A, a child element of element A can never be higher than element B.
How does green div, which is a child of blue is able to come on top of red div?

!! note, all quotes here below with a * at the end are from this source
Also note that nesting plays a big role. If an element B sits on top of element A, a child element of element A can never be higher than element B.*
How does green div, which is a child of blue is able to come on top of red div?
You have probably mis-interpret that sentence. It is meant for that situation where none of the elements have z-index set. If you style the elements without setting the z-index, it holds the truth. Look at the interactive example here below, which is without modifying the z-index of the elements.
.main {
border: 1px solid;
padding-left: 50px;
}
.red, .blue {
width: 100px;
height: 100px;
}
.red {
background-color: red;
position: relative;
top: 50px;
left: -50px;
}
.blue {
background-color: blue;
}
.green {
width: 50px;
height: 50px;
position: relative;
top: -25px;
background-color: green;
}
<div class="main">
<div class="red"></div>
<div class="blue">
<div class="green"></div>
</div>
</div>
As you can see, the following statement is true
Without any z-index value, elements stack in the order that they appear in the DOM (the lowest one down at the same hierarchy level appears on top)*
However, it is not obvious first because blue's position is static, in contrary to the other two elements, whose position are relative (thus non-static). If you expect that blue is above red (and below green), then you have to change its position CSS attribute. It is also mentioned in the link, as quoted here below
Elements with non-static positioning will always appear on top of elements with default static positioning.*
In the example here below, I have given the blue element (look for "ADDED") a non-static position value. This leads to a similar behavior as when all element's position are static: red comes first, then blue comes on top of it, followed by green on top of it because it is a child of blue (lower in hierarchy).
.main {
border: 1px solid;
padding-left: 50px;
}
.red, .blue {
width: 100px;
height: 100px;
}
.red {
background-color: red;
position: relative;
top: 50px;
left: -50px;
}
.blue {
background-color: blue;
position: relative; /* !! ADDED !! */
}
.green {
width: 50px;
height: 50px;
position: relative;
top: -25px;
background-color: green;
}
<div class="main">
<div class="red"></div>
<div class="blue">
<div class="green"></div>
</div>
</div>
Now, back at the first quote;
Also note that nesting plays a big role. If an element B sits on top of element A, a child element of element A can never be higher than element B.*
This occurs when you are only giving the element B (in this situation, it is .red) a z-index value. In the example here below, I have added a z-index value to the red element.
.main {
border: 1px solid;
padding-left: 50px;
}
.red, .blue {
width: 100px;
height: 100px;
}
.red {
background-color: red;
position: relative;
top: 50px;
left: -50px;
z-index:1; /* !! ADDED !! */
}
.blue {
background-color: blue;
position: relative;
}
.green {
width: 50px;
height: 50px;
position: relative;
top: -25px;
background-color: green;
}
<div class="main">
<div class="red"></div>
<div class="blue">
<div class="green"></div>
</div>
</div>
See, the green element does not appear anymore. This is because red is above blue. And green is a part of the blue.
In yours question, you have given green another z-index value. This will overrule the current behavior so that it becomes above the red element as shown here below.
.main {
border: 1px solid;
padding-left: 50px;
}
.red, .blue {
width: 100px;
height: 100px;
}
.red {
background-color: red;
position: relative;
top: 50px;
left: -50px;
z-index:1;
}
.blue {
background-color: blue;
position: relative;
}
.green {
width: 50px;
height: 50px;
position: relative;
top: -25px;
background-color: green;
z-index: 2; /* !! ADDED !! */
}
<div class="main">
<div class="red"></div>
<div class="blue">
<div class="green"></div>
</div>
</div>

Look at your CSS file. The z-index of .green is 9999.
EDIT: When z-index is auto, no stacking context is created. So red and blue have the same stacking context. This is why the children of blue doesn't work as expected of a nested element with a lower z-index.
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context

Related

CSS margin-top moving element down

I have a context of 3 divs, one parent, and two children.
The two children are placed one on top of the other and I want to add a margin-top on the bottom one to move the one on top 50px up.
What ends up happening is that the one on the bottom moves down 50px instead.
Here is the code:
.container {
background-color: red;
width: 500px;
height: 300px;
position: relative;
margin: auto;
font-size: 30px;
}
.top,
.bottom {
height: 100px;
width: 100px;
}
.top {
background-color: purple;
}
.bottom {
margin-top: 50px;
background-color: blue;
}
<body>
<div class="container">
<div class="top">top</div>
<div class="bottom">bottom</div>
</div>
</body>
Any suggestions?
CSS allows you to move an element relative to its position without affecting other elements' positions if you use transform.
In this case you can translate the top element in the Y direction by -50px to move it up:
.container {
background-color: red;
width: 500px;
height: 300px;
position: relative;
margin: auto;
font-size: 30px;
}
.top,
.bottom {
height: 100px;
width: 100px;
}
.top {
background-color: purple;
transform: translateY(-50px);
}
.bottom {
background-color: blue;
}
<body>
<div class="container">
<div class="top">top</div>
<div class="bottom">bottom</div>
</div>
</body>
gap (grid-gap) Syntax
gap: 50px;
As you can see the first element is already on the highest point inside the parent container.
html
What you can do is in case you want to increase its height is scaling its y position by a negative number.

Making an overlay appear over a parent div, but below that div's child

I want an output like this:
The actual structure is:
<div class="container"> <!-- green -->
<div class="overlay"> <!-- purple -->
</div>
<div class="parent"> <!-- red -->
<div class="child"> <!-- lightblue -->
Child
</div>
</div>
</div>
I have achieved it after half an hour of struggle via this CSS:
.container {
position: relative;
width: 500px;
height: 500px;
background: green;
z-index: 1;
}
.overlay{
background: purple;
position: absolute;
width: 150px;
height: 150px;
z-index: 2;
}
.parent {
position: relative;
background: red;
width: 250px;
height: 250px;
}
.child {
z-index: 100;
background: lightblue;
width: 50px;
height: 50px;
position: relative;
}
(Fiddle demo)
What I do not understand is: why is it not allowed to set .parent's z-index value to anything? Because doing so immediately breaks this order. If I set .parent{z-index: a;} where a is an integer <= 1, then the purple overlay comes over the child, but if a>=2, then .child and .parent both come over .overlay.
But why does it break anything at all? I would at-least expect z-index: 0; to not break anything, since it is the ground level kind of thing. What am I missing?
Also, if I am working on a webpage where the z-index of .parent cannot be changed from its given integer value, because of other constraints of the page, is there any other way to insert an .overlay between the .parent and its .child? (the positioning and z-index of the .overlay is in my hands)

How can you horizontally center an absolutely positioned element relative to the viewport when it has a positioned parent?

Eg. how can you get the blue child in this example to be horizontally centered relative to the viewport (ie. in the center of the page), provided that the parent must stay the same.
Other qualifications:
I don't want it to be fixed.
Suppose that distance between the parent and the left viewport is unknown.
.parent {
margin-left: 100px;
margin-top: 100px;
background: red;
position: relative;
width: 50px;
height: 50px;
}
.child {
background: blue;
position: absolute;
bottom: 100%;
}
<div class="parent">
<div class="child">
child
</div>
</div>
I am trying to make this question a SSCCE. In reality, the use case is that I have a dropup (like dropdown, expect it appears above rather than below the triggering button). I want the dropup menu to be centered.
The menu needs to be absolutely positioned, otherwise it'd get in the way of the flow of other DOM elements. And I need to position the container so that I could set bottom: 100%; on the menu so that it appears right above the triggering button.
In in this case you can use position:fixed BUT to avoid it being fixed apply a null transform to the body:
body {
transform:translate(0,0);
min-height:150vh;
}
.parent {
margin-left: 100px;
margin-top: 100px;
background: red;
position: relative;
width: 50px;
height: 50px;
}
.child {
background: blue;
position: fixed;
left:50%;
transform:translate(-50%,-100%);
}
<div class="parent">
<div class="child">
child
</div>
</div>

negative top value makes parent div higher

I try to position a div relative to its parent with a negative top value. This works fine, the problem is now that this div, even it has a negative top value makes the parent div bigger.
What could I do to make the parent div not bigger?
Here's a fiddle.
I do not want that the .innera-rel div makes the .a div bigger (I dont want to see the red at the bottom).
HTML:
<div class="a">
<div class="innera">
blah blah blah
</div>
<div class="innera-rel">
test
</div>
</div>
CSS:
.a {
background: red;
}
.innera {
height: 80px;
background: blue;
}
.innera-rel {
border: 1px solid gray;
background: white;
position: relative;
z-index: 100;
right: -50px;
top: -38px;
width: 130px;
}
The red space you see is the space that the .innera-rel would take up if it was not positioned. This space stays "occupied", you just move the div around relative to that space. If you do not want this to happen you have to use absolute positioning.
Just give the same height to the .a
.a {
background: red;
height: 80px;
}
.innera {
height: 80px;
background: blue;
}
.innera-rel {
border: 1px solid gray;
background: white;
position: relative;
z-index: 100;
right: -50px;
top: -38px;
width: 130px;
}
}
<div class="a">
<div class="innera">
blah blah blah
</div>
<div class="innera-rel">
test
</div>
</div>
.a {
background: red;
height:80px;
}
.innera {
height: 80px;
background: blue;
}
Make the .a height same as the .innera height
This will make the red bar disappear:
.a {
display: inline;
}

How to change the layer order of divs

I have 2 divs with the following class names: "red" and "green"
The green div is hidden but when the mouse hovers over the red div, the green div appears.
However, the green div is appearing in front of the red one. I would like to reverse the order so that when hovering over the red div, the green one appears beneath it i.e. I want the red div to overlap the green div (I don't simply want a space between the 2 divs, I want the red div to be over the green one). I actually want to include a background PNG inside the red div so that when the green div appears it is actually underneath the PNG.
The green div is nested inside the red div and my CSS is as follows:
.green{
display: none;
}
.red:hover .green{
display: block;
}
you can change the order of layers with z-index
if you always want the red on to be on top and not put some text on it make it like this:
z-index:9999;
if you want to know more about it, use this tutorial: z-index
I don't see how you can hover over the red div since it's nested inside the green div and that one is hidden, therefor the red div should be hidden too.
But I changed some things.
I used position, z-index and nested the green inside the red div.
HTML
<div class="red">
<div class="green">
</div>
</div>
CSS
.green {
display: none;
background: green;
width: 310px;
height: 310px;
margin-left: -5px;
position: relative;
z-index: -1;
}
.red {
background: red;
width: 300px;
height: 300px;
margin: 5px;
}
.red:hover .green {
display: block;
}
I made the size of the green div a little bigger and added some margin so it would be visible.
Here's a live example http://jsfiddle.net/PaulvdDool/FpsuA/
HTML
<div class="wrapper">
<div class="red"></div>
<div class="green"></div>
</div>
CSS
.wrapper {
position: relative;
height: 50px;
width: 50px;
}
.red {
top:0;
left: 0;
position: absolute;
height: 100%;
width: 100%;
background-color:#d42046;
z-index:2;
}
.green {
top: 0;
left: 0;
position: absolute;
display: none;
height: 60px;
width: 60px;
background: #1cc477;
z-index:1;
}
.wrapper:hover .green {
display: block;
}
DEMO:
jsFiddle

Resources