IE z-index relative/absolute bug in list

2019-02-06 06:44发布

I have the following navigation where .topNav has position:relative and subnav has position:absolute. I cant get the sublist to appear over the main list due to z-index problems. This seems to be a known problem.

<ul>
<li class="topNav">About Us
<ul class="subNav"><li> Subsection A</li><li>Subsection B</li></ul>
</li>
</ul>

Does anyone know of a workaround?


UPDATE http://brh.numbera.com/experiments/ie7_tests/zindex.html shows exacly the problem I have. My original posting was in the context of a list but I have reduced the problem to the fact that z-index dosn't seem to work when have an element with position:absolute inside a parent element with position:relative


8条回答
做自己的国王
2楼-- · 2019-02-06 06:49

Solution: assign z-index in decreasing order

<div class="base" style="z-index:2">
<div class="inside">
    This has some more text in it. This also has a background. This should obscure the second block of text since it has a higher z-index.
</div>
This has some text in it. This has some text in it. This has some text in it. This has some text in it. This has some text in it.
</div>

<div class="base" style="z-index:1">
This is the second div. You should not be seeing this in front of the grey box. This has some text in it. This has some text in it. This has some text in it. This has some text in it. This has some text in it. This second box should be obscured by the grey box.
</div>
查看更多
手持菜刀,她持情操
3楼-- · 2019-02-06 06:56

Ok, I think your problem probably stems from a lack of understanding about how z-index works. The z-index property is only relevant for elements at the same level in the DOM hierarchy. In other words, if you have:

<ul id="a">
  <li id="b">b</li>
  <li id="c">c</li>
</ul>
<div id="d"></div> 

and "b" and "c" are styled such that they overlap, z-index will determine which one ends up on top. However, if "c" and "d" overlap, "d" will always be on top, no matter what c's z-index is, because elements that are closer to the root DOM node will always appear above elements that are nested deeper in.

So, as long as "subnNav" is a child of "topNav," I don't think there is any way to make it cover it's parent's content. In other words, as far as I know there is no workaround for this issue, except to make "subNav" not be a child of "topNav".

(NOTE: All that being said, CSS is not simple, so there may still be some way to get the effect you want that I'm not aware of. All I can say is that, based on my understanding of z-index and my pretty good general CSS knowledge, there's no way that I know of.)

查看更多
Bombasti
4楼-- · 2019-02-06 06:57

Here's a very good article that explains the stacking issues that machineghost mentions.

http://css-discuss.incutio.com/wiki/Overlapping_And_ZIndex

What you might want to consider (depending on why you're wanting the positioning on multiple elements) is adding a hover selector to .base (use JavaScript for IE6) that adds the class to give it relativity.

.base:hover{position:relative;}

This then means that the second .base doesn't have position: relative.

查看更多
你好瞎i
5楼-- · 2019-02-06 06:59
淡お忘
6楼-- · 2019-02-06 07:07

adding

background: url(blank.gif); 

for absolutely positioned elemnts solves the problem for me. Mybe it can helps u 2 :)

regards

查看更多
劫难
7楼-- · 2019-02-06 07:10

Similar to answer by @Orhaan, setting a background property to the absolute element is the only solution that worked for me...

background-color: rgba(0,0,0,0);

Thanks Alex Leonoard

查看更多
登录 后发表回答