I tried to put a child div that will come under its parent and over the other elements.
.box1{
background-color: blue;
width: 500px;
height: 100px;
position: relative;
z-index: 3;
}
.box2{
position: absolute;
background-color: red;
width: 200px;
height: 100px;
left: 30%;
top: 20px;
z-index: 2;
}
.box3{
background-color: yellow;
width: 500px;
height: 100px;
z-index: 1;
}
<div class="box1">
<div class="box2"></div>
</div>
<div class="box3"></div>
I want to position the red rectangle to be under the blue and over the yellow. I put the z-index on three of them. However, it doesn't work.
What do you think about this? Thanks!
Update: Although the boxes are in the right order, however, the elements inside those boxes cannot be clicked now.
You can take a look at the error here: https://jsfiddle.net/p1xd6zah/
You can do a hack with z-index:
You can add z-index: -1 to box2. (stacks the child below the parent)
Add z-index: -2 and position: relative to box3 (now stack this behind box2)
Remove the z-index from box1 - see demo below:
.box1 {
background-color: blue;
width: 500px;
height: 100px;
position: relative;
}
.box2 {
position: absolute;
background-color: red;
width: 200px;
height: 100px;
left: 30%;
top: 20px;
z-index: -1;
}
.box3 {
background-color: yellow;
width: 500px;
height: 100px;
z-index: -2;
position: relative;
}
<div class="box1">
<div class="box2"></div>
</div>
<div class="box3"></div>
Related
I working a layout that changes the behavior of z-index.
Is this possible?
The yellow box is a dropdown menu. It should be inside the Red box.
Pretty much anything is possible with CSS3. However the element inside div 1 would need to be separate for this to work. If it's inside div 1 it will drag div 1 around with it. You'll get much more flexibility if the side div is on it's own
But for your specific example you would need something like:
HTML:
<div class="top"></div>
<div class="bottom"></div>
<div class="side"></div>
CSS:
.top {
width: 90%;
margin-left: 10%;
height: 200px;
height: 250px;
background: red;
}
.bottom {
width: 90%;
height: 200px;
height: 250px;
margin-left: 5%;
background: grey;
margin-top: -150px;
}
.side {
width: 20%;
height: 200px;
height: 250px;
margin-left: 78%;
background: yellow;
margin-top: -300px;
}
Working CodePen is here too: https://codepen.io/WebDevelopWolf/pen/mBLqxm
Not sure why this works, but it may be helpful for you:
#div1, #div2{
width: 100%;
height: 400px;
}
#div1{
background-color: red;
position: relative;
}
#div2{
background-color: green;
}
#div2{
margin-left: 50px;
margin-top: -300px;
position: relative;
}
#div1 > div{
background-color: yellow;
position: absolute;
width: 200px;
height: 200px;
right: 0;
top: 50px;
z-index: 2;
}
.as-console-wrapper{ display: none !important;}
<div id="div1">
DIV 1
<div>INSIDE DIV 1</div>
</div>
<div id="div2">
DIV 2
</div>
Here is all you need
div {
height: 100px;
width: 100px;
background: #ccc;
position: absolute;
top: 0;
left: 0;
}
.div1{
background: #f00;
}
.div2{
top: 30px;
}
.div_child{
background: #3a2525;
left: auto;
right: 0;
width: 50px;
z-index: 1;
}
<div class="div1">
1
<div class="div_child">
child
</div>
</div>
<div class="div2">
2
</div>
Why is the red div in front of the green div when I remove z-index from .wrapperRed?
It feels like z-index is inherited up the chain.
If I change the z-index of the green div to 6, it stays in front of the red one even after removing the line described in the first sentence.
.wrapperRed {
height: 200px;
width: 200px;
position: absolute;
z-index: 1; /* Why is the red div in front of the green one, if this z-index is deleted? */
}
.red {
position: absolute;
height: 100%;
width: 100%;
background-color: red;
z-index: 5;
}
.green {
height: 200px;
width: 200px;
background-color: green;
position: absolute;
top: 100px;
left: 100px;
z-index: 1;
}
<div class="wrapperRed">
<div class="red"></div>
</div>
<div class="green"></div>
When you remove z-index from .wrapperRed, the element defaults to z-index: auto.
In this case, both .red and .green participate in the same stacking context because positioned elements do not create a stacking context when z-index is auto (reference).
Learn more about z-index and stacking contexts here: Basics of the CSS z-index property
Why is the .red div in front of the green div when I remove z-index
from .wrapperRed?
Because .red no longer has a parental z-index to constrain it.
ie.
Before: .red has a z-index of 5 within a parental z-index of 1.
After: .red has a global z-index of 5.
N.B. In both Before and After cases, .wrapperRed is always behind .green. But, when it is unconstrained, .red (which is 100% the width and height of .wrapperRed) appears in front of .green.
You can see this more easily if you give the parent and child divs different background colours and make the child div smaller than the parent.
Compare:
.wrapperRed {
height: 200px;
width: 200px;
position: absolute;
background-color: red;
z-index: 1;
}
.yellow {
position: absolute;
height: 75%;
width: 75%;
background-color: yellow;
z-index: 5;
}
.green {
height: 200px;
width: 200px;
background-color: green;
position: absolute;
top: 100px;
left: 100px;
z-index: 1;
}
<div class="wrapperRed">
<div class="yellow">
</div>
</div>
<div class="green"></div>
with:
.wrapperRed {
height: 200px;
width: 200px;
position: absolute;
background-color: red;
}
.yellow {
position: absolute;
height: 75%;
width: 75%;
background-color: yellow;
z-index: 5;
}
.green {
height: 200px;
width: 200px;
background-color: green;
position: absolute;
top: 100px;
left: 100px;
z-index: 1;
}
<div class="wrapperRed">
<div class="yellow">
</div>
</div>
<div class="green"></div>
How to make the green div wrap around the blue and yellow divs (his children)
in this particular problem:
https://jsfiddle.net/y74ueuLa/
HTML
<div id="main">
<div id="one"></div>
<div id="two"></div>
</div>
<div id="footer"></div>
CSS
#main {
width: 100%;
background-color: green;
z-index: -2;
position: relative;
margin-bottom: 10px;
}
#one {
width: 100%;
height: 150px;
background-color: blue;
position: absolute;
z-index:-1;
}
#two {
position: relative;
top: 100px;
z-index:3;
width: 300px;
height: 500px;
background-color: yellow;
margin: 0px auto;
}
The green div is wrapped around the blue div. It just doesn't appear that way because the blue div is on top.
With div #two you're positioning it relatively with top 100px. When you position something relative, you're moving the visual component of the div relative to where it would naturally fall in the browser. It's equivalent to saying "visually move down 150px from where you are". You could just make the green div taller, but I don't think that's what you're going for.
I think what you're trying to do (and please correct me if I'm wrong), is this:
https://jsfiddle.net/dk6L1zLL/
#main {
width: 100%;
background-color: green;
z-index: -2;
position: relative;
margin-bottom: 10px;
padding-top:10px;
padding-bottom:10px;
}
#one {
//width: 100%;
height: 150px;
background-color: blue;
//position: absolute;
z-index:-1;
margin:0 10px 0;
}
#two {
//position: relative;
//top: 100px;
z-index:3;
width: 300px;
height: 500px;
background-color: yellow;
margin: 0px auto;
/*margin-bottom: 500px;*/
}
#footer {
height: 100px;
background-color: red;
width: 100%;
position: relative;
z-index: -3;
}
<body>
<div id="main">
<div id="one"></div>
<div id="two"></div>
</div>
<div id="footer"></div>
</body>
I got rid of a lot of the positioning rules and added some margin and padding.
There is an element that has 3 layer . For example:
book layer , adv layer and black layer
The div structure of them are
<div id="black"></div>
<div id="book">
<div id="adv"></div>
</div>
(black is absolute, book is relative, adv is absolute)
The problem is , if I would like the black layer is above book but under adv , how can I achieve this without changing the structure?
Got it:
See it on JSFiddle!
HTML
<div id="black"></div>
<div id="book">
<div id="adv"></div>
</div>
CSS
#black{
width: 60%;
height: 60%;
background: black;
position: absolute;
top: 20%;
left:20%
z-index:1;
}
#book {
width: 100%;
height: 400px;
background: pink;
z-index:-1;
}
#adv{
width: 20%;
height: 20%;
background: red;
position: absolute;
top: 40%;
left:40%;
z-index:2;
}
The Z-index highlights
#book{
z-index: -1;
}
#black{
z-index: 1;
}
#adv{
z-index: 2;
}
Your main problem will be aligning the elements unless you wrap them all in a div with position:relative
I have test it,i think this css help you to create that structure of layer as you want.
Background color is used for differentiate layers.
#black{width: 100px;
height: 100px;
background: red;
position: absolute;
top: 187px;}
#book
{
width: 585px;
height: 230px;
background: pink;
padding: 92px;
}
#adv{
width: 100px;
height: 100px;
background: red;
position: absolute;
top: 187px;
}
i have div area which is devided in to 4 equal parts, like the one atached.
now i need another div to be placed at the bottom area as an overlay to the above div. Imagine it like a text scroll area on the bottom side of the TV and the TV screen is constructed by 4 divs.
I am able to create the 5 divs. now the issue is that the 5th div(scroll area) is not going above the bottom edge of the 2 lower divs (3 and 4). I also had put z-index also but failed
can anybody share a sample for styling this.
You can solve it this way:
HTML:
<div class="area"></div>
<div class="area"></div>
<div class="area"></div>
<div class="area"></div>
<div class="overlay"></div>
CSS:
.area{
float: left;
width: 49%;
height: 300px;
border: 1px solid black;
}
.overlay{
width: 200px;
height: 100px;
background-color: blue;
clear: both;
position: absolute;
bottom: 30px;
margin: -100px;
left: 50%;
}
Please note that I have used hard coded example values. The actual values depends on which context the markup is in.
Without your code it's hard to figure what's not working.
If I understand what you want this is what I would have done:
<div class="container">
<div class="block1"></div>
<div class="block2"></div>
<div class="block3"></div>
<div class="block4"></div>
<div class="overlay"></div>
</div>
css:
.container {
position: relative;
width: 600px; /* use the size you want */
height: 400px;
}
.container div {
position: absolute;
width: 50%;
height: 50%;
}
.container .block1 { top: 0; left: 0; background: pink; }
.container .block2 { top: 50%; left: 0; background: red; }
.container .block3 { top: 0; left: 50%; background: green; }
.container .block4 { top: 50%; left: 50%; background: blue; }
.container .overlay {
position: absolute;
width: 80%;
height: 100px;
left: 10%;
bottom: 30px; /* distance from the bottom */
z-index: 1;
background: yellow;
}