I've got a "Dialog" widget that pops up with a z-index of 100. When I create another popup (a floating div), it appears underneath the dialog widget, because I haven't explicitly set the z-index on the new popup.
The structure ends up looking something like
<div id="dialogbox" style="z-index: 100">
<div>
<div id="widgetThatCausesThePopup" />
</div>
</div>
<div id="popupHiddenBehindTheDialog" />
I'm using GWT to generate this HTML. There can be arbitrary levels of nesting between dialogbox and widgetThatCausesThePopup, and the actual z-index may be arbitrary as well.
How can I ensure that the new div will be shown in front of the dialogbox?
The natural CSS solution is to:
z-index
to something else thanauto
,position
to eitherrelative
,absolute
orfixed
.In that case, your popup doesn't need a
z-index
at all. This completely avoids the "z-index hell".Example:
The stacking context even allows you to use z-indexes relative to the context, if you need them (note, that the child order doesn't matter, and the z-indexes don't have to be larger than 100):
Get the computed z-index of the parent (see In GWT how to know all the styles applied to a given element (by id or class name)) and increment it for each child.
If your new dialog windows are inserted in the DOM after the previous ones:
You can set the
z-index: 100
on all dialog windows. When elements with the samez-index
are found, order in the DOM determines which is on top.