Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Z-index not working as expected working with picture and background-div

Tags:

I have a image, and I'd like to have an effect on my page where a background-div with gray color is displayed 5px on the left side and bottom of the picture. But for some reason, I can't get the background-div to align behind the picture. It always lays ontop. How can I fix this?

Jsfiddle: http://jsfiddle.net/3Z7m4/2/

like image 524
OptimusCrime Avatar asked Nov 15 '11 12:11

OptimusCrime


People also ask

Does Z Index Working with background image?

No, z-index cannot be applied to a background image.

Why Z index is not working with div?

z-index only works on positioned elements (relative, absolute, fixed, sticky) so if you set a z-index on an element with a static position, it won't work.

What do you do when Z index is not working?

To sum up, most issues with z-index can be solved by following these two guidelines: Check that the elements have their position set and z-index numbers in the correct order. Make sure that you don't have parent elements limiting the z-index level of their children.

Does Z Index work with position fixed?

Note: z-index only works on positioned elements (position: absolute, position: relative, position: fixed, or position: sticky) and flex items (elements that are direct children of display:flex elements).


2 Answers

Add position: relative; into the img CSS.

See: http://www.w3schools.com/cssref/pr_pos_z-index.asp

Note: z-index only works on positioned elements (position:absolute, position:relative, or position:fixed).

like image 163
Jarno Argillander Avatar answered Sep 18 '22 13:09

Jarno Argillander


just try,

.container .gray {     width: 420px;     height: 315px;     position: absolute;     margin-left: 5px;     margin-top: -315px;     z-index: -10;     background-color: gray; } 
like image 41
anglimasS Avatar answered Sep 19 '22 13:09

anglimasS