有没有办法用css而不是脚本来做到这一点?我有一个最小宽度的div,它内部是一个绝对定位的内部div,动态生成可变宽度的内容。有时内容超出了包含div,但由于它的绝对位置,它不会拉伸包含div。如何让它延伸包含div?感谢
答案 0 :(得分:1)
尝试引用我制作的这个JSfiddle。 http://jsfiddle.net/gA7mB/ 如果我读得正确,那基本上就是你要找的东西。
HTML
<div class="container" >
<div class="relative" >
<div class="absolute" >
<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>
</div>
</div>
CSS
.container{
width: 500px;
}
.relative{
position: relative;
background: #666;
min-width: 200px;
min-height: 200px;
}
.absolute{
position: absolute;
background: #999;
top: 20px; /* not important, just to show absolute position */
left: 20px; /* not important, just to show absolute position */
}
答案 1 :(得分:1)
如果您的内容元素有position: absolute
,则无法进行此操作。但是,有时您可以避免绝对定位,而是通过使用浮动元素来获得所需的结果。有关示例,请参阅 this answer 至 very similar question 。