CSS Grid 布局基础
grid 关键词解释
grid-template-columns: repeat(3, 1fr):
template:模板;
column:纵列;
repeat:重复;
(3, 1fr):将页面分成3纵列、每一列宽度为1fr(fr是自适应长度单位);
grid-gap: 10px:间隙大小10px;
grid-auto-rows: minmax(100px, auto);
auto:自动设置;
rows:每一行的长度;
minmax():设置最小最大值;
grid-column: 设置元素列的区域范围;
grid-row:设置元素行的区域范围;
代码实例:
.wrapper { display: grid; grid-template-columns: repeat(3, 1fr); grid-gap: 10px; grid-auto-rows: minmax(100px, auto); } .one { grid-column: 1 / 3; grid-row: 1; } .two { grid-column: 2 / 4; grid-row: 1 / 3; } .three { grid-column: 1; grid-row: 2 / 5; } .four { grid-column: 3; grid-row: 3; } .five { grid-column: 2; grid-row: 4; } .six { grid-column: 3; grid-row: 4; } .wrapper div{ background-color: rgb(251, 205, 145); border: 2px solid #666; opacity: .7; border-radius :5px; text-align: center; }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <link rel="stylesheet" href="./class2.css"> <title>Document</title> </head> <body> <div class="wrapper"> <div class="one">One</div> <div class="two">Two</div> <div class="three">Three</div> <div class="four">Four</div> <div class="five">Five</div> <div class="six">Six</div> </div> </body> </html>