简体   繁体   English

Z索引无法正常工作

[英]z-index not working right

Page in question: 有问题的页面:

http://meyers.ipalaces.org/sitemap/ http://meyers.ipalaces.org/sitemap/

The first <LI> should be z-index: 2; 第一个<LI>应该是z-index: 2; and should be on top of any other <LI> further down. 并应位于其他<LI>上方。 (Meyers) (迈耶斯)

CSS: CSS:

.sitemap #primaryNav > ul > li {
    float: none;
    background: #ffffff url('images/L1-left.png') center bottom no-repeat;
    z-index: 2;
    position: relative;
}

The idea is that the background color of #fff should be ontop of the <LI> 's below, therefore creating an effect similar to this: 这个想法是#fff的背景色应该在下面<LI>的上方,因此会产生类似于以下的效果:

http://astuteo.com/slickmap/demo/ http://astuteo.com/slickmap/demo/

if you use firebug on the above link and disable position:relative from #primaryNav #home You'll see that it looks like mine. 如果您在上述链接上使用Firebug并从#primaryNav #home禁用position:relative#primaryNav #home发现它看起来像我的。 I am not sure how to get it to be like theirs. 我不确定如何使其像他们一样。

First thing I would point out is that, in your case, "home" is not the origin in the sitemap. 我要指出的第一件事是,就您而言,“主页”不是站点地图中的原点。 While might want it to seem that way visually, semantically this is not the case. 虽然可能希望它看起来像那样,但在语义上却并非如此。 SlickMap understands this (or maybe they just got lucky), which is why in their html the "home" li is at the same level as the other primary pages. SlickMap理解了这一点(或者也许他们很幸运),这就是为什么在其html中“ home” li与其他主要页面处于同一级别的原因。 The only thing above other pages is the root, which has no page (though most people redirect root to the "homepage"). 其他页面上方唯一的东西是根目录,它没有页面(尽管大多数人将根目录重定向到“主页”)。

Second, once a parent's z-index is set, unless the child is set to position: absolute and the parent is not set to position: relative , all children are considered to start one level above the parent in regards to stacking order. 其次,一旦父母的z-index设置,除非孩子设为position: absolute和家长没有设置为position: relative ,所有的孩子都被认为开始父的上一级的问候堆叠顺序。 This is defined in the CSS 2.1 standard under 9.9.1 Specifying the stack level: the 'z-index' property as (emphasis mine): 这是在CSS 2.1标准的9.9.1下定义的。 指定堆栈级别:'z-index'属性为(强调我的):

The order in which the rendering tree is painted onto the canvas is described in terms of stacking contexts. 根据堆栈上下文描述了将渲染树绘制到画布上的顺序。 Stacking contexts can contain further stacking contexts . 堆栈上下文可以包含其他堆栈上下文 A stacking context is atomic from the point of view of its parent stacking context; 从其父堆栈上下文的角度来看,堆栈上下文是原子的。 boxes in other stacking contexts may not come between any of its boxes. 其他堆叠上下文中的盒子可能不在其任何盒子之间。

So, while SlickMap was able to tell you: 因此,尽管SlickMap能够告诉您:

The first <LI> should be z-index: 2; 第一个<LI>应该是z-index: 2; and should be on top of any other <LI> further down. 并应位于其他<LI>上方。

In your design doing so makes no difference because moving the first li upwards moves all of it's children with it. 在您的设计中,这样做没有什么区别,因为向上移动第一个li会使它的所有子对象都移动。 And, since the children in your home block start their own stacking context one level higher than the parent, your "home" li can never be above the items inside it. 并且,由于您的home块中的孩子开始自己的堆叠上下文比父级高一层,因此您的“ home” li永远不能位于其内部项目之上。

Now that that is out of the way. 现在,这已成为不可能。 I reviewed your code with the SlickMap code to understand the differences. 我用SlickMap代码检查了您的代码,以了解它们之间的差异。 As you can see below, there is pretty much only one reasonable option here that is going to be 100% compatible. 正如您在下面看到的那样,这里几乎只有一个合理的选择是100%兼容的。

z-index does support negative numbers but, since the stacking context is different between "home" and it's siblings, you cannot use that here. z-index确实支持负数,但是,由于“ home”和它的同级之间的堆叠上下文不同,因此您不能在此处使用它。 However, if each child of "home" was set to position: absolute and the "home" li was set to position: static (the default) you could, in some edge cases, use z-index: -1 on those children and make them appear to be behind the parent. 但是,如果将“ home”的每个子级设置为“ position: absolute ,将“ home” li设置为“ position: static (默认值),则在某些情况下,可以对这些子级使用z-index: -1让他们看起来在父母的后面。 But, you have to have other variables available as well (such as the parent's parent's background being transparent) AND , most importantly, you would have to position each child manually. 但是,您还必须具有其他可用变量(例如,父母的父母的背景是透明的) 并且 ,最重要的是,您必须手动定位每个孩子。 Clearly this is not a good idea. 显然,这不是一个好主意。 Not to mention negatives in z-index is buggy in IE6/7. 更不用说z-index负数在IE6 / 7中是错误的。

What you need to do is, as SlickMap did, merge the first ul under the li containing "home" with ul.level--0 . 您需要做的是像SlickMap一样,将包含“ home”的li下的第一个ulul.level--0合并。 This will result in the following hierarchy: 这将导致以下层次结构:

ul
--Home
--Meyers
    --Another Level
--M....
    --Another Level
--etc

Then, you should be able to apply z-index: 2 to the "home" li and you will get the result you are looking for. 然后,您应该能够将z-index: 2应用于“ home” li ,您将获得所需的结果。 Obviously, the change in structure will likely require other changes to your styles as well. 显然,结构上的更改可能还需要对样式进行其他更改。

There are probably some ways that CSS 3 properties could help you, but I dont want to get into that as I'm unsure your requirements. CSS 3属性可能有一些方法可以为您提供帮助,但是由于不确定您的要求,因此我不想涉足。

Hopefully this lesson in CSS layout strcutres was helpful enough for you to make an informed decision on how to move forward with your project. 希望本节有关CSS布局结构的课程对您有足够的帮助,以便您就如何继续进行项目做出明智的决定。 If you have any addition questions about what I've said, just let me know. 如果您对我所说的还有其他疑问,请告诉我。

Positioning and z-index won't be of any use, if the <li> you're trying to bring to the top layer is actually bringing the entire site map with it.* 如果您要带到最上层的<li>实际上带了整个站点地图,则定位和z-index将<li> 。*

Look at the source of your page side by side with Astuteo's code. 与Astuteo的代码并排查看页面源代码。 There are a couple of places that you REALLY changed the markup: 确实在几个地方更改了标记:

Astuteo:     <ul id="primaryNav"> /* using natural block tags */
Meyers: <div id="primaryNav"><ul class="level--0"> /* wrapping without need */

Astuteo:      <li id="home"><a href....>Home</a></li> /* CLOSED li (immediately) */
*Meyers:  <li><a href...>Home</a>...the entire sitemap...</li> /* you wrapped the entire site map inside your first <li> */

Your desired solution essentially mirrors the original code. 您所需的解决方案实质上是原始代码的镜像。 A rewrite or copy/paste at this point would probably be less time consuming than tracking down the remaining errors in syntax modification and/or layout strategy. 此时的重写或复制/粘贴可能比跟踪语法修改和/或布局策略中的其余错误要耗费更少的时间。

EDIT : have you tried something as simple as...removing the background property for the first-child of home? 编辑 :您是否尝试过一些简单的操作,如...为家庭中的第一个孩子删除背景属性?

<ul class="level--1">
    <li style="background: none">...

It would be something like this in your CSS (to keep your CSS out of your HTML): CSS中可能是这样的(将CSS保留在HTML中):

.level--1 li:first-child { background:none; }

Well, it took a while to work out but I think I have it now. 好吧,花了一段时间才解决了,但我想我现在知道了。 I must say that my Spidey sense was on high due to the fact that the page does not validate . 我必须说由于页面未通过验证 ,我的Spidey感觉很高。

This is not really a z-index problem since you're using a 3rd party (and most excellent!) framework to style the HTML - SlickMap CSS which should work as advertised. 这并不是真正的z-index问题,因为您使用的是第三方(也是最出色的!)框架来设置HTML的样式-SlickMap CSS应该可以如广告所示那样工作。

The problem isn't with the invalid markup although that should be fixed regardless. 问题不在于无效的标记,尽管无论如何应予以解决。 Namely that there are missing </li> tags, there is an invalid <br> in the utilityNav and duplicate id attributes in the main primaryNav . 也就是说,缺少</li>标记, utilityNav有无效的<br> ,主primaryNav重复的id属性。

The problem is that you have introduced extra <ul> levels and the CSS rules just don't match the correct elements any more. 问题是您引入了额外的<ul>级别,而CSS规则不再与正确的元素匹配。 Is this required for any reason? 是否出于任何原因要求这样做? I can see that this has tried to be corrected by adding child selectors to your CSS, but the markup is radically different to what the framework expects and hence the z-index stacking isn't correct. 我可以看到已经尝试通过向CSS添加子选择器来纠正此问题,但是标记与框架期望的完全不同,因此z-index堆栈是不正确的。

With a minimal <ul> this demo works as expected since the HTML is correct for the SlickMap CSS. 由于HTML对SlickMap CSS而言是正确的,因此使用最小的<ul> 此演示 So I'd try and remove the extra <ul> levels from your markup and mimic the markup on the SlickMap demo page or my fiddle. 因此,我将尝试从您的标记中删除多余的<ul>级别,并模仿SlickMap演示页或我的小提琴上的标记。

Hope this helps :) 希望这可以帮助 :)

The parent will need to have position relative to get a proper z-index stacking context. 父级将需要具有相对位置才能获得适当的z索引堆叠上下文。 Super obscure, but maybe this will work for you? 超级晦涩难懂,但这也许对您有用吗?

Try reading through this link to get a better understanding of the (super obscure, no fault of yours) method of determining the stacking priority of DOM elements: 尝试通读此链接,以更好地理解确定DOM元素堆叠优先级的方法(超级晦涩,没有错):

https://developer.mozilla.org/En/Understanding_CSS_z-index/The_stacking_context https://developer.mozilla.org/En/Understanding_CSS_z-index/The_stacking_context

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM