[英]How do I absolutely position a relatively positioned element?
Let's say I have a container, and that I want to put this one container into yet another container that's also full of other stuff. 假设我有一个容器,我想将此容器放入另一个也充满其他东西的容器中。 The CSS code might look something like this:
CSS代码可能如下所示:
.parent-container {
width: 100%;
position: relative;
}
.child-container {
width: 600px;
position: absolute;
left: 25px;
bottom: 100px;
}
However, .child-container
also includes absolutely positioned elements, which are position relatively to .parent-container
because .child-container
doesn't have position: relative
. 但是,
.child-container
还包括绝对定位的元素,它们相对于.parent-container
相对定位,因为.child-container
没有position: relative
。 So my question is, how can I position .child-container
's children relatively to itself, while still keeping it correctly positioned in .parent-container
? 所以我的问题是,我如何相对于自身定位
.child-container
的子代,同时仍将其正确定位在.parent-container
?
PS Wrapping .child-container
in a position: absolute
'd div
and making .child-container
position: relative
should do the trick, but I was hoping for something more... semantic. PS将
.child-container
包裹在一个position: absolute
d div
并将.child-container
position: relative
应该解决这个问题,但是我希望有更多...语义。
However, .child-container also includes absolutely positioned elements, which are position relatively to .parent-container because .child-container doesn't have position: relative.
但是,.child-container还包括绝对定位的元素,它们相对于.parent-container相对定位,因为.child-container没有位置:相对。
Incorrect. 不正确 Absolute positioning is with respect to the nearest ancestor that is positioned , not
position: relative
. 绝对定位是相对于最近定位的祖先,而不是
position: relative
。 Anything except position: static
will make an element positioned. 除了
position: static
之外的任何内容position: static
都会使元素定位。 ( position: relative
won't move the container out of normal flow so it is used when you want to set a positioning context with no side effects). (
position: relative
不会将容器移出正常流程,因此在您要设置无副作用的定位上下文时使用它)。
Since the parent is position: absolute;
由于父级是
position: absolute;
they are positioned with respect to that already. 他们已经相对于此定位。
You don't need to change .child-container position to relative in order to set him has "relative" parent. 您无需将.child-container的位置更改为相对,即可将其设置为“相对”父母。 please review this link from MDN about position absolute.
请查看MDN中有关绝对排名的链接。
"The absolutely positioned element is positioned relative to nearest positioned ancestor. If a positioned ancestor doesn't exist, the initial container is used." “绝对定位的元素相对于最近定位的祖先定位。如果不存在定位的祖先,则使用初始容器。”
*positioned ancestor is an element with either: relative, fixed or absolute position * positioned祖先是具有以下位置的元素:相对,固定或绝对位置
Absolutely positioned elements should already be relative to their parent. 绝对定位的元素应该已经相对于其父元素。 Here's a demo which shows nested items within absolute positioning
这是一个演示,展示了绝对位置内的嵌套项目
HTML 的HTML
<div class='parent-container'>
Parent
<div class='child-container'>
1st Child
<div class='grandchild-container'>
2nd Child
</div>
</div>
</div>
CSS ( color added to illustrate differences ) CSS ( 添加了颜色以说明差异 )
.parent-container {
position: relative;
background: grey;
}
.child-container {
position: absolute;
background: red;
left: 20px;
}
.grandchild-container {
position: absolute;
background: yellow;
left: 20px;
}
It will look like this 看起来像这样
*Notice each position is relative to its parent.
*请注意,每个职位都是相对于其上级的。
For more info see: 有关更多信息,请参见:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.