How can I make nested CSS Grid rows automatically adjust their height to match content?

DDD
Release: 2024-11-24 20:05:13
Original
977 people have browsed it

How can I make nested CSS Grid rows automatically adjust their height to match content?

CSS Grid: Auto Height Rows, Sizing to Content

When working with nested CSS grids, achieving consistent row heights across both grids can be a challenge. In this scenario, the rows in the right nested grid are forced to match the height of the rows in the left nested grid, resulting in uneven spacing.

To rectify this issue, you can employ the grid-auto-rows: max-content property for the right nested grid. This property sets the height of each row to the height of its tallest child element. As a result, the rows will dynamically adjust to accommodate the content, aligning the heights with those in the left nested grid.

Example:

.grid-3 {
  grid-auto-rows: max-content;
}
Copy after login

This modification ensures that each row in the right nested grid is sized to fit its content, creating a consistent height between both grids. Here's an updated snippet:

.grid-2 {
  grid-auto-rows: max-content;
}

.grid-3 {
  grid-auto-rows: max-content;
}
Copy after login
<div class="grid-2">
  <div class="grid-2">
    <div class="left">L</div>
    <div class="left">L</div>
    <div class="left">L</div>
    <div class="left">L</div>
    <div class="left">L</div>
    <div class="left">L</div>
    <div class="left">L</div>
    <div class="left">L</div>
    <div class="left">L</div>
    <div class="left">L</div>
    <div class="left">L</div>
    <div class="left">L</div>
    <div class="left">L</div>
    <div class="left">L</div>
    <div class="left">L</div>
    <div class="left">L</div>
    <div class="left">L</div>
    <div class="left">L</div>
  </div>
  <div class="grid-3">
    <div class="right">R</div>
    <div class="right">R</div>
    <div class="right">R</div>
    <div class="right">R</div>
    <div class="right">R</div>
    <div class="right">R</div>
    <div class="right">R</div>
    <div class="right">R</div>
    <div class="right">R</div>
    <div class="right">R</div>
    <div class="right">R</div>
    <div class="right">R</div>
    <div class="right">R</div>
  </div>
</div>
Copy after login

Now, the rows in both the left and right nested grids will automatically adjust their height, creating visually consistent results.

The above is the detailed content of How can I make nested CSS Grid rows automatically adjust their height to match content?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template