Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iframe preventing z-index from working?

I've been trying to use the z-index css attribute to make one element always be in front of another, but it isn't working. The z-index of one element is clearly greater than that of the other, but it is still positioned behind the other element. Could it be because one of the elements (the one showing up in front) is an iframe? Does anyone have any other advice?

like image 301
Proffesor Avatar asked Nov 14 '11 17:11

Proffesor


People also ask

Does Z Index work with IFrame?

To get around this we use the z-index property of CSS. This property only works on elements that have been positioned, as we have done with each IFrame, in that it has been placed within an absolutely positioned HTML div element. Z-index facilitates the display order (or 'stack' order) of elements on a page.

Why your Z index is not working?

You set z-index on a static element By default, every element has a position of static. 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.

Are IFrame deprecated?

Overview. The widget is considered deprecated. If you need to use an IFrame, consider the "IFrame Component" widget The Iframe can be used to embed websites in the client. Its URL is read from a String attribute of the dataview it is embedded in.


2 Answers

For those that arrive here later, the correct answer is to put position: relative; or any of the other position props on the problematic elements.

My recommendation would be to put this on every element involved with the problem:

position: relative;
z-index: 0;

and then increase the z-index on the back-most element(s) that are in front of the iframe.

If you do that, you will start winning pretty quick.

In my testing here, z-index only works if you have position explicitly set. Test it by modulating your z-index values and then trying to highlight the text on the screen via mouse-clicking. You should see evidence of layers acting either as desired or horribly incorrect.

I find it works great if you press F12 (to open dev pane, in Chrome) and then click the Inspect Button at the top-left or press CTRL + SHIFT + C. Then you can mouseover everything and see what their stacking context is relative to adjacent elements.

UX BONUS TIP: Remember, users may want to copy text, so make sure they can select it.

If you are having problems, most likely you are either:

  1. missing position: relative; on an element's parent, or
  2. missing z-index: 0; somewhere
like image 101
agm1984 Avatar answered Sep 27 '22 17:09

agm1984


Remember that the z-index index only counts on absolute elements. Both elements should has the position:absolute. More info in the CSS 2.1 Specification

like image 44
sanbor Avatar answered Sep 27 '22 18:09

sanbor