Home > Web Front-end > CSS Tutorial > How can I make nested CSS Grid rows adjust to their content size instead of inheriting the height of a parent grid?

How can I make nested CSS Grid rows adjust to their content size instead of inheriting the height of a parent grid?

Linda Hamilton
Release: 2024-11-23 06:03:14
Original
352 people have browsed it

How can I make nested CSS Grid rows adjust to their content size instead of inheriting the height of a parent grid?

CSS Grid: Adjusting Nested Grid Rows to Content Size

In the provided HTML and CSS, you've encountered an issue where the nested grid .grid-3 forces its rows to align with the height of the nested grid .grid-2. To resolve this, you want the rows of .grid-3 to adjust to the size of their content, thereby matching the height of the .grid-2 rows.

The solution lies in the grid-auto-rows property. By default, this property is set to auto, which distributes space evenly among rows regardless of their content size. However, in this case, we need rows that adjust to the content.

To achieve this, you'll need to set grid-auto-rows: max-content on the .grid-3 grid. This tells the browser to make the rows as tall as their content dictates, ensuring they'll match the height of the .grid-2 rows.

Here's the updated CSS snippet:

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

With this change, the rows in .grid-3 will now resize to accommodate their content, matching the height of the .grid-2 rows while keeping their content fully visible.

<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

This demonstrates how to configure CSS Grid to ensure rows adjust to the size of their content, allowing different grids to seamlessly align with each other.

The above is the detailed content of How can I make nested CSS Grid rows adjust to their content size instead of inheriting the height of a parent grid?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template