Kareem Kamal
Kareem Kamal

Reputation: 1048

how to set image height to be equal to window's height in CSS

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

how to make the two images' height equal to the window and the width to automatically adjust wth height? I've really searched a lot and I can't find the answer.

  <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>

Upvotes: 1

Views: 2148

Answers (4)

itacode
itacode

Reputation: 3787

Please try the following:

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"/>

Upvotes: 0

Chris James Champeau
Chris James Champeau

Reputation: 994

You can use viewport height but you will also need to display it as flex. See the example below

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

That should work, you may also want to add z-index to your container so the image is behind it. Like this.

#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;
}

Upvotes: 1

Jibin.Jay
Jibin.Jay

Reputation: 342

Remove src=".." from <img> and apply it in 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;
}

Upvotes: 1

Bob
Bob

Reputation: 49

The only way i know how it works is

img { height:100% }

or

img { height:100%; width:auto; }

let me know if this helped.

Upvotes: 0

Related Questions