为什么我的嵌套网格容器不能创建等宽的列?

时间:2019-07-03 22:40:01

标签: html css css-grid

所以我是CSS网格的新手,并且一直在研究它。我试图查看我是否可以在一般意义上将该模型的布局与我编写的一些实验代码相匹配:https://ibb.co/GFYqMRx

这是我的HTML:

<!DOCTYPE html>
  <head>
    <meta charset="utf-8">
    <title></title>
    <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="normalize.css">
    <link rel="stylesheet" href="styles.css">
  </head>
  <body class="body">


    <div class="header">Header</div>
    <div class="side-navigation">Side Navigation</div>

    <main class="main">
      <div class="item search">Search</div>
      <div class="item traffic">Traffic</div>
      <div class="item daily-traffic">Daily Traffic </div>
      <div class="item mobile-users">Mobile Users</div>
      <div class="item social-stats">Social Stats</div>
      <div class="item new-members">New Members</div>
      <div class="item recent-activity">Recent Activity</div>
      <div class="item message-user">Message User</div>
      <div class="item settings">Settings</div>
    </main>


    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.bundle.min.js"></script> 
    <script src="app.js"></script>
  </body>
</html>

这是我的CSS:

* {
  box-sizing: border-box;
}
.item {
  border: 1px red solid;
}

.header {
  background-color: purple;
  grid-area: header;
}
.side-navigation {
  background-color: orange;
  grid-area: side-navigation;
}
.main {
  background-color: grey;
  grid-area: main;
}

.search {
  grid-area: search;
}
.traffic {
  grid-area: traffic;
}
.daily-traffic {
  grid-area: daily-traffic;
}
.mobile-users {
  grid-area: mobile-users;
}
.social-stats {
  grid-area: social-stats;
}
.new-members {
  grid-area: new-members;
}
.recent-activity {
  grid-area: recent-activity;
}
.message-user {
  grid-area: message-user;
}
.settings {
  grid-area: settings;
}
body {
  display: grid;
  grid-template-rows: 80px 80px 1fr;
  grid-template-areas: 
  "header"
  "side-navigation"
  "main";
}


@media (min-width: 769px) {

  body {
    grid-template-columns: 80px 1fr;
    grid-template-rows: 80px 1fr;
    grid-template-areas:
    "header header"
    "side-navigation main"
  }
  .main {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-auto-rows: 150px;
  } 
}

我首先能够通过操纵网格线来创建所需的布局,但是我想看看是否可以仅通过使用grid-template-areas属性来做同样的事情。

对于大于769px的屏幕,我将嵌套的主容器分为两列,每列1fr,但是最终由于安装了第三列而产生了一些奇怪的效果: enter image description here

为什么我不能在嵌套的主容器中创建等宽的列?如果我为grid-template-columns放置两个fr单元,是否不应该将我的网格平均分成两半?

一切正常,直到我将网格应用于嵌套的主容器。

为什么我的网格项目在屏幕底部这样混乱?

2 个答案:

答案 0 :(得分:1)

我认为这与没有grid-area:规则的所有grid-template-areas:标签有关,以定义它们的去向。我只是在开发工具中检查了您的代码,当我删除了这些grid-area:标签时,它变成了两列网格。

为.main添加一个网格模板区域

.main {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-auto-rows: 150px;
    grid-template-areas: 
      "search          traffic"
      "daily-traffic   mobile-users"
      "social-stats    new-members"
      "recent-activity message-user"
      "settings        ............";
}

网格模板区域中的所有多余(超过1个)空白都将被忽略,因此您可以添加额外的空间来对齐列。另外,请注意“。”最后,您使用“。”或倍数(例如多余的空白将被忽略)表示将该单元格留空。如果最后删除这些句点,整个过程都会中断,则必须在网格模板区域中考虑每个单元格。

Rachel Andrew在CSS网格上有很多很棒的材料。 https://www.youtube.com/watch?v=RssSS_xhv2E

答案 1 :(得分:0)

这是因为您在不使用main的grid-area属性的情况下,随主网格一起为元素提供了网格面积。我已在代码段中删除了这些内容,并在主网格中显示了两列相等的列:

* {
  box-sizing: border-box;
}

.item {
  border: 1px red solid;
}

.header {
  background-color: purple;
  grid-area: header;
}

.side-navigation {
  background-color: orange;
  grid-area: side-navigation;
}

.main {
  background-color: grey;
  grid-area: main;
}

.search {}

.traffic {}

.daily-traffic {}

.mobile-users {}

.social-stats {}

.new-members {}

.recent-activity {}

.message-user {}

.settings {}

body {
  display: grid;
  grid-template-rows: 80px 80px 1fr;
  grid-template-areas: "header" "side-navigation" "main";
}

@media (min-width: 769px) {
  body {
    grid-template-columns: 80px 1fr;
    grid-template-rows: 80px 1fr;
    grid-template-areas: "header header" "side-navigation main"
  }
  .main {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-auto-rows: 150px;
  }
}
<body>
  <div class="header">Header</div>
  <div class="side-navigation">Side Navigation</div>

  <main class="main">
    <div class="item search">Search</div>
    <div class="item traffic">Traffic</div>
    <div class="item daily-traffic">Daily Traffic </div>
    <div class="item mobile-users">Mobile Users</div>
    <div class="item social-stats">Social Stats</div>
    <div class="item new-members">New Members</div>
    <div class="item recent-activity">Recent Activity</div>
    <div class="item message-user">Message User</div>
    <div class="item settings">Settings</div>
  </main>


  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.bundle.min.js"></script>
  <script src="app.js"></script>
</body>