嵌套CSS网格是不好的做法吗? [英] Is it bad practice to nest CSS Grids?

查看:84
本文介绍了嵌套CSS网格是不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用组件驱动的前端框架(例如Angular),最后学习CSS Grid.

I'm experimenting with component driven front end frameworks, such as Angular, and finally learning CSS Grid.

我的问题是:嵌套 CSS Grids是不明智的做法吗?

My question is: is it bad practice to nest CSS Grids?

我在这里所做的是在我的main/root组件中,我已经使用css grid做了两件事:navbar和主要内容区域,因为navbar将出现在整个应用程序以及主要内容中.

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.

正如您在下面看到的,在根级别上的网格然后在<nav-bar>组件中的另一个网格.而且在主要内容区域中,我使用的每个/任何Angular组件中还会有更多的网格,可能是网格.

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      *
*                    *
**********************

下面的示例代码:

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>

推荐答案

嵌套网格容器没有错或无效.

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:

网格容器可以根据需要嵌套或与flex容器混合以创建更复杂的布局.

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.

此处有更多详细信息:

  • Grid properties not working on elements inside grid container
  • Positioning content of grid items in primary container (subgrid feature)

这篇关于嵌套CSS网格是不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆