
批改狀態(tài):合格
老師批語:
設置最小臨界寬度以及不同的樣式以實現頁面樣式根據寬度變化
CSS
<style>
body {
background-color: burlywood;
}
@media screen and (min-width: 400px) {
body {
background-color: cadetblue;
font-size: 20px;
}
}
@media screen and (min-width: 600px) {
body {
background-color: darkblue;
font-size: 30px;
}
}
</style>
寬度 <400px時,
400px< 寬度 <600px時,
寬度 >600px時,
設置容器 display: grid
會把項目轉為塊元素(flex是轉為行內)
/* 1. grid 容器 */
.container {
/* 轉為網格容器 */
display: grid;
grid-template-columns: auto auto auto;
border: 1px solid #000;
padding: 0.5rem;
}
.container>.item {
border: 1px solid;
background-color: lightsteelblue;
padding: 0.5rem;
}
默認
設置為grid布局,3列, 寬度自適應
設置行數,行高
/* 1. grid 容器 */
.container {
/* 轉為網格容器 */
display: grid;
/* 3 columns */
grid-template-columns: auto auto auto;
/* 2 rows */
grid-template-rows: 5em 5em;
border: 1px solid #000;
padding: 0.5rem;
}
設置網格間隙
.container {
border: 1px solid #000;
padding: 0.5rem;
/* 轉為網格容器 */
display: grid;
/* 軌道列寬 3 columns */
grid-template-columns: auto auto auto;
/* 軌道行高 2 rows */
grid-template-rows: 5em 5em;
/* 軌道間隙 水平 垂直*/
gap: 0.5em;
}
(可與其他單位共同使用,總-固定值)
.container {
border: 1px solid #000;
padding: 0.5rem;
/* 轉為網格容器 */
display: grid;
/* 軌道列寬 3 columns */
/* grid-template-columns: auto auto auto; */
grid-template-columns: 1fr 2fr 1fr;
/* 軌道行高 2 rows */
grid-template-rows: 5em 5em;
/* 軌道間隙 水平 垂直*/
gap: 0.5em;
}
注:repeat與()間不能有空格
repeat(列數,寬度)
repeat(3, 10em)
repeat(3, 10em 2em)
/* grid-template-columns: 10em 10em 10em; */
grid-template-columns: repeat(3, 10em);
grid-template-columns: repeat(3, 10em 2em);
minmax(最小寬度,最大寬度)
設置前minmax前
grid-template-columns: 1fr 2fr 1fr;
設置minmax(30em, 2fr)后
grid-template-columns: 1fr minmax(30em, 2fr) 1fr;
項目數量 > 行*列
grid-auto-flow: row;(默認)
默認行優(yōu)先,即項目先水平排列,空間不夠再換行
grid-auto-flow: column
列優(yōu)先,即項目先垂直排列
例:
容器有9個item
<div class="container">
<div class="item">item1</div>
<div class="item">item2</div>
<div class="item">item3</div>
<div class="item">item4</div>
<div class="item">item5</div>
<div class="item">item6</div>
<div class="item">item7</div>
<div class="item">item8</div>
<div class="item">item9</div>
</div>
設置grid 布局
.container {
display: grid;
border: 1px solid;
padding: 0.5em;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: 5em 5em;
}
.container>.item {
background-color: lightsteelblue;
border: 1px solid;
padding: 0.5em;
}
效果 (item7,8,9在隱式軌道上)
設置grid-auto-flow: column;
grid-auto-rows
設置隱式軌道行的高度
設置grid-auto-rows:5em 后,高度與前面的item相同
.container {
display: grid;
border: 1px solid;
padding: 0.5em;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: 5em 5em;
/* grid-auto-flow: column; */
grid-auto-rows: 5em;
}
grid-auto-columns
設置隱式軌道列的寬度
設置grid-auto-columns:1fr 后,寬度與前面的item相同
.container {
display: grid;
border: 1px solid;
padding: 0.5em;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: 5em 5em;
grid-auto-flow: column;
grid-auto-columns: 1fr;
}
grid-area: 行起始編號/列起始編號/行結束編號/列結束編號
將下圖中item5變色并放到item9的位置
.container>.item:nth-of-type(5) {
background-color: yellow;
grid-area: 2/2/3/3;
grid-area: 3/3/4/4;
}
.container>.item:nth-of-type(5) {
background-color: yellow;
/* grid-area: 2/2/3/3; */
grid-area: 2/2;
/* grid-area: 3/3/4/4; */
grid-area: 3/3;
}
.container>.item:nth-of-type(5) {
background-color: yellow;
/* grid-area: 2/2/3/3; */
grid-area: 2/2;
/* grid-area: 3/3/4/4; */
grid-area: 3/3;
grid-area: 2/2/4/4;
}
也可用:
span 跨越行數
(span 1 可用auto代替)
.container>.item:nth-of-type(5) {
background-color: yellow;
/* grid-area: 2/2/3/3; */
grid-area: 2/2;
/* grid-area: 3/3/4/4; */
grid-area: 3/3;
/* grid-area: 2/2/4/4; */
grid-area: 2/2/span 2/span 2;
/* 簡寫: */
/* grid-area: span 2/span 2; */
}
前提:設置了寬高,每個項目在其單元格中有剩余空間
place-items: 垂直對齊方式 水平對齊方式;
(默認值place-items:stretch; 不設置寬高有效)
.container {
/* place-items: 垂直對齊方式 水平對齊方式; */
place-items: start center;
}
place-self: 垂直對齊方式 水平對齊方式;
.container>.item:nth-of-type(6) {
place-self: end center;
}
place-content: 垂直對齊方式 水平對齊方式;
.container {
display: grid;
background-color: lightyellow;
height: 25em;
border: 1px solid;
padding: 0.5em;
grid-template-columns: repeat(3, 10em);
grid-template-rows: 5em 5em;
gap: 0.5em;
grid-auto-rows: 5em;
/* grid-auto-flow: column; */
/* grid-auto-columns: 1fr; */
place-content: end center;
}
place-content: space-between space-between
.container {
place-content: space-between space-between;
}
在容器中寫出命名的布局:
body {
height: 20em;
display: grid;
/* grid-template-rows: 15em 1fr 15em; */
grid-template-rows: 15em minmax(50vw, auto) 15em;
grid-template-columns: 3em minmax(50vh, auto) 3em;
gap: 0.5em;
grid-template-areas:
"h h h"
"l m r"
"f f f";
}
header {
grid-area: h;
}
footer {
grid-area: f;
}
main {
grid-area: m;
}
aside.left {
grid-area: l;
}
aside.right {
grid-area: r;
}
以一行為起點,將其平分為12份,控制每個項目所占的列數(份)實現
例:
HTML
<body>
<div class="container">
<!-- 先定義行 -->
<!-- 1 -->
<div class="row">
<span class="item col-12">col12</span>
</div>
<!-- 2 -->
<div class="row">
<span class="item col-6">col6</span>
<span class="item col-6">col6</span>
</div>
<!-- 3 -->
<div class="row">
<span class="item col-4">col6</span>
<span class="item col-4">col6</span>
<span class="item col-4">col6</span>
</div>
<!-- 2:10 -->
<div class="row">
<span class="item col-2">col6</span>
<span class="item col-10">col6</span>
</div>
</div>
</body>
CSS
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
width: 100vw;
height: 100vh;
display: grid;
place-content: center;
}
.container {
min-width: 80vw;
display: grid;
gap: 0.5em;
}
.container > .row {
display: grid;
grid-template-columns: repeat(12, 1fr);
min-height: 3em;
gap: 0.5em;
}
.container > .row > .item {
padding: 1em;
border: 1px solid;
}
.col-12 {
grid-area: auto / span 12;
}
.col-11 {
grid-area: auto / span 11;
}
.col-10 {
grid-area: auto / span 10;
}
.col-9 {
grid-area: auto / span 9;
}
.col-8 {
grid-area: auto / span 8;
}
.col-7 {
grid-area: auto / span 7;
}
.col-6 {
grid-area: auto / span 6;
}
.col-5 {
grid-area: auto / span 5;
}
.col-4 {
grid-area: auto / span 4;
}
.col-3 {
grid-area: auto / span 3;
}
.col-2 {
grid-area: auto / span 2;
}
.col-1 {
grid-area: auto / span 1;
}
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號