2016-04-28 46 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}'是不足以完成这 –