简体   繁体   English

如何绝对定位相对定位的元素?

[英]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;
}

jsFiddle jsFiddle

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.

相关问题 如何使这就像两个“绝对”和“相对”定位在同一时间,我位置的元素? -CSS - How do I position an element so that it acts like both 'absolutely' & 'relatively' positioned at the same time? - CSS 如何快速找到绝对定位的子项的相对定位的HTML父元素? - How can I quickly find the relatively positioned HTML parent element of an absolutely positioned child? 如何将绝对定位的元素居中? - How do I center an absolutely positioned element? 如何在相对定位的父作品中制作绝对定位的文本? - how do i make absolutely positioned text within a relatively positioned parent work? 如何将元素固定在相对放置的容器中? - How to position element as fixed in relatively positioned container? 如何在其位置保留绝对定位的元素? - How do I keep an absolutely positioned element in its place? 将绝对定位的div扩展并定位到相对定位的父对象 - Expand and position absolutely positioned div to relatively positioned parent 为什么绝对居中的div相对居中的绝对居中的元素不居中? - Why an absolutely positioned element inside centered relatively positioned div is not centered? 如何将绝对定位的DIV放在SPAN或相对定位的DIV后面? - How to put absolutely positioned DIV behind the SPAN or relatively positioned DIV? 绝对定位一个兄弟姐妹时如何保留兄弟姐妹元素的位置? - How to preserve sibling element position when one sibling is absolutely positioned?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM