2

I am trying to set a max-height of 40px to the second row of a CSS Grid.
The width of the container is unknown therefore it should be responsive.
Also the content of each box is unknown.

I tried grid-template-rows: auto minmax(0, 40px); but unfortunately it will also set a height of 40px if no second row exists.

Is there any way to use grid-template-rows and prevent the second row from growing to 40px?

Here is an example where you can see that the second row takes space although there are no grid items:

ul {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(95px, 1fr));
  grid-template-rows: auto minmax(0, 40px);
  grid-auto-rows: 15px;
  overflow: hidden;
  border: 1px solid orange;
  padding: 0;
}

li {
  list-style: none;
  padding: 10px;
  background: black;
  color: white;
}


body {
 width: 650px;
}
<h1><a href="https://en.wikipedia.org/wiki/List_of_colors:_A%E2%80%93F">Colors</a></h1>

<ul>
  <li>Android green</li>
  <li>Antique brass</li>
  <li>Antique bronze</li>
  <li>Antique fuchsia</li>
  <li>Antique ruby</li>
  <li>Antique white</li>
</ul>
jantimon
  • 36,840
  • 23
  • 122
  • 185

2 Answers2

0

The current grid implementation for minmax is always using the larger value. (See minmax() defaulting to max)

Therefore minmax(0, 40px) will always result in 40px.

jantimon
  • 36,840
  • 23
  • 122
  • 185
-1

You can use the grid-auto-rows property to define the height of implicitly created grid rows.

In your case, you'll define your grid as having only one row. Then any additional grid rows created by your content will use the dimensions defined with the grid-auto-rows property. This way the second row won't exist until it's needed.

Edit: To have only one row display by default, but also define a height of 40px for a second row if needed, AND define a height of 15px for third, fourth, fifth rows if needed, you can add multiple values to grid-auto-rows.

ul {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(95px, 1fr));
  grid-template-rows: auto;
  grid-auto-rows: 40px 15px 15px 15px;
  overflow: hidden;
  border: 1px solid orange;
  padding: 0;
}
li {
  list-style: none;
  padding: 10px;
  background: black;
  color: white;
}
body {
 width: 650px;
}
<h1><a href="https://en.wikipedia.org/wiki/List_of_colors:_A%E2%80%93F">Colors</a></h1>

<ul>
  <li>Android green</li>
  <li>Antique brass</li>
  <li>Antique bronze</li>
  <li>Antique fuchsia</li>
  <li>Antique ruby</li>
  <li>Antique white</li>
</ul>
Sean
  • 6,873
  • 4
  • 21
  • 46
  • not sure why this answer was downvoted but this won’t help me as it will style all upcoming rows not only the second row – jantimon Feb 08 '22 at 21:57
  • @jantimon It sounded like you were only planning to have two rows. I'm also not sure why this was downvoted. – Sean Feb 09 '22 at 02:18
  • @jantimon What size should the 3rd, 4th, etc rows be? – Sean Feb 09 '22 at 13:21
  • All other rows should be of height `0` – jantimon Feb 09 '22 at 13:22
  • @jantimon why do you want rows with contents but no height? – Sean Feb 09 '22 at 13:23
  • I am using it for a responsive show more button: https://codesandbox.io/s/strange-joliot-d55tw?file=/src/ShowMoreGrid.tsx – jantimon Feb 09 '22 at 13:23
  • @jantimon That's helpful context. Setting a row's height to 0 is not an accessible way to hide content. It will still be focusable by keyboard and assistive tech users. Regardless, moving the "show more" button out of the grid will simplify things greatly in your example. – Sean Feb 09 '22 at 13:29
  • why does accessibility matter for this question? :) btw my demo is accessible try to use the tab key – jantimon Feb 09 '22 at 13:31
  • your suggestion to always show a "show more" button leads to a bad ux as soon as all items fit in one row – jantimon Feb 09 '22 at 13:36
  • if it helps to understand the question assume that all following rows should have a height of 15px.. – jantimon Feb 09 '22 at 13:39
  • @jantimon [Accessibility matters for all digital interfaces](https://www.w3.org/WAI/fundamentals/accessibility-intro/). If you can tab to things that aren't visible, it's not accessible. I'm not following how it would lead to bad user experience. – Sean Feb 09 '22 at 14:09
  • 1
    @jantimon I've edited the question to have an auto first row, 40px second row (if needed), 15px subsequent rows (if needed). – Sean Feb 09 '22 at 14:15