I am writing a chrome extension which using injection to modify the element in target webpage. I want to show a message bubble when cursor move on an element. But I encounter a problem that sometimes the message bubble is hidden by some other elements. I found that it was because z-index will not work if an element B sits on top of element A, a child element of element A(message bubble) can never be higher than element B.
To demonstrate it,Here is an example. That obj2 will always be hidden by obj3, although obj2's z-index is 1000000.
Is there any solutions for it?
.aaa{
background-color:blue;
width:100px;
height:100px;
}
.ccc{
background-color:red;
width:80px;
height:80px;
z-index:1000000;
}
.bbb{
background-color:green;
position:relative;
top:-50px;
left:50px;
width:100px;
height:100px;
}
<div>
<section id=obj1 class="aaa">
<article id=obj2 class="ccc">
</article>
</section>
</div>
<section id=obj3 class="bbb">
</section>
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.
If position: absolute; or position: fixed; - the top property sets the top edge of an element to a unit above/below the top edge of its nearest positioned ancestor. If position: relative; - the top property makes the element's top edge to move above/below its normal position.
z-index only works on positioned elements. If you try to set a z-index on a non-positioned element, it will do nothing. However, there is one exception - flex children can use z-index even if they're non-positioned.
Using CSS position property: The position: absolute; property is used to position any element at the absolute position and this property can be used to stack elements on top of each other. Using this, any element can be positioned anywhere regardless of the position of other elements.
An element with greater stack order is always in front of an element with a lower stack order. 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).
z-index is only applied when the container is also marked as position: relative/absolute. If you change your ccc
rule to the following it will work correctly:
.ccc{
position: relative;
background-color:red;
width:80px;
height:80px;
z-index:1000000;
}
http://jsfiddle.net/RrUbh/
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