'How do I make my parent div resize with my child div using display: grid and flex?
html, body {
height: 100vh;
width: 100%;
margin: 0;
padding: 0;
}
* {
box-sizing: border-box;
}
.container {
display: grid;
grid-template-columns: repeat(5, calc(20% - 20px));
grid-auto-rows: 1fr;
grid-gap: 20px;
margin-left: 20px;
}
.container .item {
background-color: red;
}
.item .inside {
display: flex;
justify-content: space-between;
}
.inside-elem {
padding: 20px;
}
<div class="container">
<div class="item">
<div class="inside">
<span class="inside-elem">ABC</span>
<span class="inside-elem">EFG</span>
</div>
</div>
<div class="item">
</div>
<div class="item">
</div>
<div class="item">
</div>
<div class="item">
</div>
</div>
In the example above, if you zoom in, you could see that EFG goes outside the grid cell. I want the grid cell to resize with its child element while maintaining the given format. The issue stems from me adding padding: 20px, the grid cell doesn't resize on the padding.
I tried adding border-box but that didn't fix it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
