要想了解grid布局,首先了解 repeat()
的作用。
repeat()
函数表示轨道列表的重复片段。grid-template-columns
和grid-template-rows
上才有效。repeat([auto-fill | auto-fit | ] , [ ? [ | ]] + ?)
auto-fit
,可以填满整个父元素。repeat(auto-fill, 1fr min-content)
<body>
<div class="container">
<div style="border: 1px solid black;height: 50px;">1div>
<div style="border: 1px solid black;height: 50px;">1div>
<div style="border: 1px solid black;height: 50px;">1div>
<div style="border: 1px solid black;height: 50px;">1div>
div>
<div id="container">
<div style="border: 1px solid black;height: 50px;">1div>
<div style="border: 1px solid black;height: 50px;">1div>
<div style="border: 1px solid black;height: 50px;">1div>
<div style="border: 1px solid black;height: 50px;">1div>
div>
body>
<style>
.container {
display: grid;
width: 600px;
background-color: #ccc;
grid-gap: 5px;
grid-template-columns: repeat(auto-fit, minmax(20px, 1fr));
border: 1px dashed skyblue;
}
#container {
display: grid;
width: 600px;
background-color: skyblue;
grid-gap: 5px;
grid-template-columns: repeat(auto-fill, minmax(20px, 1fr));
border: 1px dashed skyblue;
}
style>
.container {
...
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
...
}
#container {
...
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
...
}
<body>
<div class="container">
<div style="border: 1px solid black;height: 50px;">225255656sad56561div>
<div style="border: 1px solid black;height: 50px;">1div>
<div style="border: 1px solid black;height: 50px;">1div>
<div style="border: 1px solid black;height: 50px;">1div>
div>
body>
<style>
.container {
display: grid;
width: 300px;
background-color: #ccc;
grid-gap: 5px;
grid-template-columns: repeat(1, auto 20px);
border: 1px dashed skyblue;
}
style>
<body>
<div class="container">
<div style="border: 1px solid black;height: 50px;">225255656sad56561div>
<div style="border: 1px solid black;height: 50px;">1div>
<div style="border: 1px solid black;height: 50px;">1div>
<div style="border: 1px solid black;height: 50px;">1div>
div>
body>
<style>
.container {
display: grid;
width: 300px;
background-color: #ccc;
grid-gap: 5px;
grid-template-columns: repeat(1, max-content 20px);
border: 1px dashed skyblue;
}
style>
<body>
<div class="container">
<div style="border: 1px solid black;height: 50px;">225255656sad56561div>
<div style="border: 1px solid black;height: 50px;">1div>
<div style="border: 1px solid black;height: 50px;">1div>
<div style="border: 1px solid black;height: 50px;">1div>
div>
body>
<style>
.container {
display: grid;
width: 300px;
background-color: #ccc;
grid-gap: 5px;
grid-template-columns: repeat(1, min-content 20px);
border: 1px dashed skyblue;
}
style>
display: grid
:块级元素。<body>
1
<div id="container">
<div>div>
<div>div>
<div>div>
<div>div>
<div>div>
<div>div>
<div>div>
<div>div>
div>
2
body>
<style>
#container {
display: grid;
grid: repeat(2, 60px) / auto-flow 80px;
}
#container > div {
background-color: #8ca0ff;
width: 50px;
height: 50px;
}
style>
display: inline-grid
:行内元素。<body>
1
<div id="container">
<div>div>
<div>div>
<div>div>
<div>div>
<div>div>
<div>div>
<div>div>
<div>div>
div>
2
body>
<style>
#container {
display: inline-grid;
grid: repeat(2, 60px) / auto-flow 80px;
}
#container > div {
background-color: #8ca0ff;
width: 50px;
height: 50px;
}
style>
grid
:可以用来设置多个属性。比如grid-template-rows、grid-template-columns 和 grid-template-areas等等。类似于 background 可以设置 bgColor、bgPosition。grid-template
:用以定义网格中行、列与分区。grid-template-columns
:基于网格列的维度,定义网格线的名称和网格轨道的尺寸大小grid-template-rows
:基于网格行的维度,定义网格线的名称和网格轨道的尺寸大小<body>
<div id="container">
<div>div>
<div>div>
<div>div>
<div>div>
<div>div>
<div>div>
<div>div>
<div>div>
div>
body>
<style>
#container {
display: inline-grid;
grid-gap: 5px;
grid-template-columns: repeat(2, 50px);
grid-template-rows: repeat(4, 50px);
}
#container > div {
background-color: #8ca0ff;
width: 50px;
height: 50px;
}
style>
grid-row-gap
:行间距grid-column-gap
:列间距grid-gap
:行列间距<body>
<div id="container">
<div>div>
<div>div>
<div>div>
<div>div>
<div>div>
<div>div>
<div>div>
<div>div>
div>
body>
<style>
#container {
display: inline-grid;
grid-column-gap: 10px;
grid-row-gap: 20px;
grid-template-columns: repeat(2, 50px);
grid-template-rows: repeat(4, 50px);
}
#container > div {
background-color: #8ca0ff;
width: 50px;
height: 50px;
}
style>
grid-template-areas
用于划分网格区域。grid-area
用于指定网格所属哪个区域。.
表示不会用到这个区域。<body>
<div id="container">
<header>headerheader>
<nav>navnav>
<main>mainmain>
<footer>footerfooter>
div>
body>
<style>
#container {
display: grid;
grid-template-columns: 50px 200px;
grid-template-rows: 50px 200px 50px;
grid-template-areas: ". head"
"nav main"
"foot foot";
}
header {
grid-area: head;
background-color: #fedcba;
}
nav {
grid-area: nav;
background-color: #abcdef;
}
main {
grid-area: main;
background-color: #654321;
}
footer {
grid-area: foot;
background-color: #98abcd;
}
style>
grid-auto-flow
控制着自动布局算法怎样运作,精确指定在网格中被自动布局的元素怎样排列。(指定排列顺序)grid-auto-flow: [ row | column ] || dense
justify-items
:内容水平对齐(start | end | center | stretch)。align-items
:内容垂直对齐(start | end | center | stretch)。place-items
:可以同时设置垂直对齐和水平对齐。stretch
,铺满网格。justify-content
:容器水平对齐(start | end | center | stretch | space-around | space-between | space-evenly)。align-content
:容器垂直对齐(start | end | center | stretch | space-around | space-between | space-evenly)。place-content
:同时设置垂直对齐和水平对齐。<body>
<div id="grid">
<div>1div>
<div>2div>
<div>3div>
<div>4div>
<div>5div>
<div>6div>
<div>7div>
<div>8div>
<div>9div>
div>
body>
<style>
#grid {
height: 500px;
width: 500px;
display: grid;
grid-gap: 10px;
grid-template: repeat(3, 50px) / repeat(3, 50px);
grid-auto-flow: row;
background-color: #f70;
place-items: stretch stretch;
/* space-around space-between space-evenly */
justify-content: space-around;
}
#grid > div{
background-color: #acd;
}
style>
grid-template-columns
显式指定尺寸的列中,隐式的 grid 轨道就会被创建出来支撑它。显式地定位到超出范围的列中,或者通过自动布局算法创建额外的列,就会发生上述情况。grid-auto-rows
、grid-auto-columns
设置隐式网格大小。<body>
<div id="grid">
<div id="item1">div>
<div id="item2">div>
<div id="item3">div>
div>
body>
<style>
#grid {
height: 100px;
display: grid;
grid-template-areas: "a a";
grid-gap: 10px;
grid-auto-columns: 200px;
}
#grid > div {
background-color: lime;
}
style>
area-name 4
为该区域的第四行/列。<body>
<div class="wrapper">
<div class="box1">Onediv>
<div class="box2">Twodiv>
<div class="box3">Threediv>
<div class="box4">Fourdiv>
<div class="box5">Fivediv>
div>
body>
<style>
.wrapper {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 100px;
}
.wrapper > div {
border: 1px solid #000;
}
.box1 {
grid-column-start: 1;
grid-column-end: 4;
grid-row-start: 1;
grid-row-end: 3;
}
.box2 {
grid-column-start: 1;
grid-row-start: 3;
grid-row-end: 5;
}
style>