I'm experimenting with component driven front end frameworks, such as Angular, and finally learning CSS Grid.
My question is: is it bad practice to nest CSS Grids
?
What I've done here is in my main/root component, I've used css grid to make two things: the navbar and the main content area, since navbar will be present in the entire app and also the main content.
As you can see below, the grid on the root level then another grid in the <nav-bar>
component. And in the main content area, there will be many more, probably a grid in each/any Angular component I use.
********************** ******************************
* Navbar * => * img | nav | logout *
********************** ******************************
**********************
* *
* Content *
* *
**********************
Example code below:
app.component.html
<div class="container">
<div class="item-navbar"></div>
<div class="item-nav">
<nav-bar></nav-bar>
</div>
<div class="item-content">
<router-outlet></router-outlet>
</div>
</div>
<!-- With this CSS: -->
<style>
.container {
display: grid;
grid: ". nav ."
". content ."
/ 3vh auto 3vh;
row-gap: 1vh;
}
.item-navbar {
grid-area: 1 / 1 / 2 / 4;
position: relative;
z-index: -1;
background: #579C87;
box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
}
.item-nav {
grid-area: nav;
}
.item-content {
grid-area: content;
background: #D1C7B8;
box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
}
</style>
then nav-bar.component.html
<nav class="navbar" role="navigation" aria-label="main navigation">
<div class="navbar-brand">
<a class="navbar-item" routerLink="/">
<div class="img">
<img src="logo.jpg">
</div>
</a>
</div>
<div class="navbar-menu">
<a routerLink="/dashboard" class="navbar-item">Dashboard</a>
</div>
<div class="navbar-logout">
<a routerLink="/logout" class="navbar-item">Logout</a>
</div>
</nav>
<!-- with this CSS: -->
<style>
.navbar {
display: grid;
grid-template-columns: 64px auto auto;
grid-template-rows: auto;
grid-template-areas: "image navs logout";
gap: 1vh;
}
.navbar-brand {
grid-area: image;
place-self: center / start;
}
.navbar-menu {
grid-area: navs;
place-self: center start;
}
.navbar-logout {
grid-area: logout;
place-self: center end;
}
</style>
Introduction to subgridYou can "nest" grids by making a grid item a grid container. These grids however are independent of the parent grid and of each other, meaning that they do not take their track sizing from the parent grid. This makes it difficult to line nested grid items up with the main grid.
CSS Grid may seem a bit daunting with new syntax and layout ideas, but it's fairly simple and can be broken down into a handful of powerful concepts that when used together will blow your mind and change the way you create layouts for the web forever.
CSS Grid is a tool you can use to help create layouts for your website. It's especially useful if you need to think about the position, layers, or sizes of different elements. CSS Grid is complicated and there are many things to learn. But the good news is that you don't need to know everything all at once.
If we want to place the content precisely on a page, then CSS Grid is better, but if we want the flexibility of the layout, then we should go for Bootstrap.
With CSS Grid, it is possible to nest grids inside other grids by converting grid items into grid containers. This gives developers more freedom when coding a design into HTML and CSS and increases the productivity during the layout process of design mockups.
If you open the grid inspector of your browser, you will have the option to select either of the grids on the page. With CSS Grid, it is possible to nest grids inside other grids by converting grid items into grid containers.
The nested grid can influence the size of the parent grid track that contains it. This happens when the parent grid track is flexible enough to allow it — its size should be defined using auto, fr, min-content, max-content, or fit-content.
Finally, a nested grid container allows the creation of implicit grid tracks. Implicit grid tracks are tracks created automatically by the auto-placement algorithm. This happens when the required definition of grid tracks is missing — for example, when there are more elements to be placed than cells in the grid.
There is nothing wrong or invalid with nesting grid containers.
The grid specification doesn't prohibit, or even admonish, against the practice. It says this:
Grid containers can be nested or mixed with flex containers as necessary to create more complex layouts.
In fact, nesting grid containers is what you must do to apply grid properties to the descendants of a top-level container, since grid layout works only between parent and child elements.
More details here:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With