Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use z-index with relative positioning?

I have a problem with z-index and my code. I want to have a popup on every row, positioned relative to that row. So I created this code:

<div class="level1">     <div class="level2">         <input type="text" value="test1" />         <div class="popup">test1</div>     </div>     <div class="level2">         <input type="text" value="test2" />         <div class="popup">test2</div>     </div> </div> 

with te following style

.level1 {     position:relative;     z-index:2; } .level2 {     position:relative;        z-index:3; } .popup {     position:absolute;     left:0px;     top:10px;     width:100px;     height:100px;     background:yellow;     z-index:4; } 
like image 601
Chris Vaarhorst Avatar asked Jan 24 '12 11:01

Chris Vaarhorst


People also ask

How do you use Z index without absolute positioning?

Yes: use position:relative; z-index:10 . z-index has no effect for position:static (the default).

Does Z Index work with static positioning?

z-index only affects elements that have a position value other than static (the default). Elements can overlap for a variety of reasons, for instance, relative positioning has nudged it over something else. Negative margin has pulled the element over another. Absolutely positioned elements overlap each other.

What is Z index relative to?

The z-index of elements inside of a stacking context are always relative to the parent's current order in its own stacking context. The <html> element is a stacking context itself and nothing can ever go behind it.

What is the default Z index value in positioning elements?

Since the default z-index value for page elements is 0, elements with a negative z-index value appear behind elements without a set z-index value.


1 Answers

When you set position: relative on an element then you establish a new containing block. All positioning inside that block is with respect to it.

Setting z-index on an element inside that block will only alter its layer with respect to other elements inside the same block.

I'm not aware of any work-arounds.

like image 142
Quentin Avatar answered Oct 01 '22 11:10

Quentin