css中grid特性的应用详解

日期:2021-01-20 类型:科技新闻 

关键词:小游戏源代码,互动小游戏微信,自制小游戏,html5游戏,制作游戏大概多少钱

grid合理布局

加在父元素上的特性

grid-template-columns/grid-template-rows

  • 界定元素的行或列的宽高
  • 假如父元素被等分为了9等分,则,无论有是多少个子元素,都显示信息9等分
  • grid-template-columns: 33% 33% 33%;能够写成grid-template-columns:repeat(3, 33%);
.container {
  width: 200px;
  height: 200px;
  display: grid;
  background-color: coral;
  margin: 10px;
}

.container .item {
  border: 1px solid #ccc;
  background-color: chocolate;
}
.container1 {
  grid-template-columns: 33% 33% 33%;
  grid-template-rows: 33% 33% 33%;
}

grid-template-areas

  • 父元素的grid-template-areas相互配合子元素的grid-area界定网格地区
  • 1个句点表明1个空的网格模块
.container {
  width: 200px;
  height: 200px;
  display: grid;
  background-color: coral;
  margin: 10px;
}

.container .item {
  border: 1px solid #ccc;
  background-color: chocolate;
}
.container2 {
  grid-template-columns: 1fr 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr;
  grid-template-areas: "header header . footer"
    "main main . sidebar"
    "main main . sidebar";
}

.container2 .item⑴ {
  grid-area: header;
}

.container2 .item⑵ {
  grid-area: main;
}

.container2 .item⑶ {
  grid-area: sidebar;
}

.container2 .item⑷ {
  grid-area: footer;
}
<div class="container container2">
  <div class="item item⑴">header</div>
  <div class="item item⑵">main</div>
  <div class="item item⑶">sidebar</div>
  <div class="item item⑷">footer</div>
</div>

grid-column-gap/grid-row-gap/grip-gap

  • 指网格线的尺寸,还可以说是网格子项之间的间隔
.container {
  width: 200px;
  height: 200px;
  display: grid;
  background-color: coral;
  margin: 10px;
}

.container .item {
  border: 1px solid #ccc;
  background-color: chocolate;
}
.container3 {
  grid-template-columns: repeat(3, 30%);
  grid-template-rows: repeat(3, 30%);
  grid-column-gap: 2%;
  grid-row-gap: 2%;
}
<div class="container container3">
  <div class="item item⑴"></div>
  <div class="item item⑵"></div>
  <div class="item item⑶"></div>
  <div class="item item⑷"></div>
  <div class="item item⑸"></div>
  <div class="item item⑹"></div>
  <div class="item item⑺"></div>
  <div class="item item⑻"></div>
  <div class="item item⑼"></div>
</div>

justify-items/align-itemsjustify-items

  • 让子元素的內容和纵向列轴对齐
  • align-items让子元素的內容和横向行轴对齐
  • 两个特性都有4个值
  • 我的事例中,以便便捷比照,嵌套循环了grid网格
.container {
  width: 200px;
  height: 200px;
  display: grid;
  background-color: coral;
  margin: 10px;
}

.container .item {
  border: 1px solid #ccc;
  background-color: chocolate;
}

.container4 {
  width: 400px;
  height: 200px;
  grid-template-columns: repeat(4, 25%);
  grid-template-rows: repeat(2, 50%);
}
.container4 .item {
  display: grid;
}
.container4 .item div {
  background-color: coral;
  border: 1px dashed #aaa
}
.container4 .item⑴ {
  grid-template-columns: repeat(2, 50%);
  grid-template-rows: repeat(2, 50%);
  justify-items: start;
}
.container4 .item⑵ {
  grid-template-columns: repeat(2, 50%);
  grid-template-rows: repeat(2, 50%);
  justify-items: end;
}
.container4 .item⑶ {
  grid-template-columns: repeat(2, 50%);
  grid-template-rows: repeat(2, 50%);
  justify-items: center;
}
.container4 .item⑷ {
  grid-template-columns: repeat(2, 50%);
  grid-template-rows: repeat(2, 50%);
  justify-items: stretch;
}
.container4 .item⑸ {
  grid-template-columns: repeat(2, 50%);
  grid-template-rows: repeat(2, 50%);
  align-items: start;
}
.container4 .item⑹ {
  grid-template-columns: repeat(2, 50%);
  grid-template-rows: repeat(2, 50%);
  align-items: end;
}
.container4 .item⑺ {
  grid-template-columns: repeat(2, 50%);
  grid-template-rows: repeat(2, 50%);
  align-items: center;
}
.container4 .item⑻ {
  grid-template-columns: repeat(2, 50%);
  grid-template-rows: repeat(2, 50%);
  align-items: stretch;
}
<div class="container container4">
  <div class="item item⑴">
    <div>list</div>
    <div>list</div>
    <div>list</div>
    <div>list</div>
  </div>
  <div class="item item⑵">
    <div>list</div>
    <div>list</div>
    <div>list</div>
    <div>list</div>
  </div>
  <div class="item item⑶">
    <div>list</div>
    <div>list</div>
    <div>list</div>
    <div>list</div>
  </div>
  <div class="item item⑷">
    <div>list</div>
    <div>list</div>
    <div>list</div>
    <div>list</div>
  </div>
  <div class="item item⑸">
    <div>list</div>
    <div>list</div>
    <div>list</div>
    <div>list</div>
  </div>
  <div class="item item⑹">
    <div>list</div>
    <div>list</div>
    <div>list</div>
    <div>list</div>
  </div>
  <div class="item item⑺">
    <div>list</div>
    <div>list</div>
    <div>list</div>
    <div>list</div>
  </div>
  <div class="item item⑻">
    <div>list</div>
    <div>list</div>
    <div>list</div>
    <div>list</div>
  </div>

justify-content/align-content

  • 设定子元素的对齐方法,justify表明纵向,align表明横向
.container {
  width: 200px;
  height: 200px;
  display: grid;
  background-color: coral;
  margin: 10px;
}

.container .item {
  border: 1px solid #ccc;
  background-color: chocolate;
}

.container5 {
  width: 700px;
  height: 200px;
  grid-template-columns: repeat(7, 14%);
  grid-template-rows: repeat(2, 50%);
}

.container5 .item {
  display: grid;
}

.container5 .item div {
  background-color: coral;
  border: 1px dashed #aaa
}

.container5 .item⑴ {
  grid-template-columns: repeat(2, 40%);
  grid-template-rows: repeat(2, 40%);
  justify-content: start;
}

.container5 .item⑵ {
  grid-template-columns: repeat(2, 40%);
  grid-template-rows: repeat(2, 40%);
  justify-content: end;
}

.container5 .item⑶ {
  grid-template-columns: repeat(2, 40%);
  grid-template-rows: repeat(2, 40%);
  justify-content: center;
}

.container5 .item⑷ {
  grid-template-columns: repeat(2, 40%);
  grid-template-rows: repeat(2, 40%);
  justify-content: stretch;
}

.container5 .item⑸ {
  grid-template-columns: repeat(2, 40%);
  grid-template-rows: repeat(2, 40%);
  justify-content: space-around;
}

.container5 .item⑹ {
  grid-template-columns: repeat(2, 40%);
  grid-template-rows: repeat(2, 40%);
  justify-content: space-between;
}

.container5 .item⑺ {
  grid-template-columns: repeat(2, 40%);
  grid-template-rows: repeat(2, 40%);
  justify-content: space-evenly;
}

.container5 .item⑻ {
  grid-template-columns: repeat(2, 40%);
  grid-template-rows: repeat(2, 40%);
  align-content: start;
}

.container5 .item⑼ {
  grid-template-columns: repeat(2, 40%);
  grid-template-rows: repeat(2, 40%);
  align-content: end;
}

.container5 .item⑴0 {
  grid-template-columns: repeat(2, 40%);
  grid-template-rows: repeat(2, 40%);
  align-content: center;
}

.container5 .item⑴1 {
  grid-template-columns: repeat(2, 40%);
  grid-template-rows: repeat(2, 40%);
  align-content: stretch;
}

.container5 .item⑴2 {
  grid-template-columns: repeat(2, 40%);
  grid-template-rows: repeat(2, 40%);
  align-content: space-around;
}

.container5 .item⑴3 {
  grid-template-columns: repeat(2, 40%);
  grid-template-rows: repeat(2, 40%);
  align-content: space-between;
}

.container5 .item⑴4 {
  grid-template-columns: repeat(2, 40%);
  grid-template-rows: repeat(2, 40%);
  align-content: space-evenly;
}
<div class="container container5">
  <div class="item item⑴">
    <div>list</div>
    <div>list</div>
    <div>list</div>
    <div>list</div>
  </div>
  <div class="item item⑵">
    <div>list</div>
    <div>list</div>
    <div>list</div>
    <div>list</div>
  </div>
  <div class="item item⑶">
    <div>list</div>
    <div>list</div>
    <div>list</div>
    <div>list</div>
  </div>
  <div class="item item⑷">
    <div>list</div>
    <div>list</div>
    <div>list</div>
    <div>list</div>
  </div>
  <div class="item item⑸">
    <div>list</div>
    <div>list</div>
    <div>list</div>
    <div>list</div>
  </div>
  <div class="item item⑹">
    <div>list</div>
    <div>list</div>
    <div>list</div>
    <div>list</div>
  </div>
  <div class="item item⑺">
    <div>list</div>
    <div>list</div>
    <div>list</div>
    <div>list</div>
  </div>
  <div class="item item⑻">
    <div>list</div>
    <div>list</div>
    <div>list</div>
    <div>list</div>
  </div>
  <div class="item item⑼">
    <div>list</div>
    <div>list</div>
    <div>list</div>
    <div>list</div>
  </div>
  <div class="item item⑴0">
    <div>list</div>
    <div>list</div>
    <div>list</div>
    <div>list</div>
  </div>
  <div class="item item⑴1">
    <div>list</div>
    <div>list</div>
    <div>list</div>
    <div>list</div>
  </div>
  <div class="item item⑴2">
    <div>list</div>
    <div>list</div>
    <div>list</div>
    <div>list</div>
  </div>
  <div class="item item⑴3">
    <div>list</div>
    <div>list</div>
    <div>list</div>
    <div>list</div>
  </div>
  <div class="item item⑴4">
    <div>list</div>
    <div>list</div>
    <div>list</div>
    <div>list</div>
  </div>
</div>

grid-auto-columns/grid-auto-rowsgrid-column

  • 后边的值假如是1 / 2方式,表明的是从第1列网格线刚开始到第2列网格线完毕,假如这个特性界定的网格超过了父元素的范畴,则会全自动转化成隐式网格
  • grid-auto-columns和grid-auto-rows两个特性来特定这些隐式网格运动轨迹的宽度
.container {
  width: 200px;
  height: 200px;
  display: grid;
  background-color: coral;
  margin: 10px;
}

.container .item {
  border: 1px solid #ccc;
  background-color: chocolate;
}

.container6 {
  width: 120px;
  height: 180px;
  grid-template-columns: 60px 60px;
  grid-template-rows: 90px 90px;
  grid-auto-columns: 60px;
}
.container6 .item⑴ {
  grid-column: 1 / 2;
  grid-row: 2 / 3;
  border: 1px solid #ccc;
}
.container6 .item⑵ {
  grid-column: 5 / 6;
  grid-row: 2 / 3;
  border: 1px solid #ccc;
}
<div class="container container6">
  <div class="item⑴">1/2&2/3</div>
  <div class="item⑵">5/6&2/3</div>
</div>

grid-auto-flow

  • 沒有网格元素的情况下,全自动排序
  • row表明从左到右排序,column表明从上到下排序
.container {
  width: 200px;
  height: 200px;
  display: grid;
  background-color: coral;
  margin: 10px;
}

.container .item {
  border: 1px solid #ccc;
  background-color: chocolate;
}

.container7 {
  display: grid;
  width: 200px;
  height: 40px;
  grid-template-columns: 40px 40px 40px 40px 40px;
  grid-template-rows: 40px 40px;
  /* grid-auto-flow: row; */
  grid-auto-flow: column;
}
.container7 .item⑴ {
  grid-column: 1;
  grid-row: 1 / 3;
}

.container7 .item⑸ {
  grid-column: 5;
  grid-row: 1 / 3;
}
<div class="container container7">
  <div class="item item⑴">1</div>
  <div class="item item⑵">2</div>
  <div class="item item⑶">3</div>
  <div class="item item⑷">4</div>
  <div class="item item⑸">5</div>
</div>

加在子元素上的特性

grid-column-start/grid-column-end/grid-row-start/grid-row-end/grid-column/grid-row

  • 界定网格刚开始或完毕的部位
  • 值为数据,表明从这条线刚开始。值为span加数据,表明遮盖了这条线的部位
.container {
  width: 200px;
  height: 200px;
  display: grid;
  background-color: coral;
  margin: 10px;
}

.container .item {
  border: 1px solid #ccc;
  background-color: chocolate;
}

.container8 {
  margin-top: 20px;
  grid-template-columns: repeat(5, 20%);
  grid-template-rows: repeat(5, 20%);
}
.container8 .item⑴ {
  grid-column-start: 2;
  grid-column-end: 4;
  grid-row-start: 1;
  grid-row-end: 2;
}
.container8 .item⑵ {
  grid-column-start: 4;
  grid-column-end: span 5;
  grid-row-start: 2;
  grid-row-end: span 5;
}
.container8 .item⑶ {
  grid-column: 1 / span 2;
  grid-row: 2 / span 4;
}
<div class="container container8">
  <div class="item item⑴">item⑴</div>
  <div class="item item⑵">item⑵</div>
  <div class="item item⑶">item⑶</div>
</div>

justify-self/align-self

网格子项內容与格线对齐

.container {
  width: 200px;
  height: 200px;
  display: grid;
  background-color: coral;
  margin: 10px;
}

.container .item {
  border: 1px solid #ccc;
  background-color: chocolate;
}
.container9 {
  width: 400px;
  height: 200px;
  grid-template-columns: repeat(4, 25%);
  grid-template-rows: repeat(2, 50%);
}
.container9 .item⑴ {
  justify-self: start;
}
.container9 .item⑵ {
  justify-self: end;
}
.container9 .item⑶ {
  justify-self: center;
}
.container9 .item⑷ {
  justify-self: stretch;
}
.container9 .item⑸ {
  align-self: start;
}
.container9 .item⑹ {
  align-self: end;
}
.container9 .item⑺ {
  align-self: center;
}
.container9 .item⑻ {
  align-self: stretch;
}
<div class="container container9">
  <div class="item item⑴">item⑴</div>
  <div class="item item⑵">item⑵</div>
  <div class="item item⑶">item⑶</div>
  <div class="item item⑷">item⑷</div>
  <div class="item item⑸">item⑸</div>
  <div class="item item⑹">item⑹</div>
  <div class="item item⑺">item⑺</div>
  <div class="item item⑻">item⑻</div>
</div>

以上便是本文的所有內容,期待对大伙儿的学习培训有一定的协助,也期待大伙儿多多适用脚本制作之家。