0

The following snippet works (you have to run it in "Full page", see button on the right after running a snippet), but it exceeds the browser height when an element is clicked. Why is grid-auto-rows: fit-content(50%) fit-content(50%); not enough to prevent this to happen?

How to fix this, in order to have all elements visible in a single page, without having to scroll? (Click on an image to see the problematic layout)

var $ = document.querySelector.bind(document);
var $$ = document.querySelectorAll.bind(document);
$('body').addEventListener('click', (event) => {
    $$('.container > div').forEach(element => element.classList.remove('clicked'));
    $('.container').classList.remove('clicked');
    if (event.target.tagName == "IMG") {
        event.target.closest('.container').classList.add('clicked');    
        event.target.closest('.box').classList.add('clicked');
    }
});
.page { max-width: 80%; max-height: 80%; margin: auto; border: 1px solid black; } 
.container { display: grid; grid-template-columns: 1fr 1fr 1fr; grid-auto-rows: fit-content(50%) fit-content(50%); gap: 0.5rem; }
.container img { max-width: 100%; }
.box { background-color: gray; }
.container.clicked { grid-template-columns: 1fr 1fr 4fr; grid-auto-rows: fit-content(33%) fit-content(33%) fit-content(33%); }    
.box.clicked { grid-column: 3 / 4; grid-row: 1 / 4; padding: 10px; background-color: yellow; }
.container.clicked .box:not(.clicked) { grid-auto-flow: column; }
<div class="page">
    <div class="container">
        <div class="box">Hello world <img src='https://picsum.photos/id/237/200'></div>
        <div class="box">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
        tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
        quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
        consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
        cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
        proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
        <div class="box"><img src='https://picsum.photos/id/241/300'></div>
        <div class="box"><img src='https://picsum.photos/id/242/400'></div>
        <div class="box"><img src='https://picsum.photos/id/238/500'></div>
        <div class="box"><img src='https://picsum.photos/id/239/200'></div>
    </div>
</div>
Basj
  • 41,386
  • 99
  • 383
  • 673
  • on clicked you are using: `grid-auto-rows: 1fr 1fr 1fr;` and not the fit content configuration – Temani Afif Dec 05 '22 at 11:18
  • @TemaniAfif You're right. I edited to use `grid-auto-rows: fit-content(33%) fit-content(33%) fit-content(33%);`, but the problem is still there: the rows are not equal height, and the total height exceeds the browser viewport (with a scrollbar), which I don't want. Would you have an idea? – Basj Dec 05 '22 at 11:22
  • you have to restrict the height of the container first. – Temani Afif Dec 05 '22 at 11:23
  • @TemaniAfif I tried with max-height or height (even if parent was positioned), but it was not working. Would you have an answer? I'm sure you would simplify all these rules :) – Basj Dec 05 '22 at 11:27

1 Answers1

1

You have to restrict the height of the container and consider overflow on the box elements

var $ = document.querySelector.bind(document);
var $$ = document.querySelectorAll.bind(document);
$('body').addEventListener('click', (event) => {
  $$('.container > div').forEach(element => element.classList.remove('clicked'));
  $('.container').classList.remove('clicked');
  if (event.target.tagName == "IMG") {
    event.target.closest('.container').classList.add('clicked');
    event.target.closest('.box').classList.add('clicked');
  }
});
.container {
  display: grid;
  max-height: 100vh; /* added */
  max-width: 80%;
  margin: auto;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  gap: 0.5rem;
}

.container img {
  max-width: 100%;
}

.box {
  background-color: gray;
  overflow: auto; /* added */
}

.container.clicked {
  grid-template-columns: 1fr 1fr 4fr;
  grid-template-rows: 1fr 1fr 1fr;
}

.box.clicked {
  grid-column: 3 / 4;
  grid-row: 1 / 4;
  padding: 10px;
  background-color: yellow;
}

body {
  margin: 0;
}
<div class="container">
    <div class="box">Hello world <img src='https://picsum.photos/id/237/200'></div>
    <div class="box">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
      dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
    <div class="box"><img src='https://picsum.photos/id/241/300'></div>
    <div class="box"><img src='https://picsum.photos/id/242/400'></div>
    <div class="box"><img src='https://picsum.photos/id/238/500'></div>
    <div class="box"><img src='https://picsum.photos/id/239/200'></div>
  </div>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
  • Thanks a lot @TemaniAfif! How did you make that the rows are equal width and columns are equal width, no matter the content? Usually `minmax(0, 1fr)` is needed to make this happen (see main answer in https://stackoverflow.com/questions/47601564/equal-width-columns-in-css-grid), but here it is not needed. Why? – Basj Dec 05 '22 at 11:33
  • 1
    @Basj the overflow is doing the job: https://stackoverflow.com/a/43312314/8620333 – Temani Afif Dec 05 '22 at 11:44