1

As you can see in my snippet, both columns are getting their heights set to the height of the tallest column. I however want my columns to have their heights set to the shortest column, where the second column would have overflow scroll instead.

I do not know the height of either of the columns as their content is loaded in dynamically.

I also do not need to use grid if flex can acheive my desired result.

.grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}
.item1 {
  background-color: #00ffff;
}
.item2 {
  background-color: #ff00ff;
  overflow-y: auto;
}
<div class='grid'>
  <div class='item1'>
    <div class='item1_content'>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
    </div>
  </div>
  <div class='item2'>
    <div class='item2_content'>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
    </div>
  </div>
</div>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
Oscar R
  • 133
  • 1
  • 11

1 Answers1

2

You could use javascript for this. I think there isn't a CSS only solution for this :)

let referenceBox = document.getElementsByClassName("item1_content")[0];
let adjustedBox = document.getElementsByClassName("item2_content")[0];

adjustedBox.style.height = referenceBox.offsetHeight + 'px';
.grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}
.item1 {
  background-color: #00ffff;
}
.item2 {
  background-color: #ff00ff;
  overflow-y: auto;
}
<div class='grid'>
  <div class='item1'>
    <div class='item1_content'>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
    </div>
  </div>
  <div class='item2'>
    <div class='item2_content'>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
    </div>
  </div>
</div>
Wimanicesir
  • 4,606
  • 2
  • 11
  • 30