Search code examples
htmlcsscss-grid

Make image take all remaining space on page without creating a scrollbar


I'm expecting my image to take all remaining space on space without creating a scrollbar, but still it makes page scrollable.

Why image still takes as much height as it needs, despite I said that wrapper size is 100vh, second row is 1fr and height of image is 100% (which should mean that it size should be 100% of parents size and parent size is literally 100vh - navbar size).

.wrapper {
  display: grid;
  grid-template-rows: auto 1fr;
  height: 100vh;
}

.navbar {
  padding: 1rem;
  text-align: center;
}

.reader {
  height: 100%
}

.image {
  display: block;
  margin: 0 auto;
  object-fit: contain;
  width: auto;
  height: 100%;
}
<div class="wrapper">
  <nav class="navbar">NavBar</nav>
  <div class="reader">
    <img class="image" src="https://picsum.photos/id/237/870/1265" alt="">
  </div>
</div>


Solution

  • It is necessary to set oveflow: hidden to container div:

    * {
      margin: 0;
      box-sizing: border-box;
    }
    
    .wrapper {
      display: grid;
      grid-template-rows: auto 1fr;
      height: 100vh;
    }
    
    .navbar {
      padding: 1rem;
      text-align: center;
    }
    
    .reader {
      height: 100%;
      overflow:hidden;
    }
    
    .image{
      display: block;
      margin: 0 auto;
      object-fit: contain;
      width: auto;
      height: 100%;
      overflow:hidden;
    }
    <div class="wrapper">
        <nav class="navbar">NavBar</nav>
        <div class="reader">
          <img class="image" src="https://picsum.photos/id/237/870/1265" alt="">
        </div>
      </div>