1

There is a known behaviour with cs grid where if the content of the cell is larger than the computed layout cell size, the cell expands. Meaning your grid is only a fixed size if the content is less than or equal to the cell size.

There are several workarounds for this that involve you changing the style for the grid and cells themselves.

However, I have noticed that these workarounds are not necessary when I use a 3rd party control (e.g MUI List), where all you have to do is set overflow: 'auto' on the actual control itself. You don't have to mess with the parent grid.

So my question is. How do I go about making a control that will switch to a scrollviewer if its content is too large to be displayed inside a grid cell. i.e I don't want to mess with the parent grid every time I want to use my control in a new place.

Below I have included a fiddle that shows the issue and the workaround. But I would to know how to fix it without messing with grid cells, as per the case when using 3rd party controls

https://jsfiddle.net/8b074ung/11/

.grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);

  width: 800px;
  height: 300px;
}

.cell {
  background: red;
  margin: 10px;
  /* min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: row; */
}

.scrollable {
  background: orange;
  margin: 10px;
  overflow: auto;
}

.row {
  background: purple;
  display: grid;
  grid-auto-flow: column;
  width: max-content;
}

.item {
  width: 100px;
  height: 100px;
  margin: 5px;
  background: green;
}
<div class="grid">

  <div class="cell">
    <div class="scrollable">
      <div class='row'>
        <div class='item'></div>
        <div class='item'></div>
        <div class='item'></div>
        <div class='item'></div>
        <div class='item'></div>
      </div>

    </div>
  </div>

  <div class="cell">

  </div>
</div>

0

2 Answers 2

2

Set .cell { min-width: 0; } or .cell { overflow: hidden; } (any value other than visible) - then they will shrink!

.grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  width: min(800px, 100%);
  height: 300px;
  gap: 20px;
}

.cell {
  background: red;
  min-width: 0;
}

.scrollable {
  background: orange;
  margin: 10px;
  overflow: auto;
}

.row {
  background: purple;
  display: grid;
  grid-auto-flow: column;
  width: max-content;
  padding: 10px;
  gap: 10px;
}

.item {
  width: 100px;
  height: 100px;
  background: green;
}
<div class="grid">
  <div class="cell">
    <div class="scrollable">
      <div class='row'>
        <div class='item'></div>
        <div class='item'></div>
        <div class='item'></div>
        <div class='item'></div>
        <div class='item'></div>
      </div>
    </div>
  </div>
  <div class="cell"></div>
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, didn't know that :) Upvoted
0

Based on the code you provided, one solution would be to not use the "fr" unit when defining your grid and instead use a more concrete size like 50%. This way, your .scrollable container shouldn't expand the grid column:

.grid {
  display: grid;
  grid-template-columns: repeat(2, 50%); /* <-- 50% instead of 1fr */

  width: 800px;
  height: 300px;
}

.cell {
  background: red;
  margin: 10px;
  /* min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: row; */
}

.scrollable {
  background: orange;
  margin: 10px;
  overflow: auto;
}

.row {
  background: purple;
  display: grid;
  grid-auto-flow: column;
  width: max-content;
}

.item {
  width: 100px;
  height: 100px;
  margin: 5px;
  background: green;
}
<div class="grid">

  <div class="cell">
    <div class="scrollable">
      <div class='row'>
        <div class='item'></div>
        <div class='item'></div>
        <div class='item'></div>
        <div class='item'></div>
        <div class='item'></div>
      </div>

    </div>
  </div>

  <div class="cell">

  </div>
</div>

2 Comments

Good to know. One thing I did notice with this solution though is that it doesn't seem to work with gap? if you add a gap of 20px to .grid (and a background colour to make it clear) .. it knocks things out of wack.
Yes, that's right. This works - as I wrote - "based on the code you provided", which doesn't use gap or mention it as a requirement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.