Home > database >  Modify grid to use full height but not more
Modify grid to use full height but not more

Time:12-06

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 >
    <div >
        <div >Hello world <img src='https://picsum.photos/id/237/200'></div>
        <div >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 ><img src='https://picsum.photos/id/241/300'></div>
        <div ><img src='https://picsum.photos/id/242/400'></div>
        <div ><img src='https://picsum.photos/id/238/500'></div>
        <div ><img src='https://picsum.photos/id/239/200'></div>
    </div>
</div>

CodePudding user response:

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 >
    <div >Hello world <img src='https://picsum.photos/id/237/200'></div>
    <div >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 ><img src='https://picsum.photos/id/241/300'></div>
    <div ><img src='https://picsum.photos/id/242/400'></div>
    <div ><img src='https://picsum.photos/id/238/500'></div>
    <div ><img src='https://picsum.photos/id/239/200'></div>
  </div>

  • Related