2

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>
Daweed
  • 1,419
  • 1
  • 9
  • 24
Anon Anon
  • 329
  • 3
  • 12
  • 1
    Seems like using it as a background image would give you better control – Kinglish Apr 22 '21 at 18:54
  • @JohnTyner Backgorund image seems like a hack, not the solution of the problem – Anon Anon Apr 22 '21 at 18:56
  • Look at this solution: https://stackoverflow.com/questions/43311943/prevent-content-from-expanding-grid-items Set the ```min-height: 0``` , as by default it is ```min-height: auto;```. This will work. https://jsfiddle.net/f9gdr0b1/2/ – prettyInPink Apr 22 '21 at 19:36

1 Answers1

2

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>
StepUp
  • 36,391
  • 15
  • 88
  • 148