grid网格布局
grid-area:2/4/4/5; 第几行开始/第几列开始/到第几行结束/到第几列结束
grid-template-columns: repeat(auto-fill,minmax(300px,1fr));
自适应的布局
.container{ width: 80%; margin: 0 auto;padding: 20px;box-sizing: border-box; display: grid; grid-template-columns: repeat(auto-fill,minmax(300px,1fr)); gap: 10px; box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1); border: 1px solid ;overflow: hidden; } @media screen and (max-width:800px) { .container{width: 100%; grid-template-columns: repeat(auto-fill,minmax(100%,1fr)); } }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> * { padding: 0; margin: 0; } .parent { background: red; height: 200px;width: 800px;margin: 0 auto; display: grid; grid-template-rows: 1fr 1fr 1fr 1fr; grid-template-columns: 1fr 1fr 1fr 1fr; grid-gap: 5px 8px; } .parent div{background-color: pink} .div1 { grid-area: 1/1/3/3; background-color: red; } .div2 { grid-area:1/3/1/5 ; } .div3 { grid-area:2/3/4/4; } .div4 { grid-area:2/4/4/5; } .div5 { grid-area:3/1/5/3 ; } </style> </head> <body> <div class="parent"> <div class="div1">a111</div> <div class="div2">a222 </div> <div class="div3">a333 </div> <div class="div4">a4444 </div> <div class="div5"> a555</div> </div> </body> </html>
grid-area:2/4/4/5;