Everything you need as a full stack developer

CSS Subgrid with nested grid alignment

- Posted in CSS by

TL;DR CSS Subgrid allows for complex layouts by nesting grids within each other. A subgrid is a grid container that participates in an outer grid layout, enabling alignment of cells with specific cells in the outer grid using grid-column and grid-row properties. By mastering this technique, developers can build responsive designs that scale across different screen sizes and devices.

Unlocking the Power of CSS Subgrid with Nested Grid Alignment

As a fullstack developer, you're likely no stranger to the world of CSS grids. Introduced in 2017, CSS Grid Layout has revolutionized the way we approach layout design on the web. One of its most powerful features is the ability to create complex layouts using nested grids and subgrids. In this article, we'll dive into the world of CSS Subgrid with nested grid alignment, exploring comprehensive examples and tricks to help you master this technique.

What is a Subgrid?

Before we dive in, let's quickly define what a subgrid is. A subgrid is a grid container that participates in an outer grid layout. It allows us to create complex layouts by nesting grids within each other. Think of it like a Russian doll: a grid container can have multiple subgrids inside it, each with its own grid layout.

Basic Subgrid Example

Let's start with a basic example to illustrate how subgrids work. We'll create a simple grid container with three columns and two rows, using the display: grid property.

.grid-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, 1fr);
}

Now, let's add a subgrid inside one of our grid cells. We'll create a new grid container with two columns and one row.

.subgrid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: 1fr;
}

We can then place this subgrid inside one of our main grid cells using the grid-column and grid-row properties.

<div class="grid-container">
  <div class="subgrid">Subgrid content</div>
  <div>Grid cell 2</div>
  <div>Grid cell 3</div>
  <div>Grid cell 4</div>
</div>

Nested Grid Alignment

Now that we have our basic subgrid setup, let's talk about nested grid alignment. When working with subgrids, you'll often want to align the subgrid's cells with the outer grid's cells. This is where the grid-column and grid-row properties come in handy.

Let's say we want our subgrid to span two columns of the outer grid, starting from the second column. We can achieve this by setting grid-column: 2 / 4; on our subgrid.

.subgrid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: 1fr;
  grid-column: 2 / 4;
}

This will align our subgrid's cells with the second and third columns of the outer grid.

Advanced Subgrid Alignment

But what if we want to create a more complex layout? Let's say we have a subgrid that spans multiple rows and columns, and we want its cells to align with specific cells in the outer grid. This is where things can get tricky.

One trick is to use the grid-template-columns property on our subgrid to define a column template that matches the outer grid's column structure. For example:

.subgrid {
  display: grid;
  grid-template-columns: 1fr repeat(2, 0.5fr);
  grid-template-rows: repeat(2, 1fr);
}

In this example, our subgrid has three columns, with the first column taking up one fractional unit (1fr) and the second and third columns sharing two fractional units (0.5fr). We can then use the grid-column property to align our subgrid's cells with specific cells in the outer grid.

Real-World Example: Responsive Layout

Let's put this into practice with a real-world example. Suppose we're building a responsive layout for a news website, with a main content area and two sidebars.

<div class="grid-container">
  <div class="main-content">Main content</div>
  <div class="sidebar-1">Sidebar 1</div>
  <div class="sidebar-2">Sidebar 2</div>
  <div class="subgrid">
    <div>Subgrid cell 1</div>
    <div>Subgrid cell 2</div>
  </div>
</div>

We can use CSS Grid to create a responsive layout that adapts to different screen sizes.

.grid-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, 1fr);
}

.main-content {
  grid-column: 1 / 4;
  grid-row: 1 / 2;
}

.sidebar-1 {
  grid-column: 1 / 2;
  grid-row: 2 / 3;
}

.sidebar-2 {
  grid-column: 3 / 4;
  grid-row: 2 / 3;
}

.subgrid {
  display: grid;
  grid-template-columns: repeat(2, 0.5fr);
  grid-template-rows: 1fr;
  grid-column: 2 / 4;
  grid-row: 2 / 3;
}

In this example, our subgrid spans two columns and one row of the outer grid, aligning its cells with the second and third columns of the outer grid.

Conclusion

CSS Subgrid with nested grid alignment is a powerful technique for creating complex layouts on the web. By mastering this technique, you can build responsive, adaptable designs that scale across different screen sizes and devices. Remember to use the grid-column and grid-row properties to align your subgrid's cells with specific cells in the outer grid, and don't be afraid to experiment with different column templates using grid-template-columns. Happy coding!

Fullstackist aims to provide immersive and explanatory content for full stack developers Fullstackist aims to provide immersive and explanatory content for full stack developers
Backend Developer 103 Being a Fullstack Developer 107 CSS 109 Devops and Cloud 70 Flask 108 Frontend Developer 357 Fullstack Testing 99 HTML 171 Intermediate Developer 105 JavaScript 206 Junior Developer 124 Laravel 221 React 110 Senior Lead Developer 124 VCS Version Control Systems 99 Vue.js 108

Recent Posts

Web development learning resources and communities for beginners...

TL;DR As a beginner in web development, navigating the vast expanse of online resources can be daunting but with the right resources and communities by your side, you'll be well-equipped to tackle any challenge that comes your way. Unlocking the World of Web Development: Essential Learning Resources and Communities for Beginners As a beginner in web development, navigating the vast expanse of online resources can be daunting. With so many tutorials, courses, and communities vying for attention, it's easy to get lost in the sea of information. But fear not! In this article, we'll guide you through the most valuable learning resources and communities that will help you kickstart your web development journey.

Read more

Understanding component-based architecture for UI development...

Component-based architecture breaks down complex user interfaces into smaller, reusable components, improving modularity, reusability, maintenance, and collaboration in UI development. It allows developers to build, maintain, and update large-scale applications more efficiently by creating independent units that can be used across multiple pages or even applications.

Read more

What is a Single Page Application (SPA) vs a multi-page site?...

Single Page Applications (SPAs) load a single HTML file initially, handling navigation and interactions dynamically with JavaScript, while Multi-Page Sites (MPS) load multiple pages in sequence from the server. SPAs are often preferred for complex applications requiring dynamic updates and real-time data exchange, but MPS may be suitable for simple websites with minimal user interactions.

Read more