2016-04-28 25 views
0

我在嘗試設置樣式時遇到問題。 #container應該是viewportheight,#caption應該take as much height as it needsimg in #image應該fill the remaining height(同時保持其縱橫比)。 #container絕不應該超過視口height兩個div動態填充視口高度

<div id="container"> 
    <div id="image"> 
    <img src="my-image.jpg" alt="my-image"> 
    </div> 
    <div id="caption"> 
    <p> 
     Some info about this image.<br> 
     Sometimes this could be two lines.<br> 
     Maybe even three. 
    </p> 
    </div> 
</div> 

這裏是如何做到這一點工作動態的例子:

enter image description here

有什麼建議?我自己也擺弄了flex和表格,但沒有用,頁面總是以超過視口height結束。

回答

2

您可以使用flexbox

,你可能需要一些媒體的質疑。

body { 
 
    margin: 0 
 
} 
 
#container { 
 
    display: flex; 
 
    flex-direction: column; 
 
    height: 100vh 
 
} 
 
#caption { 
 
    flex: 1; 
 
    background: red 
 
} 
 
img { 
 
    display: block; 
 
    margin: auto 
 
}
<div id="container"> 
 
    <div id="image"> 
 
    <img src="//lorempixel.com/300/500" alt="my-image"> 
 
    </div> 
 
    <div id="caption"> 
 
    <p> 
 
     Some info about this image. 
 
     <br>Sometimes this could be two lines. 
 
     <br>Maybe even three. 
 
     <br> 
 
    </p> 
 
    </div> 
 
</div>

+0

這是接近,但現在像坐在後面的標題。我需要它動態縮小以不干擾標題。 –

+0

在我的片段中img永遠不會在標題後面,他們是兄弟姐妹 – dippas

+0

圖像是否應該自動縮放以填充其餘高度?它看起來像'img {display:block; margin:auto}'是不足以完成這 –