z-index not working on relatively positioned elements - html

Given 2 elements positioned relatively inside a relatively positioned parent, how do I get the elements to respect their z-index?
HTML:
<div class="content">
<img src="http://placekitten.com/100/100" alt="" class="preview">
<img src="http://placekitten.com/200/200" alt="" class="full">
</div>
CSS:
.content {
position:relative;
z-index:1;
outline:1px solid blue;
}
.preview {
z-index:1;
position:relative;
outline:1px solid yellow;
}
.full {
z-index:2;
position:relative;
outline:1px solid green;
}
JSFiddle: http://jsfiddle.net/2Nz2g/1/
I'm trying to get the .full element to be placed overtop the .preview element, so that basically .full elements position isn't affected by the .preview element at all.
I've tried floating the elements to no avail. Positioning absolutely is not an option as it totally throws off the position. Setting top:0;left:0 also has no effect.

I've had some cases where if an element is rendered after another, some browsers may ignore the z-index and draw them as they would do normally. In this cases you should set the z-index to a negative (<0) value to force them to be under the next elements:
Take a look at this fiddle, where all z-indexes are positive:
HTML:
<div id="first"></div>
<div id="second"></div>
CSS:
#first {
width: 200px;
height: 100px;
background: red;
z-index: 10;
}
#second {
width: 200px;
height: 100px;
background: blue;
position: relative;
top: -30px;
left: 40px;
z-index: 1;
}
Here the second div is rendered on top of the first one no matter what z-index I specify.
But take a look at this one:
HTML:
<div id="first"></div>
<div id="second"></div>
CSS:
#first {
width: 200px;
height: 100px;
background: red;
z-index: 10;
}
#second {
width: 200px;
height: 100px;
background: blue;
position: relative;
top: -30px;
left: 40px;
z-index: -1;
}
As you can see I only set the second element's z-index to -1 and now it's rendered properly.
This was tested on Google Chrome 35. You may want to check this bug on other browsers.

Is possible for you set a top / left /rigth positions to move the div
Css:
.content {
position:relative;
z-index:1;
outline:1px solid blue;
}
.preview {
z-index:1;
position:relative;
outline:1px solid yellow;
}
.full {
top:20px;
right:20px;
z-index:2;
position:relative;
outline:1px solid green;
}
http://jsfiddle.net/2Nz2g/4/

This can be fixed by using position: relative on the parent div and position: absolute on the images. This forces the images to be aligned on top of each other
JSFiddle: http://jsfiddle.net/yL5Sf/

Related

Make a div float within another Div

I need a div to float within another div. Tried using position: fixed, but the div floats beyond the parent div now.
Here is the sample code.
I need the "Div to Float" to float inside "Div 1". now it floats outside 'Div 1' and go behind 'Div 2'
Here is the code.
.wrapper {<!--from www .j av a2s.c o m-->
width:100%;
height: 200px;
overflow-y: scroll;
}
.container {
width: 301px;
margin: 0px auto;
height: 1501px;
background: green;
position: relative;
}
.element {
background:yellow;
position:fixed;
width:101px;
height:71px;
top:51px;
right:0px;
left:769px;
border:2px solid blue;
}
<div class="wrapper">
<div class="container">
Div 1
<div class="element">Div to float</div>
</div>
</div>
<div class="container" style="margin-top: 30px; background: purple">Div 2</div>
What I've tried?
.wrapper {<!--from www .j av a2s.c o m-->
width:100%;
height: 200px;
overflow-y: scroll;
}
.container {
width: 301px;
margin: 0px auto;
height: 1501px;
background: green;
position: relative;
}
.element {
background:yellow;
position:fixed;
width:101px;
height:71px;
top:51px;
right:0px;
left:769px;
border:2px solid blue;
}
<div class="wrapper">
<div class="container">
Div 1
<div class="element">Div to float</div>
</div>
</div>
<div class="container" style="margin-top: 30px; background: purple">Div 2</div>
What I've expected?
I need the "Div to Float" to float inside "Div 1".
What is the result now?
Now it floats outside 'Div 1' and go behind 'Div 2'
.container {
position:relative;
}
.element{
position:absolute;
}
I don't fully understand what you mean by "float", but this code will place your div.element inside div.container
Position: Fixed
position: fixed; is positioning the element relative to the viewport, which means it always stays in the same place even if the page is scrolled.
Position: Sticky
position: sticky; is positioning the element relative until a given offset position is met in the viewport - then it "sticks" in place. When the user scrolls past the parent div, the element will stay with its parent.
Read more about Layout positioning
.wrapper {
width: 100%;
height: 200px;
overflow-y: scroll;
position: relative;
}
.container {
width: 301px;
margin: 0px auto;
height: 1501px;
background: green;
position: relative;
z-index: 2;
}
.second {
z-index: 0;
}
.element {
background: yellow;
position: sticky;
width: 90%;
height: 80px;
top: 50px;
right: 0px;
left: 769px;
border: 2px solid blue;
}
.fixed {
position: fixed;
top: 50px;
left: 0;
width: 50%;
z-index: 1;
}
<div class="wrapper">
<div class="container">
Div 1
<div class="element">I am 50px away from the top of my green parent, and I will stop being sticky when document gets scrolled away from my parent.</div>
</div>
<div class="fixed" style="margin-top: 30px; background: red">I am just gonna stay in this place forever cause I'm fixed. Using z-index on me or the elements will control whether I'm above or below any other elements.</div>
</div>
<div class="container second" style="margin-top: 30px; background: purple">Div 2</div>

applying z-index on inner div to appear on top of an overlay

I'm trying to overlay an entire page except for one div with a transparent overlay. The problem is that the div I want on top is a child of a fixed div. How do I make it on top, but leave its parent under the overlay?
In the below code, the element I want on top of the overlay is .holder. Here's a JS Fiddle.
HTML:
<!DOCTYPE html>
<html>
<head>
<title>z-index tests</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="root">
Root
<div class="header">
Header
<div class="holder">
Holder
</div>
</div>
<div class="content">
content
<div class="box"></div>
</div>
</div>
<div class="overlay"></div>
</body>
</html>
CSS:
.box {
height: 100px;
width: 100px;
border: 1px solid #000;
}
.root {
display: block;
background-color: grey;
}
.header {
background-color: red;
position: fixed;
top: 0px;
width: 1600px;
}
.holder {
background-color: green;
height: 60px;
width: 1600px;
z-index: 1002;
position: absolute;
}
.content {
background-color: blue;
}
.overlay {
position: fixed;
top:0;
bottom:0;
left:0;
right:0;
background-color: rgb(50,50,50);
opacity:0.8;
z-index:10;
}
You cannot. That's how z-index and stacking layers work. Until .holder is descendant of .header which creates it's own stacking context through position: fixed, the only way to push .holder above the overlay is to move it in DOM tree outside .header and make it a sibling or descendant of .overlay.
The second option (but I guess .header is not fixed without reason) is to change styles of .header to not create stacking context, ex. change it's position to absolute and remove z-index. In that case stacking context of .holder will be on equal level with .overlay's and you would be able to manipulate depth of those with z-index.
Look at list of CSS properties creating new stacking context.
I know I'm leaving you inconsolable.
You could cheat and achieve the same effect by adding a massive box-shadow to the .holder:
.box {
height: 100px;
width: 100px;
border: 1px solid #000;
}
.root {
display: block;
background-color: grey;
}
.header {
background-color: red;
position: fixed;
top: 0px;
width: 1600px;
/*z-index: 1;*/
}
.holder {
background-color: green;
height: 60px;
width: 1600px;
z-index: 1002;
position: absolute;
}
.content {
background-color: blue;
}
.highlight {
box-shadow:0 0 0 9999999px rgba(50,50,50,0.8);
}
<div class="root">
Root
<div class="header">
Header
<div class="holder highlight">
Holder
</div>
</div>
<div class="content">
content
<div class="box"></div>
</div>
</div>
JS Fiddle

CSS: Why h3 drops down its parent div?

There is a simple example where a div element contains h3.
But the h3 element drops down its parent div when h3 has position relative.
Changing h3 position to absolute solves this problem.
What is the reason?
.personal-details{
background-color: green;
}
.personal-image{
display: inline-block;
width: 150px;
height: 150px;
background-color: white;
}
.personal-description {
display: inline-block;
width: 150px;
height: 150px;
background: black;
}
.personal-description h3 {
position: relative; /*absolute solves the problem*/
}
<div class="personal-details">
<div class="personal-image"></div>
<div class="personal-description"><h3 class="name">My Name</h3></div>
</div>
This is caused by the default vertical-align: baseline; property of inline-block elements.
Overriding the default with vertical-align: top for your element will get you somewhere like correct:
.personal-details {
background-color: green;
vertical-align: middle
}
.personal-image {
display: inline-block;
width: 150px;
height: 150px;
background-color: green;
}
.personal-description {
display: inline-block;
width: 150px;
height: 150px;
background: black;
vertical-align: top;
}
.personal-description h3 {
position: relative;
background: yellow;
}
<div class="personal-details">
<div class="personal-image"></div>
<div class="personal-description"><h3 class="name">My Name</h3></div>
</div>
Notice I say "somewhere like correct" as you will still have issues with space around the elements (notice the gap below the black square and space between the two child divs). But that is out of the scope of your question and has been dealt with many times before.
.personal-details{
background-color: red;
}
.personal-image{
display: inline-block;
width: 150px;
height: 150px;
background-color: green;
margin:0;
}
.personal-description {
float:left;
display: inline-block;
width: 150px;
height: 150px;
background: black;
margin:0;
padding:0;
}
.personal-description h3 {
margin:0;
background-color:blue;
padding:0;
position: relative; /*absolute solves the problem*/
}
<div class="personal-details">
<div class="personal-image"></div>
<div class="personal-description"><h3 class="name">My Name</h3></div>
</div>
May be your are familiar with all the positioning.Firstly, you need to understand about it.There are four possible useful positioning in css which are given below.
Static
Relative
Absolute.
Fixed
-Static positioning:
It is basically a default position of every element or tag, use of this position will never effect on your element’s state or position.In static we can not use top,left, bottom & right properties.
position:static;
-Relative:
Relative positioning,makes element or tag movable.Yes, we can move it any where on container.By default it works like an static but we can use left,top,bottom & right in it.
position: relative;
top:50px;
left:50px;
-Absolute:
Absolute positioning, get the space according to browser window or container(that may be parent or ancestor) window.If container window’s position set to relative than absolute will get the position according to container.
position:absolute;
left:0px;
right:0px;
Task: Now, make a parent div and it’s two child's and check both relative and absolute.
/* Example */
</div>
<div class='box2'>
<h3>Here my name</h3>
</div>
</div>
.parent_box{
background-color:grey;
margin-top: 20px;
}
.box1{
height:200px;
width: 200px;
background-color:red;
display: inline-block;
}
.box2{
height: 200px;
width: 200px;
background-color:yellow;
display: inline-block;
position: relative;
}
.box2 h3{
position: absolute;
/* Working according to it's parent because it's parent div contains relative position now check it by given it left top and remove the position relative of box2*/
}

z-index not working inside position fixed element

I have div which is position is fixed and another div inside the position fixed div which is position relative
<div class="wrap">
<div class="content">
</div>
</div>
css is:
.wrap{
position: fixed;
width:80%;
height:200px;
background-color:#a157df;
z-index:5;
}
.content{
position: relative;
width:30%;
height:100px;
background-color:#a1a7af;
left:80%;
z-index:1;
}
example is in this link:
how to place 2nd div inside 1st one, i used z-index but its not working
I do not think you need the z-index. How about this?
.wrap {
position: fixed;
width: 80%;
height: 200px;
background-color: #a157df;
}
.content {
position: relative;
width: 30%;
height: 100px;
background-color: #a1a7af;
left: 70%;
}
This is how the z-index works if you need it. http://www.w3schools.com/cssref/pr_pos_z-index.asp

Set width of relative parent div to width of fixed child div

I have a relative parent div and a fixed child div. i would like the relative parent to get the width from the child div.
<div id="parent">
<div id="child">
</div>
</div>
Css Code;
#parent {
position: relative;
top: 40px;
left: 20px;
background-color: #F1A323;
padding: 20px;
box-shadow: 10px 10px 5px #0F0F0F;
border:2px solid;
border-radius:25px;
-webkit-border-radius: 25px;
behavior: url(pie/PIE.htc);
}
#child{
position: fixed;
background-color: #FDF0DA;
height: 100px;
}
#parent{width:500px;}
#child{width:100%}
It may help.
Use the following:
#child{
width:100%;
}
If that doesnot work, use:
#child{
min-width:100%;
max-width:100%;
}
Give the width to parent div(which you want to give to the child div)
#parent {
width: 650px;
}
#child {
width: 100%;
}
the parent and child div are always equal.