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/
No, z-index cannot be applied to a background image.
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.
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.
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).
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).
just try,
.container .gray { width: 420px; height: 315px; position: absolute; margin-left: 5px; margin-top: -315px; z-index: -10; background-color: gray; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With