问题 css绝对位置一个内部div到页面顶部忽略父母的相对位置


有没有办法绝对将内部div放在页面顶部忽略父母的相对位置?


10624
2018-02-24 11:36


起源



答案:


不,除非你在DOM中重新找到它。

运用 position:fixed 如果您希望它与窗口相关而不是与文档相关,则可能是另一种选择


13
2018-02-24 11:54





您可以使用 position: absolute; 和负值:

HTML:

<div id="parent">
      <div id="child">
           The child's content
      </div>
</div>

CSS:

#parent
{
    position: relative;
    top: 200px;
    border: 1px dashed #f00;
    height: 50px;
    width: 200px;
}

#child
{
    position: absolute;
    top: -200px;
}

这应该做到这一点。 你在这里的例子


1
2018-02-24 11:41



是的,如果父母没有设置位置,这是有效的:亲... - Gidon
更新以包括位置相对。 - Kyle
有没有办法完全忽略父母? - Gidon
只有将孩子带出文件中的父母。 jsfiddle.net/Kyle_Sevenoaks/eWj8Z/4 - Kyle


答案:


不,除非你在DOM中重新找到它。

运用 position:fixed 如果您希望它与窗口相关而不是与文档相关,则可能是另一种选择


13
2018-02-24 11:54





您可以使用 position: absolute; 和负值:

HTML:

<div id="parent">
      <div id="child">
           The child's content
      </div>
</div>

CSS:

#parent
{
    position: relative;
    top: 200px;
    border: 1px dashed #f00;
    height: 50px;
    width: 200px;
}

#child
{
    position: absolute;
    top: -200px;
}

这应该做到这一点。 你在这里的例子


1
2018-02-24 11:41



是的,如果父母没有设置位置,这是有效的:亲... - Gidon
更新以包括位置相对。 - Kyle
有没有办法完全忽略父母? - Gidon
只有将孩子带出文件中的父母。 jsfiddle.net/Kyle_Sevenoaks/eWj8Z/4 - Kyle