CSS位置相对没有相对宽度? [英] CSS position relative without relative width?

查看:109
本文介绍了CSS位置相对没有相对宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望某个元素具有相对的位置。但是我不希望其子元素(具有 position:absolute )具有相对于父元素的相对宽度。

I'd like an element to have relative positioning. But I don't want its child element (with position:absolute) to have relative width to the parent.

例如: http://jsfiddle.net/t2yJP/ 。我希望第二个 body> div 具有 position:relative ,但让其子项的宽度保持相同的行为。

For example: http://jsfiddle.net/t2yJP/. I'd like the second body>div to have position:relative, but have its child's width maintain the same behavior.

推荐答案

这个jsFiddle

但是您应该真正重新考虑您的策略。在您的小提琴中,您的第二个示例仅适用于父div,因为它未定位,因此 .abs div从技术上讲不在父中。

But you should really rethink your strategy. In your fiddle, your second example only works because the parent div is not positioned and therefore, the .abs div is technically not in the parent.

通常,子元素位于其父母内部。这就是容器的用途!因此,如果您不希望 .abs div受到红色矩形的约束,则不要将其放在红色矩形内。

Normally, child elements are inside their parents. That's what containers are for! So if you don't want the .abs div to be constrained by the red rectangle, don't put it inside the red rectangle.

这篇关于CSS位置相对没有相对宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆