如何在CSS中将图像高度设置为等于窗口的高度

时间:2017-04-23 09:17:51

标签: html css

https://codepen.io/Krimlen/pen/zwBmjV

如何使两个图像的高度等于窗口和宽度以自动调整高度?我真的搜索了很多,我找不到答案。

  <body>
<div id="container">
<img src="https://s-media-cache-ak0.pinimg.com/originals/36/ef/af/36efafe91fddde518cba85e974c7e8c8.jpg" class="home" alt="Makeup Artists" 
class="home"/><img src="https://s-media-cache-ak0.pinimg.com/originals/36/ef/af/36efafe91fddde518cba85e974c7e8c8.jpg" alt="Photographers" class="home"/>
</div>
<img src="images/logo.png" alt="Satch" id="logo"/>
</body>
<style>
html, body{
margin: 0;
padding: 0;
}
#container{
text-align: center;
}
</style>

4 个答案:

答案 0 :(得分:1)

删除src =&#34; ..&#34;来自<img>并将其应用于css

img.home {
  background-image: url('https://s-media-cache-ak0.pinimg.com/originals/36/ef/af/36efafe91fddde518cba85e974c7e8c8.jpg');
  background-size: 100% 100%;
  position: absolute;
}

答案 1 :(得分:0)

我知道它是如何工作的唯一方法是

img {height:100%}

img {身高:100%;宽度:汽车; }

如果有帮助,请告诉我。

答案 2 :(得分:0)

您可以使用视口高度,但您还需要将其显示为flex。请参阅下面的示例

img {
    height: 100vh;
    display: flex;
    flex-direction: row;
}

这应该可行,您可能还想将z-index添加到容器中,以便图像位于其后面。像这样。

#container {
    text-align: center;
    position: relative;
    z-index: 100;
}
img {
    height: 100vh;
    display: flex;
    flex-direction: row;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 0;
}

答案 3 :(得分:0)

请尝试以下方法:

html, body {
  height: 100%;
}
.img_wrap {
  height: 100%;
  text-align: center;
}
.img_wrap img {
  width: auto;
  height: auto;
  max-height: 100%;
}
<div class="img_wrap">
<img src="https://s-media-cache-ak0.pinimg.com/originals/36/ef/af/36efafe91fddde518cba85e974c7e8c8.jpg" class="home" alt="Makeup Artists" 
class="home"/><img src="https://s-media-cache-ak0.pinimg.com/originals/36/ef/af/36efafe91fddde518cba85e974c7e8c8.jpg" alt="Photographers" class="home"/>
</div>
<div id="container"><img src="https://s-media-cache-ak0.pinimg.com/originals/36/ef/af/36efafe91fddde518cba85e974c7e8c8.jpg" alt="Photographers" class="home"/>
</div>
<img src="images/logo.png" alt="Satch" id="logo"/>