iT邦幫忙

2021 iThome 鐵人賽

DAY 15
0
Modern Web

30天搞定CSS及切版系列 第 15

CSS display:Grid

grid-template-areas

使用 grid-template-areas 定義每個區塊,grid-item再使用grid-area決定元素的位置。

<div class="wrapper">
   <div class="purple">purple</div>
   <div class="green" >green</div>
   <div class="yellow">yellow</div>
   <div class="red">red</div>
</div>
.wrapper {
  display: grid;
  grid-template-areas : 
    "purple purple green green green"
    "purple purple yellow yellow yellow"
    "red red red red red";
}
.purple
{
  border:1px solid black;
  grid-area:purple;
  background-color: purple;
}
.green
{
  border:1px solid black;
  grid-area:green;
  background-color: green;
}
.yellow
{
  border:1px solid black;
  grid-area:yellow;
  background-color: yellow;
}
.red
{
  border:1px solid black;
  grid-area:red;
  background-color: red;
}

使該區域無效可以用「.」

grid-template-areas : 
    "purple purple green green ."
    "purple purple yellow yellow yellow"
    "red red red red red";

可以在搭配上昨天講的grid-template-columns/rows 控制寬高。


.wrapper {
  display: grid;
  grid-template-columns : 50px 60px 100px 100px 100px;
  grid-template-rows : 30px 60px 90px;
  grid-template-areas : 
    "purple purple green green ."
    "purple purple yellow yellow yellow"
    "red red red red red";
}


上一篇
CSS display:grid
下一篇
SCSS
系列文
30天搞定CSS及切版16

尚未有邦友留言

立即登入留言