Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to force an element to the top when z-index is not working?

Tags:

css

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>

enter image description here

like image 954
camino Avatar asked Aug 04 '13 02:08

camino


People also ask

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.

How do you move an element to the top?

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.

Will Z Index work for an element without 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.

How do I make an element on top in CSS?

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.

Does Z index only work with absolute positioning?

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).


1 Answers

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/

like image 98
Matthew Cox Avatar answered Sep 23 '22 21:09

Matthew Cox