将绝对定位的元素从其相对定位的祖先中分离出来?

编程入门 行业动态 更新时间:2024-10-25 08:17:28
本文介绍了将绝对定位的元素从其相对定位的祖先中分离出来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我有一个文档

<style type="text/css"> div { border:1px solid; padding:15px; } #i_am_relatively_positioned{ background-color:#fcc; margin:50px; padding:50px; position:relative; } #inner { background-color:#cfc; } #i_want_to_be_absolute_to_body{ background-color:#ccf; left:0; position:absolute; top:0; } </style> <body> <div id="i_am_relatively_positioned"> <div id="inner">inner</div> <div id="i_want_to_be_absolute_to_body">absolute to body</div> </div> </body>

是否有办法使#i_want_to_be_absolute_to_body相对于body元素(而不是其直接相对容器)绝对定位?

Is there a way to make #i_want_to_be_absolute_to_body positioned absolute with respect to the body element, rather than its immediate relative container?

我知道我只能用一些负数上下,但这似乎很笨拙-这是我唯一的选择吗?

I know I can just use some negative top and left but that seems kludgey - is this my only option?

推荐答案

您可以使用一些javascript来做到这一点(我假设您无法更改标记?).

You can use a bit of javascript to do this (I'm assuming you can't change the markup?).

document.body.appendChild(document.getElementById('i_want_to_be_absolute_to_body'));

更多推荐

将绝对定位的元素从其相对定位的祖先中分离出来?

本文发布于:2023-10-08 02:57:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1471308.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:祖先   元素

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!