Introduction
The article discusses how to use CSS subgrids to create intricate grid systems within parent grids. Specifically, it explores the implementation of nested rounds in a layout similar to the structure used by FIFA for its tournament rounds. This technique allows designers and developers to maintain a clean and scalable design while accommodating multiple levels of nesting without the need for complex column or row adjustments.
CSS Subgrid: A Powerful Grid Feature
CSS subgrids are an advanced feature that enables elements within a grid container to align themselves relative to other elements in their grid container. By using grid-auto-rows and grid-auto-columns, we can define how subgrids should be arranged, which is particularly useful when dealing with nested grids where parent rows or columns must accommodate multiple levels of children.
Implementing Nested Knockout Rounds
To demonstrate the use of CSS subgrids in this context, consider a setup similar to FIFA's knockout rounds. In this example, we have five knockout rounds, each contained within a single grid container. The outermost round (the quarter-finals) contains four matches, while the next inner level (the semi-finals) contains two matches each. Each match is represented as a grid-template-rows and grid-template-columns defined section in CSS.
HTML Structure
The basic structure for rendering this nested grid layout starts with an outermost container holding our entire knockout rounds series:
<div class="knockout-container">
<div class="round">
<div class="match"></div>
<div class="match"></div>
<div class="match"></div>
<div class="match"></div>
</div>
<div class="round">
<div class="match"></div>
<div class="match"></div>
</div>
</div>CSS Styling
Using the outermost container to define our grid, we first establish its grid-template-columns for a consistent layout:
.knockout-container {
display: grid;
grid-template-columns: repeat(4, 1fr);
}Here we create four equal-width columns. The inner rounds will fit into these columns without any additional row or column adjustments.
We then style the round class to utilize CSS subgrids for its children elements (match). We set up each match element to be twice as wide and twice as high compared to a single grid cell, allowing them to stack neatly within their parent container:
.round {
display: contents; /* Prevents layout thrashing by not calculating the grid */
}
.match {
grid-row: span 2;
grid-column: span 2;
}The grid-row: span 2 and grid-column: span 2 properties ensure that each match spans two rows and columns, effectively creating a "2x2" box within its parent container.
By utilizing subgrids, we eliminate the need to manually adjust row or column numbers for nested elements. This approach allows us to create clean, scalable designs even when dealing with complex, multi-level grid layouts like the FIFA knockout rounds structure.
Conclusion
CSS subgrids offer a powerful and efficient way of handling nested grids within parent containers. The technique showcased in this article demonstrates how we can utilize CSS features to achieve clean and scalable design without introducing unnecessary complexity or requiring extensive manual adjustments. This method not only simplifies our designs but also makes it easier for others who may work with the codebase to understand and maintain the layout, ultimately saving time and effort during development and maintenance phases.
