0

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: column with grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)). I am new to CSS Grid and learning main concepts.
2
  • What happens if you set max-height? Commented May 16 at 16:59
  • 1
    your element is not growing, it's overflowing Commented May 16 at 19:03

1 Answer 1

1

The element which you bound to a specific height or width, is always of that height & width, even in your given example if you see in inspect mode then you'll find the height & width to be 500px, But the issue which you are facing is happening because of the overflow behavior of the element, the overflow behavior of an element can be defined through the overflow attribute of css, by default It's set to visible, meaning even if any children/content overflows out of your container It'll still be visible as like your facing, to fix it you can set the overflow attribute of the container to hidden or scroll (It'll make the container scrollable), It'll make sure that any content, that gets out of container is not shown, For more information about overflow attribute you might visit this

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

Comments

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.