使用 z-index 将 Div 置于 Div 之上
我的 HTML 中有以下 div:
<div class="main">
<div class="bgimage"></div>
<div class="content">Text</div>
它直接在我的体内。
使用以下 CSS:
body {
margin: 0;
padding: 20px 0;
}
.content {
filter: alpha(opacity=50);
-moz-opacity: 0.5;
opacity: 0.5;
}
.content {
position: relative;
z-index: 1;
border: #000 thin solid;
width: 960px;
margin-left: auto;
margin-right: auto;
background-color: #000;
}
.bgimage {
position: absolute;
z-index: -1;
width: 1024px;
height: 768px;
margin-left: auto;
margin-right: auto;
background-image: url(bg1.jpg);
}
基本上,我有一个显示背景图像的 Div,并且在其上有另一个具有透明度的 Div。当前的代码可以工作,但我的问题是当我尝试从顶部取出内容 div 时。
例如,当我添加 margin-top:100px 时,也会使图像下降。我想如果它不在同一个 z-index 上它就不会碰它?为什么添加边距也会迫使 bgimage div 向下?
我也尝试过将内容类别的 div 设置为绝对位置和 zindex,但这样就不会居中。我应该如何解决这个问题?
I have the following divs in my HTML:
<div class="main">
<div class="bgimage"></div>
<div class="content">Text</div>
which is directly inside my body.
With the following CSS:
body {
margin: 0;
padding: 20px 0;
}
.content {
filter: alpha(opacity=50);
-moz-opacity: 0.5;
opacity: 0.5;
}
.content {
position: relative;
z-index: 1;
border: #000 thin solid;
width: 960px;
margin-left: auto;
margin-right: auto;
background-color: #000;
}
.bgimage {
position: absolute;
z-index: -1;
width: 1024px;
height: 768px;
margin-left: auto;
margin-right: auto;
background-image: url(bg1.jpg);
}
Basically, I have a Div that with a display of a background image, and I will have another Div on top of this with transparency. This current code works, but my problem is when I am trying to take the content div down from the top.
When I add margin-top:100px, for example, is also brings the image down. I thought it would not touch it if it is not on the same z-index? Why does adding a margin also force the bgimage div down?
I have also tried making the div with class of content a position of absolute and a zindex, but then this won't centre. How should I solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的 CSS 应该是
这样的 .content 将相对于 .bgimage 定位
您当前的 CSS 使 .bgimage 相对于文档的位置。
请参阅有关 CSS 定位的链接
your CSS should be
so the .content will be positioned relative to the .bgimage
your current CSS makes the .bgimage position relative to the document.
see this link on CSS positioning
z-index
与定位无关:它只影响元素的渲染顺序。Position:relative
告诉浏览器将元素呈现在它应该在的位置,并最终将其偏移left
、right
、顶部坐标或底部坐标。因此,边距、填充等仍然会影响它。
只有
position:absolute
才能保证位置独立性。z-index
has no relation to positioning: it only affects the rendering order of your elements.Position: relative
tells the browser to render the element at the place it should be, and offset it by eventualleft
,right
,top
orbottom
coordinates. Therefore, margins, paddings, etc. still affect it.Only
position: absolute
guarantees position independance.我认为您的代码中根本不需要“z-index”或“position:absolute”——除非您有其他未向我们透露的复杂情况。
要将背景置于 DIV class="main" 的中心:
“center top”将背景图像的中心顶部放置在其所应用到的元素的中心顶部。您可能希望将 a 应用于
同一元素——以防止较窄的窗口隐藏边缘(如果“视口”比背景尺寸窄,这将改变元素的渲染方式)。
你的预修改代码唯一可以做的事情是我的修改后的代码不能做的:
关于“z-indexing”和“position”属性的一些说明是正确的,但未能实现提到:
您已将 class="content" 元素从“流程”中取出。当 class="content" 元素的内容增长时,祖先元素不会增长。这是“z 索引”元素和保持“流中”元素之间的重要且根本的区别。
其他附注:
I see no need for "z-index"es or "position: absolute" in your code at all -- unless you have other complications that you have not revealed to us.
To center the background on the DIV class="main":
The "center top" places the center-top of the background image on the center-top of the element it's applied to. You may want to apply a
to the same element -- to prevent a narrower window from hiding the edges (this will change how the element is rendered if the "viewport" is narrower than the background's dimensions).
The only thing your pre-modified code it can do that my modified code can't:
Some of what was stated about "z-indexing" and the "position" property above was correct but failed to mention:
you've taken your class="content" element out of "the flow". The ancestor elements won't grow when the content of class="content" element grows. This is an important and fundamental difference between "z-index"ed elements and elements that remain "in the flow".
Other side notes:
CSS 绝对定位始终“相对”于具有“position:relative”的最近祖先,否则默认使用 body 标记。如果您包含的 CSS 只影响这些 div,那么您的 .content div 将相对于 .main div 定位,但您的 .bgImage 将根据标签定位。
如果您希望 .content 和 .bgImage 同步移动,那么您需要向 div.main 添加“position:relative”。
CSS absolute positioning is always done "relative" to the most recent ancestor that has a "position: relative", otherwise it uses the body tag by default. If the CSS you included is all that affects those divs, then your .content div will be positioned relative to the .main div, but your .bgImage will be positioned based on the tag.
If you want both .content and .bgImage to move in lockstep, then you'll need to add a "position: relative" to div.main.