Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Css grid grow to fit content

Tags:

css

css-grid

I've been trying to wrap me head around css grid. Why doesn't the outer grid grow to the size of the inner grid in this example? How can I fix this layout?

.scroll {
  width: 200px;
  overflow: auto;
}

.wrapper {
  box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.23);
  border-radius: 2px;
  margin: 1rem;
}

.grid {
  display: inline-grid;
  grid-template-columns: auto auto;
  grid-column-gap: 1em;
}
<div class="scroll">
  <div class="wrapper">
    <div class="grid">
      <p>Grid:</p>
      <div class="grid">
        <p>text here</p>
        <p>verylongtextherewithoutanybreakingoptions</p>
      </div>
    </div>
  </div>
</div>
like image 416
F43nd1r Avatar asked Dec 22 '25 22:12

F43nd1r


2 Answers

In your example you have two grids AND two other wrappers. Both grid are extended to the same size since they are made inline-grid but it's not the case of the .wrapper where you applied box-shadow.

Make the .wrapper inline-block and it will fix the layout.

.scroll {
  width: 200px;
  overflow: auto;
}

.wrapper {
  box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.23);
  border-radius: 2px;
  margin: 1rem;
  display:inline-block;
}

.grid {
  display: inline-grid;
  grid-template-columns: auto auto;
  grid-column-gap: 1em;
}
<div class="scroll">
  <div class="wrapper">
    <div class="grid">
      <p>Grid:</p>
      <div class="grid">
        <p>text here</p>
        <p>verylongtextherewithoutanybreakingoptions</p>
      </div>
    </div>
  </div>
</div>
like image 170
Temani Afif Avatar answered Dec 24 '25 16:12

Temani Afif


The outer .grid is always grow up with inner .grid. Which is fixed in here is width of .scroll

like image 40
KibGzr Avatar answered Dec 24 '25 15:12

KibGzr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!