I’m using CSS Grid with grid-auto-flow: column and grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)).
I’ve explicitly set a fixed height on the container (height: 500px), but when one of the grid items contains long content (like a paragraph), the container appears to grow vertically.
.container {
height: 500px;
width: 500px;
margin: 200px auto;
background-color: black;
box-shadow: 0 6px 20px rgb(0 0 0 / 0.2);
display: grid;
grid-auto-flow: column;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
}
.box {
color: white;
font-size: 1.5rem;
text-align: center;
}
body {
background-color: bisque;
}
<body>
<div class="container">
<div class="box" style="background-color: #e53935">FIRST</div>
<div class="box" style="background-color: #d81b60">SECOND</div>
<div class="box" style="background-color: #8e24aa">Lorem ipsum dolor sit amet consectetur adipisicing elit. Esse ipsa hic nemo veniam voluptate tempore quod et numquam adipisci asperiores cumque optio, incidunt enim odit saepe deleniti accusamus eveniet labore.
Veniam voluptatibus a laborum nam odit ratione necessitatibus doloribus perferendis hic incidunt? Nulla quam repellendus eveniet necessitatibus, eum, odio vel officiis atque illum perspiciatis id? Totam, perspiciatis voluptatibus? Ducimus, placeat.
Tenetur, aliquid? Asperiores eligendi alias assumenda, quo dolor porro vero molestias commodi temporibus quis libero deserunt numquam magnam fuga? Quisquam numquam obcaecati officiis expedita nulla reiciendis quia porro maxime sapiente!
Aliquid voluptatem modi animi omnis culpa similique veniam quas ipsa, sequi porro ea ut maiores quo error repellendus nobis rerum perspiciatis dolorem adipisci totam libero. Modi minus cum ad repudiandae.</div>
<div class="box" style="background-color: #5e35b1">FOURTH</div>
<div class="box" style="background-color: #3949ab">FIFTH</div>
<div class="box" style="background-color: #1e88e5">SIXTH</div>
<div class="box" style="background-color: #00acc1">SEVENTH</div>
<div class="box" style="background-color: #00897b">EIGHTH</div>
</div>
</body>
Questions:
- Why does the height appear to grow beyond the 500px I set?
Note:
- The one in question is an example which I am using to understand the Grid behavior upon using
grid-auto-flow: columnwithgrid-template-columns: repeat(auto-fit, minmax(150px, 1fr)). I am new to CSS Grid and learning main concepts.