位置绝对定位DIV相对于(也绝对定位)父DIV?

编程入门 行业动态 更新时间:2024-10-27 22:20:16
本文介绍了位置绝对定位DIV相对于(也绝对定位)父DIV?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我在固定布局时总是遇到的一个问题。我有一个绝对定位的DIV,我放置一个孩子DIV里面,也需要绝对定位。但我真的想让这个孩子DIV的行为相对于父母...这是甚至可能吗?

This is one problem I always have when i'm fixing layouts. I've got a absolutely positioned DIV, I place a child-DIV inside which also needs to be absolutely positioned. But I really want this child-DIV to behave relative to the parent... Is this even possible? Or do I need to create a wrap-DIV?

<div class="container" style="position:relative;"> <div class="parent" style="position:absolute;"> <!-- this child div will behave relative to .container --> <div class="child" style="position:absolute;"></div> </div> </div>

这是我想避免的包装解决方案:

This is the 'wrapping' solution which I'd rather avoid:

<div class="container" style="position:relative;"> <div class="parent" style="position:absolute;"> <div class="wrapper" style="position:relative;"> <!-- this child div will behave relative to .wrapper--> <div class="child" style="position:absolute;"></div> </div> </div> </div>

推荐答案

绝对定位的元素相对于边

An absolutely positioned element is positioned with respect to the edges of its nearest ancestor that is not positioned (i.e. not position: static).

由于父元素是 position:absolute,子元素将会

Since the parent element is position: absolute, the child will be positioned with respect to its edges.

所以你似乎在问如何获得你已经拥有的行为。您的第一个示例中的评论似乎不正确。

So you appear to be asking how to get behaviour you already have. The comment in your first example appears to be incorrect.

更多推荐

位置绝对定位DIV相对于(也绝对定位)父DIV?

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

发布评论

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

>www.elefans.com

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