2015-10-13 73 views
2

工作,我有這樣的代碼顯示absolute divrelative divCSS高度自動不相對DIV

HTML:

<section> 
    <div class="container"> 
     <div class="row"> 
      <div class="col-md-12"> 
       <div class="relative"> 
        <div class="absolute">Setting the top, right, bottom, and left properties of a relatively-positioned element will cause it to be adjusted away from its normal position. Other content will not be adjusted to fit into any gap left by the element.</div> 

       </div> 
      </div> 
      <div class="col-md-12"> 
       normal text added 
      </div>  
     </div> 
    </div> 
</section> 

CSS:

.relative { 
    position: relative; 
    height:auto; 
    background-color:#e1e1e1; 
} 
.absolute { 
    position: absolute; 
    bottom: 0; 
    left: 0; 
    background-color: white; 
    width: auto; 
    background-color:#f5f5f5; 
    margin:0 15px; 
} 

在現實中,當我設置爲height:autorelative divdiv沒有顯示。如果我設置任何height值,即:height:150px;相對div它的工作原理和顯示正確。我該如何解決這個問題?

演示here

+0

替換代碼.... .absolute { 的位置是:絕對的; bottom:0; left:0; background-color:white; 寬度:100%; background-color:#f5f5f5; margin:0 15px; 身高:100%; } –

回答

0

添加溢出:隱藏;最小高度:100像素;

.relative { 
    position: relative; 
    height:auto; 
    overflow :hidden; 
    min-height:100px; 
    background-color:#e1e1e1; 
} 
+0

高度:汽車不能正常工作,請參閱:http://jsfiddle.net/4onnaoa2/2/ –

+0

需要改變這一部分也.absolute { 的位置是:絕對的; bottom:0; left:0; background-color:white; 寬度:自動; background-color:#f5f5f5; margin:0 15px; 溢出:auto; height:100px; } –

2

你不能用CSS做到這一點,需要設置你與position:relative外層div高度爲你的內在position:absolute顯現。

這是因爲當您將<div>設置爲position:absolute時,外部容器不再具有其寬度和高度的概念,並且不會容納它佔用的空間。

0

這裏的竅門

CSS

.relative { 
    position: relative; 
    overflow :hidden; 
    min-height:100px; 
    background-color:#e1e1e1; 
} 
.absolute { 
    position: absolute; 
    bottom: 0; 
    left: 0; 
    background-color: white; 
    width: auto; 
    background-color:#f5f5f5; 
    margin:0 15px; 
     overflow: auto; 
    height: 100px; 
} 

DEMO

設置相對高度和絕對高度

+0

我用FF檢查你的演示,看到div滾動。我不需要滾動div –