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
Related
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
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;
}
I'm trying to create a list of items where each item in the list contains essentially two columns ... the left column some text, and the right column 2 buttons for yes/no. I want the two buttons on the right to be vertically aligned with the text. For aesthetic reasons, I want a min-height on the list item. I finally figured out that a floating div must be inside an absolute div for the 100% height to work. The problem is now that I have an absolute div inside my original relative div, it no longer expands to accommodate text longer than min-height. I've read so many articles and tried so many different combinations of height/relative/absolute/float/clear/overflow and nothing has worked for my situation. Is there a solution to this?
In my example here http://jsfiddle.net/THBFY/4/ I need the red box to be the same height as the blue box so that the vertical align works.
<div class="list_container">
<div class="list_item">
<div class="item_text">
My text in this item. This could be a variable length creating a div ranging from about 75-150px in height. This is a lot of text to make it longer although I am not really saying anything here. It is only to make the blue box taller than the red box.
</div>
<div class="item_buttons">
<div class="buttons_inner">
<div class="button button_yes">Y</div>
<div class="button button_no">N</div>
</div>
</div>
</div>
</div>
.list_container { position: relative; width: 400px; }
.list_item { position: relative; min-height: 70px; overflow: hidden; border: #000000 solid 1px; }
.item_text { float: left; width: 340px; background-color: #0066BB }
.item_buttons { display: table; float: right; width: 50px; height: 100%; background: #FF0000; }
.buttons_inner { display: table-cell; vertical-align: middle; }
.button { display: block; margin-left: auto; margin-right: auto; height: 40px; width: 40px; background-repeat: no-repeat; }
.button_yes { background-image: url("images/yes.gif") }
.button_no { background-image: url("images/no.gif") }
When I add in the inner div with position:absolute http://jsfiddle.net/THBFY/5/ the problem is the height no longer increases to show all of the text.
<div class="list_item_inner">...
.list_item_inner { position: absolute; height: 100%; }
But if I now change the min-height of the outer div from 70 to 200 http://jsfiddle.net/THBFY/6/, you can see that the 100% height on the red box is in fact working, so my problem is either in the first situation without the absolute position, I need the red box to stretch, or in the 2nd situation with the absolute div, I need the container to stretch.
HTML:
<div class="list_container">
<div class="list_item">
<div class="item_text">My text in this item. This could be a variable length creating a div ranging from about 75-150px in height. This is a lot of text to make it longer although I am not really saying anything here. It is only to make the blue box taller than the red box.
</div>
<div class="item_buttons">
<div class="buttons_inner">
<div class="button button_yes">Y</div>
<div class="button button_no">N</div>
</div>
</div>
</div>
</div>
CSS:
.list_container { position: relative; width: 400px; }
.list_item { border: #000000 solid 1px; display:table; }
.item_text { display:table-cell; width: 340px; background-color: #0066BB }
.item_buttons { display:table-cell; width: 50px; background: #FF0000; }
.button { display: block; margin-left: auto; margin-right: auto; height: 40px; width: 40px; background-repeat: no-repeat; }
.button_yes { background-image: url("images/yes.gif"); }
.button_no { background-image: url("images/no.gif"); }
fiddle
http://jsfiddle.net/aN57Q/1/
When I zoom my page, an element will eventually stretch it and when it does you get a situation much like the one in the jsFiddle above.
The blue element represents the element stretching the page, it cannot be changed the rest is fair game. I need the red boxes to fill the page appropriately.
Thanks so much in advance!
<div class="a">A</div>
<div class="b">B</div>
<div class="c"></div>
<div class="somethingBig">Blah!</div>
div {
height: 50px;
background-color: red;
margin-bottom: 10px;
font-family: Verdana;
color: white;
font-size: 30px;
line-height: 50px;
}
.a { width: 100%; }
.b { width: 100vw; }
.c { width: 100%; }
.somethingBig {
width: 600px;
background-color: blue;
}
Okay, this is the same thing that happens when the browser window is smaller, such that the red bars are not as wide as the blue bar. They are only going 100% of the body, which becomes smaller than the overflowing blue.
You can do min-width:600px; on your "div" CSS.
I'm not sure how to make this work crossbrowser-wise, so I need some of your expertise ;)
How do I make styling that looks like this and works crossbrowser-wise? (IE7 as well)
http://imageshack.us/photo/my-images/543/examplek.jpg/
The red box has a fast defined width
The green box is centered inside the red box and has a dynamic width + a padding/border
The blue box is a "mouseover" div which needs to have the same width as the green box (without the padding/border)
Here is one way to achieve this (with a dynamic width for green box): http://jsfiddle.net/nKdt6/
HTML
<div class="outer">
<div class="inner">
<p>
lorem ipsum
<p>
<div>
<p>Blah blah blah</p>
</div>
</div>
</div>
CSS
.outer {
background-color : red;
text-align: center;
width: 500px;
}
.inner {
background-color: lime;
border: 3px black solid;
display: inline-block;
padding: 20px;
*display: inline;
*zoom: 1;
position: relative;
margin: 100px 0;
border-radius: 10px;
overflow: hidden;
}
.inner > div {
display: none;
background-color: aqua;
position: absolute;
bottom: 0;
left: 0;
right: 0;
}
.inner:hover > div {
display: block;
}
To center the .inner element when it has a dynamic width we can use text-align: center in .outer and display: inline-block in .inner. I have added the extra CSS *display: inline and *zoom: 1 to make this work in IE7 as it does not support display: inline-block.
Edit
To get a thin black outline (outer border) around a wide white inner boder (as achieved and demonstrated by #DonPedro in the comments below), you can add a second border to an inner child element that controls the full height and width of the parent element. In the example above, this is .inner > p.
CSS
.inner {
...
border: 1px black solid;
...
}
.inner > p {
...
border: 10px solid white;
...
}
Working JSFiddle: http://jsfiddle.net/nKdt6/1/ (provided by #DonPedro)
This cannot be achieved using outline due to the border-radius styling, and as far as I am aware Mozilla is the only browser that supports any type of outline radius (-moz-outline-radius).