拉伸div以适合绝对定位的内容

Joe*_*oey 13 css

有没有办法用css而不是脚本来做到这一点?我有一个最小宽度的div,它内部是一个绝对定位的内部div,动态生成可变宽度的内容.有时内容超出了包含div,但由于它的绝对位置,它不会拉伸包含div.如何让它延伸包含div?谢谢

lex*_*x82 5

如果您的内容元素有position: absolute,则不可能。但是,有时您可以避免绝对定位,但通过使用浮动元素来获得所需的结果。请参阅这个非常相似问题答案作为示例。


小智 0

尝试引用我制作的这个 JSfiddle 。http://jsfiddle.net/gA7mB/ \n如果我没看错的话,这基本上就是你正在寻找的东西。

\n\n

超文本标记语言

\n\n
<div class="container" >\n<div class="relative" >\n    <div class="absolute" >\n\n        <h1>I am Content i can be as long as you wish. t is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using \'Content here, content here\', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for \'lorem ipsum\' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</h1>\n\n    </div>\n</div>\n
Run Code Online (Sandbox Code Playgroud)\n\n

\xe2\x80\x8b

\n\n

CSS

\n\n
.container{\n    width: 500px;            \n}\n\n.relative{\n    position: relative;\n    background: #666;\n    min-width: 200px;\n    min-height: 200px;\n}\n\n.absolute{\n    position: absolute;\n    background: #999;\n    top: 20px;  /* not important, just to show absolute position */\n    left: 20px; /* not important, just to show absolute position */ \n}\n
Run Code Online (Sandbox Code Playgroud)\n

  • 这对于保持孩子的宽度非常有用,但是有没有办法让父母的高度也包含孩子呢? (3认同)