方法一:用float方法实现
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>一个全等四宫格的实现</title> <style> .parent{ background-color:#fff; width: 500px; height: 400px; margin-left: -10px; margin-top: -10px; } .child{ float: left; width: 50%; height: 50%; padding-left: 10px; padding-top: 10px; box-sizing: border-box; background-clip: content-box; background-color: #009899; } </style> </head> <body> <div class="parent"> <div class="child"></div> <div class="child"></div> <div class="child"></div> <div class="child"></div> </div> </body> </html>
三法并用:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> /*方法1:float*/ .container1{width:400px;height:400px;border:1px solid red;margin-top:-10px;margin-left:-10px;} .column1{float:left;background: blue;width:50%;height:50%;padding-left: 10px; padding-top:10px;box-sizing:border-box; background-clip:content-box;} /*方法2:table*/ .container-fix{margin-left:-10px; margin-top:-10px;} .container2{display: table;width:400px; height:400px; table-layout: fixed;} .row2{display: table-row;} .column2{display: table-cell;background: blue;width:50%; height:50%; padding-left: 10px; padding-top: 10px; background-clip:content-box;} p{height:100%;} /*方法3:flex*/ .container3{width:400px; height:400px;} .row3{display: flex; height:50%;} .column3{flex:1; background: blue;margin-left: 10px;margin-top: 10px; } </style> </head> <body> <div class="container1"> <div class="column1"></div> <div class="column1"></div> <div class="column1"></div> <div class="column1"></div> </div> <div class="container-fix"> <div class="container2"> <div class="row2"> <div class="column2"><p></p></div> <div class="column2"><p></p></div> </div> <div class="row2"> <div class="column2"><p></p></div> <div class="column2"><p></p></div> </div> </div> </div> <div class="container3"> <div class="row3"> <div class="column3"><p></p></div> <div class="column3"><p></p></div> </div> <div class="row3"> <div class="column3"><p></p></div> <div class="column3"><p></p></div> </div> </div> </body> </html>
时间: 2024-12-10 18:02:53