5

在 HTML 中,当您在子块中使用 position:absolute css 属性时,绝对值不会取自父标记,而是从整个浏览器窗口引用。示例代码如下所示..

CSS

.parent {
    width: 400px;
    height: 400px;
    border: 1px solid green;
}

.child {
    position: absolute;
    width: 200px;
    height: 200px;
    border: 1px solid red;
    bottom: 0px;
}
4

3 回答 3

10

如果你想在父块中安排子元素,只需添加position:relative父 CSS

于 2013-09-30T12:24:52.443 回答
2

父块需要position设置为非静态值,即 position: absoluteposition: fixedposition: relative

您需要的值取决于布局应用程序。

于 2013-09-30T12:27:49.917 回答
1
.parent {
    width: 400px;
    height: 400px;
    border: 1px solid green;
    position:relative;/*this makes all my children s position relative to me */
}

.child {
    position: absolute;/* i have an absolute position and i am relative to my parent*/
    width: 200px;
    height: 200px;
    border: 1px solid red;
    bottom: 0px;
}

演示:http: //jsfiddle.net/pGvvq/

标记:

<section class=parent>
    this makes all my children s position relative to me
    <article class=child>
        i have an absolute position and i am relative to my parent
    </article>
</section>

在此处输入图像描述

阅读更多http://css-tricks.com/absolute-positioning-inside-relative-positioning/

于 2013-09-30T12:36:05.237 回答