我正在尝试组件驱动的前端框架,例如 Angular,最后学习 CSS Grid。
我的问题是:嵌套是不好的做法CSS Grids
?
我在这里所做的是在我的主/根组件中,我使用 css 网格做了两件事:导航栏和主要内容区域,因为导航栏将出现在整个应用程序和主要内容中.
正如您在下面看到的,根级别的网格然后是 <nav-bar>
中的另一个网格成分。在主要内容区域,会有更多,可能是我使用的每个/任何 Angular 组件中的网格。
********************** ******************************
* Navbar * => * img | nav | logout *
********************** ******************************
**********************
* *
* Content *
* *
**********************
示例代码如下:
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>
然后 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>
最佳答案
嵌套网格容器没有任何错误或无效。
grid specification不禁止,甚至不告诫这种做法。 It says this :
Grid containers can be nested or mixed with flex containers as necessary to create more complex layouts.
事实上,嵌套网格容器是您必须做的,以便将网格属性应用于顶级容器的后代,因为网格布局仅在父元素和子元素之间起作用。
更多细节在这里:
关于html - 嵌套 CSS 网格是不好的做法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52212891/