2012-03-02 77 views
10

有沒有辦法做到這一點只是CSS和沒有腳本?我有一個最小寬度的div,它內部是一個絕對定位的內部div,具有動態生成的可變寬度內容。有時候內容超出了包含div,但是由於它的絕對定位,它不會延伸包含div。我如何得到它以擴展包含的div?謝謝拉伸div以適應絕對定位的內容

+1

不,沒有腳本AFAIK – 2012-03-02 19:44:04

+1

設置一個例子,所以我們可以與你有什麼工作http://jsfiddle.net – Henesnarfel 2012-03-02 19:44:10

+0

絕對定位的div不再是「內部」的父母。因此不可能用css來做到這一點。 – Bazzz 2012-03-02 19:48:37

回答

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  
} 
+4

不適用於我:http://jsfiddle.net/gA7mB/117/ – 2013-03-01 16:02:50

+2

這對於保持孩子的寬度非常有用,但是有沒有辦法讓父母的身高也包括孩子? – 2013-10-11 16:10:07

1

如果您的內容元素有position: absolute,這是不可能的。但是,有時您可以避免絕對定位,而是通過使用浮動元素來獲得所需的結果。例如,參見this answervery similar question

相關問題