网格模板区域-显示问题

时间:2019-04-10 22:40:07

标签: html css css3 css-grid

我是CSS网格中的新手,现在我正在使用它。我要制作4x4网格模板。

在我的代码中,此网格显示不正确-问题出在Table_4上。为什么显示不正确?你们能告诉我我做错了吗?

body {
  background-color: gray;
}
.grid {
  display: grid;
  width: 100%;
  height: 250px;
  grid-template-rows: 1fr 1fr 1fr 1fr;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  grid-template-areas: 
    "item-a item-a item-a item-a"
    "item-b . . ."
    "item-b . . ."
    "item-b . . .";  
}
.item-a {
  grid-area: item-a;
  background-color: lightcoral;
  text-align: center;
}
.item-b {
  grid-area: item-b;
  background-color: lightblue;
  text-align: center;
}
.item-c {
  grid-area: item-c;
  background-color: lightcyan;
  text-align: center;
}
.item-d {
  grid-area: item-d;
  background-color: lightgreen;
  text-align: center;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>

  <link rel="stylesheet" type="text/css" href="../sources/css/style.css">
</head>
<body>
  <div class="grid">
    <div class="item-a">Table_1</div>
    <div class="item-b">Table_2</div>
    <div class="item-c">Table_3</div>
    <div class="item-d">Table_4</div>
  </div>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

您需要在网格模板区域中列出item-c:

body {
  background-color: gray;
}

.grid {
  display: grid;
  width: 100%;
  height: 250px;
  grid-template-rows: 1fr 1fr 1fr 1fr;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  grid-template-areas: "item-a item-a item-a item-a" "item-b item-c item-c item-c" "item-b item-c item-c item-c" "item-d item-d item-d item-d";
}

.item-a {
  grid-area: item-a;
  background-color: lightcoral;
  text-align: center;
}

.item-b {
  grid-area: item-b;
  background-color: lightblue;
  text-align: center;
}

.item-c {
  grid-area: item-c;
  background-color: lightcyan;
  text-align: center;
}

.item-d {
  grid-area: item-d;
  background-color: lightgreen;
  text-align: center;
}
<div class="grid">
  <div class="item-a">Table_1</div>
  <div class="item-b">Table_2</div>
  <div class="item-c">Table_3</div>
  <div class="item-d">Table_4</div>
</div>