批改状态:合格
老师批语:有了grid, 是不是可以放弃好多东西了
display: grid
<style>.container {background: lightcyan;width: 400px;height: 400px;display: grid;/* 按列布局 */grid-auto-flow: column;grid-template-rows: 100px 100px 100px;}.items {background: lightblue;}</style>

举例:自动填充
<style>.container {background: lightcyan;width: 400px;height: 400px;display: grid;/* 自动填充 */grid-template-columns: repeat(auto-fill, 100px);grid-template-rows: repeat(auto-fill, 100px);}.items {background: lightblue;}</style>

举例:
<style>.container {background: lightcyan;width: 400px;height: 400px;display: grid;/* 重复设置 */grid-template-columns: repeat(4, 1fr);grid-template-rows: repeat(4, 1fr);}.items {background: lightblue;}/* 第三行开始占2行,从第一条列线到第二条,占一列 */.items.item3 {background-color: aqua;grid-row: 3/ span2;grid-column: 1 / 2;}</style>

<style>.container {background: lightcyan;width: 400px;height: 400px;display: grid;/* 重复*/grid-template-columns: repeat(4, 1fr);grid-template-rows: repeat(4, 1fr);}.items {background: lightblue;}/* 第三格子从第一条行线到第4条,占2行,从第二列线,到第四列线,占2列 */.items.item3 {background-color: aqua;grid-area: 2/2/4/4;}</style>

<style>.container {background: lightcyan;width: 400px;height: 400px;display: grid;grid-template-columns: repeat(3, 1fr);grid-template-rows: 40px 1fr 40px;grid-template-areas:"header header header""left main right""footer footer footer";}.items {background: lightblue;}/* 第三格子设置到left命名区域 */.items.item3 {background-color: aqua;grid-area: left;}/* 将第五格子设置为footer区域,同名区域合并 */.items.item5 {background-color: red;grid-area: footer;}</style>

<style>.container {background: lightcyan;width: 400px;height: 400px;display: grid;grid-template-columns: repeat(2, 1fr);grid-template-rows: 40px 1fr 40px;grid-template-areas: "header header" ". ." "footer footer";}.items {background: lightblue;}/* 第1格子设置通过hearder区域线名 */.items.item1 {background-color: aqua;/*起始行/起始列/结束行/结束列*/grid-area: header-start / header-start / header-end /header-end;}/* 将第4格子设置为footer区域,同名区域合并 */.items.item4 {background-color: red;grid-area: footer-start /footer-start /footer-end/footer-end;}</style>

<style>.container {background: lightcyan;width: 500px;height: 500px;display: grid;grid-template-columns: repeat(3, 50px);grid-template-rows: 50px 50px 50px;/* 水平均匀分布 *//* justify-content: space-evenly;/* 垂直2端对齐 *//* align-content: space-between; *//*值顺序是水平对其/垂直对齐*/place-content: space-between space-evenly;}.items {background: lightblue;}</style>

值:默认stretch, start,end,center
<style>.container {background: lightcyan;width: 500px;height: 500px;display: grid;grid-template-columns: repeat(3, 1fr);grid-template-rows: repeat(3, 1fr);/* 水平处于下方 *//* justify-items: end;/* 垂直居中 *//* align-items: center; *//*值顺序是水平/垂直*/place-items: end center;}.items {background: lightblue;width: 50px;height: 50px;}</style>

.items.item5 {background: yellow;place-self:center center;}

<style>.container {background: lightcyan;width: 500px;height: 500px;display: grid;grid-template-columns: repeat(3, 1fr);grid-template-rows: repeat(3, 1fr);/*值顺序是行/列*/gap: 10px 15px;}.items {background: lightblue;}</style>

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号