
CSS Grid has made it easier to create complex website layouts without relying on complicated positioning techniques or excessive CSS. However, traditional nested grids can create an alignment problem: a child grid normally creates its own independent rows and columns, even when it is placed inside a parent grid.
CSS Subgrid solves this problem by allowing a nested grid to use the row or column tracks defined by its parent grid. This makes it possible to align content across multiple components while keeping the HTML structure organised.
Subgrid is particularly useful for product cards, article layouts, dashboards, comparison sections, pricing components and other interfaces where content in separate sections needs to line up consistently.
This guide explains how CSS Subgrid works, its syntax, practical examples, responsive techniques, advantages, limitations and real-world applications.
CSS Subgrid is a feature of CSS Grid Layout that allows a nested grid container to inherit the track structure of its parent grid along one or both axes.
In a normal nested grid, the parent and child manage their own grid tracks independently. As a result, the columns or rows inside different child components may not align.
With Subgrid, a child grid can participate in the parent grid's existing track structure.
For example, imagine a page containing three product cards. Each card has:
A product image
A product title
A product description
A price
An action button
If the titles and descriptions have different lengths, the buttons may appear at different vertical positions. Subgrid can help align these sections by allowing the card contents to share the parent grid's row tracks.
Subgrid can be applied to columns, rows or both, depending on the layout requirements.
To understand Subgrid, it is helpful to understand the relationship between a parent grid and its nested grid items.
A parent grid defines a collection of rows and columns. A child element placed inside that grid can become another grid container.
When the child uses subgrid, it does not create independent tracks along that axis. Instead, it uses the tracks assigned to it by the parent.
For example:
.parent {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
.child {
display: grid;
grid-template-columns: subgrid;
}
Here, the parent defines three equal columns. The child grid can use the parent tracks that its grid placement covers.
Subgrid does not automatically make every nested element align with every other element. The parent must define an appropriate grid structure, and the child must span the relevant tracks.
The parent grid controls the main layout. A nested grid using Subgrid participates in that layout instead of independently calculating tracks along the subgridded axis.
This creates a shared track system that can improve alignment across components.
Column Subgrid allows a nested grid to inherit the parent's column tracks.
It is useful when multiple cards or sections need consistent horizontal alignment.
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
.card {
display: grid;
grid-template-columns: subgrid;
grid-column: span 1;
}
In this example, the parent has three columns. Each card occupies one parent column, so the example does not create a multi-column internal layout. For a nested component to align its internal content across multiple parent columns, it must span those columns.
A more useful example is a section that spans several parent columns:
.layout {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 16px;
}
.feature {
display: grid;
grid-column: span 4;
grid-template-columns: subgrid;
}
.feature-image {
grid-column: span 1;
}
.feature-content {
grid-column: span 3;
}
The feature component spans all four parent columns and uses those same tracks for its internal layout.
Row Subgrid allows a nested grid to inherit the parent's row tracks.
It is useful when content in different components needs to align vertically.
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: auto auto auto;
gap: 20px;
}
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
}
Each card spans three parent rows and uses those rows for its internal content.
The parent grid's track sizing can take the contents of subgridded items into account. This helps maintain alignment even when content lengths differ.
Subgrid can be used for both rows and columns when a component needs to share the parent grid's structure in both directions.
.child {
display: grid;
grid-template-columns: subgrid;
grid-template-rows: subgrid;
}
The child must occupy the relevant parent tracks on both axes.
The main Subgrid syntax uses the subgrid keyword in the grid-template properties.
This property defines the column tracks of a grid. Setting it to subgrid allows the child to inherit the parent grid's column tracks.
.child {
grid-template-columns: subgrid;
}
This property defines the row tracks of a grid. Using subgrid allows the child to use the parent grid's row tracks.
.child {
grid-template-rows: subgrid;
}
The grid-column property controls which parent columns an element occupies.
.child {
grid-column: span 3;
}
A child must span the appropriate columns to access the intended parent tracks.
The grid-row property controls which parent rows an element occupies.
.child {
grid-row: span 2;
}
This is important when using row Subgrid because the child needs to span the parent rows it intends to share.
The gap property controls the spacing between grid tracks.
A subgrid normally inherits the parent grid's gap along the subgridded axis. It can also use its own gap value when a different spacing arrangement is required.
.child {
display: grid;
grid-template-columns: subgrid;
column-gap: 12px;
}
CSS Grid supports named lines, which help developers identify specific positions in a layout.
Subgrid can inherit named lines from the parent. Developers can also introduce additional names within the subgrid.
.parent {
display: grid;
grid-template-columns:
[main-start] 1fr
[content-start] 2fr
[content-end] 1fr
[main-end];
}
.child {
display: grid;
grid-column: main-start / main-end;
grid-template-columns: subgrid;
}
Named lines can make large and complex layouts easier to understand and maintain.
The best way to understand Subgrid is to use it in practical interface designs.
Product cards often contain descriptions of different lengths. Without a shared row structure, prices and buttons may appear at inconsistent positions.
Subgrid can help create aligned card sections.
HTML:
<div class="products">
<article class="product">
<h2>Product One</h2>
<p>A compact product description.</p>
<strong>โน999</strong>
<button>View Product</button>
</article>
<article class="product">
<h2>Product Two</h2>
<p>
A longer description that explains the product's
important features and benefits.
</p>
<strong>โน1,499</strong>
<button>View Product</button>
</article>
<article class="product">
<h2>Product Three</h2>
<p>A product description with additional information.</p>
<strong>โน1,999</strong>
<button>View Product</button>
</article>
</div>
CSS:
.products {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
grid-template-rows: auto auto auto auto;
gap: 20px;
}
.product {
display: grid;
grid-row: span 4;
grid-template-rows: subgrid;
gap: 12px;
padding: 20px;
border: 1px solid #ddd;
border-radius: 10px;
}
.product button {
align-self: end;
}
Each product card spans four parent rows and uses them as its internal row structure. This allows corresponding content sections to align across the cards.
A website may display several article sections with headings, descriptions and additional information.
Subgrid can help align content inside nested article components.
.article-layout {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
gap: 20px;
}
.article-section {
display: grid;
grid-column: span 3;
grid-template-columns: subgrid;
}
.article-title {
grid-column: 1;
}
.article-body {
grid-column: 2;
}
.article-sidebar {
grid-column: 3;
}
The article section spans all three parent columns and uses the same column structure internally.
Dashboards often contain cards with headings, values, descriptions and charts. A shared grid structure can make these components easier to align.
.dashboard {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 16px;
}
.dashboard-card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
padding: 16px;
border: 1px solid #ddd;
border-radius: 12px;
}
.dashboard-card h2 {
margin: 0;
}
.dashboard-card p {
margin: 0;
}
A responsive layout can change the number of parent columns while retaining the component structure.
@media (max-width: 768px) {
.dashboard {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
}
@media (max-width: 480px) {
.dashboard {
grid-template-columns: 1fr;
}
}
When changing the parent grid structure, developers should also check that the row spans and child placement still make sense at each breakpoint.
Traditional nested grids create their own independent tracks. This is useful when a component needs complete control over its internal layout.
Subgrid is different because it allows the nested grid to share the parent's tracks.
Consider a layout with three cards. If each card uses its own independent rows, a long title in one card does not necessarily affect the corresponding title row in another card.
With row Subgrid, the cards can participate in shared parent row sizing. This can make corresponding sections align.
Use a traditional nested grid when:
The child component needs independent row or column sizing.
Its layout is unrelated to the parent grid.
The component should work independently in different containers.
Use Subgrid when:
Content needs to align across multiple nested components.
The parent should control shared track sizing.
A design contains repeated components with matching structural sections.
Independent nested grids create inconsistent alignment.
Both approaches are useful. The correct choice depends on the layout rather than a general preference for one technique.
Responsive design changes how content is arranged across different screen sizes. Subgrid can support these layouts by keeping nested content aligned with the parent grid.
For example, a desktop layout may use three columns, while a tablet layout uses two and a mobile layout uses one.
.page {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 20px;
}
.section {
display: grid;
grid-column: span 3;
grid-template-columns: subgrid;
}
@media (max-width: 768px) {
.page {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
.section {
grid-column: span 2;
}
}
@media (max-width: 480px) {
.page {
grid-template-columns: 1fr;
}
.section {
grid-column: span 1;
}
}
The parent and child placement must be updated together when the number of columns changes.
Subgrid does not automatically make a layout responsive. Media queries, flexible sizing and appropriate content placement are still important.
CSS Subgrid offers several benefits for modern frontend development.
Subgrid helps align content across separate components by sharing parent grid tracks.
Developers do not need to manually reproduce the same column or row definitions in every nested component.
Reusable components can participate in a larger page layout without losing their internal structure.
When the parent grid changes, subgridded children can adapt to its track structure instead of maintaining separate track definitions.
Subgrid supports both row and column alignment, making it useful for different types of interfaces.
Although Subgrid is useful, it is not suitable for every layout.
Subgrid depends on an existing parent grid. A child cannot use Subgrid meaningfully without the relevant parent grid tracks.
The child must span the correct parent tracks. Incorrect placement can lead to unexpected alignment.
Simple components with independent layouts may be easier to build using ordinary CSS Grid or Flexbox.
Modern versions of major browsers support CSS Subgrid, but developers should still verify compatibility with their target browser versions and project requirements.
For older browsers, a traditional nested grid or another layout technique can be used as a fallback.
Shared track sizing can make complex layouts harder to understand. Browser developer tools can help inspect grid tracks and identify placement issues.
Follow these practices when building layouts with Subgrid:
Start with a clear parent grid structure.
Use Subgrid only when child elements need to share parent tracks.
Make sure the child spans the correct rows or columns.
Use named grid lines for complex layouts where they improve readability.
Test layouts with short and long content.
Check responsive behaviour at different viewport widths.
Use ordinary nested grids when independent track sizing is required.
Test browser compatibility before relying on advanced layout behaviour.
Use consistent spacing and avoid unnecessary overrides.
Inspect grid tracks in browser developer tools while debugging.
Subgrid can be used in several types of web applications.
Product cards can use shared row tracks to align titles, descriptions, prices and buttons.
Course cards, lesson summaries and learning dashboards can use Subgrid to maintain consistent content alignment.
Article sections can share column tracks for headings, body content, images and supporting information.
Dashboard cards can use consistent row structures for headings, statistics, descriptions and visualisations.
Pricing plans often contain features, prices and action buttons. Subgrid can help align these sections across multiple plans.
Project cards and portfolio sections can use shared tracks to maintain a consistent visual structure.
Working on small projects is a useful way to understand how Subgrid behaves.
Create three product cards containing titles, descriptions, prices and action buttons. Use row Subgrid to align corresponding sections.
Build a course listing page with multiple cards. Use a parent grid and nested components to explore shared columns and responsive placement.
Create a dashboard containing statistics, charts and summary sections. Experiment with Subgrid to align internal headings and content.
Build a blog layout with a main content area, related information and supporting sections. Use column Subgrid to share the parent grid structure.
Create multiple pricing plans with different feature descriptions. Use shared row tracks to align prices and action buttons.
Developers should understand the following concepts before using Subgrid in larger projects:
HTML structure and semantic elements
CSS selectors and the box model
CSS Grid fundamentals
Grid rows and columns
Grid placement and track sizing
Responsive design and media queries
CSS Flexbox
Browser developer tools
Component-based frontend development
Knowledge of these concepts makes it easier to build maintainable interfaces with advanced CSS layout features.
CSS Subgrid is a specialised frontend layout skill rather than a separate job qualification. Understanding it can strengthen the practical abilities needed in roles such as:
Frontend Developer
UI Developer
React Developer
Web Designer
CSS Developer
Design System Developer
Employers generally look for broader frontend skills, including HTML, CSS, JavaScript, responsive design, accessibility and framework knowledge. Subgrid can be a useful addition to a developer's CSS expertise.
A structured learning approach can make the feature easier to understand.
Step 1: Learn CSS Grid fundamentals. Understand grid containers, grid items, rows, columns, gaps and track sizing.
Step 2: Practise nested grids. Create parent and child grids using independent track definitions.
Step 3: Introduce column Subgrid. Replace the child's independent column definition with subgrid and observe the alignment.
Step 4: Practise row Subgrid. Build repeated cards with shared row tracks.
Step 5: Learn named grid lines. Use named lines to organise complex layouts.
Step 6: Build responsive projects. Test how shared tracks behave when the parent grid changes at different breakpoints.
Step 7: Inspect the layout. Use browser developer tools to understand the parent and child grid structures.
Learners can also explore web development resources and related learning options through EasyShiksha Online Courses and the EasyShiksha Course Search.
CSS Subgrid makes complex layouts easier to manage by allowing nested grids to share the row and column tracks of their parent grids. It is especially valuable when repeated components need consistent alignment across different sections.
From product cards and pricing pages to dashboards and article layouts, Subgrid can reduce duplicated CSS and improve the consistency of interface design.
However, it works best when developers understand CSS Grid fundamentals and plan the parent grid structure carefully. By practising column Subgrid, row Subgrid and responsive layouts, frontend developers can build more organised and maintainable web interfaces.
More News Click here
Discover thousands of colleges and courses, enhance skills with online courses and internships, explore career alternatives, and stay updated with the latest educational news..
Gain high-quality, filtered student leads, prominent homepage ads, top search ranking, and a separate website. Let us actively enhance your brand awareness.