CSS实现栅格布局

时间:2023-03-09 14:14:42
CSS实现栅格布局

CSS实现栅格布局

设置容器container:

.grid-container {
width: 100%;
max-width: 1200px;
}

清除浮动:

.row:before, .row:after {
content: "";
display: block;
height:;
width:;
visibility: hidden;
clear: both;
}

假设有12列布局:

[class*='col_'] {
float: left;
min-height: 1px;
width: 8.33%;
box-sizing: border-box;
-moz-box-sizing: border-box;
-o-box-sizing: border-box;
-ms-box-sizing: border-box;
-webkit-box-sizing: border-box;
}
.col_1 { width: 8.33%; }
.col_2 { width: 16.66%; }
.col_3 { width: 25%; }
.col_4 { width: 33.33%; }
.col_5 { width: 41.66%; }
.col_6 { width: 50%; }
.col_7 { width: 58.33%; }
.col_8 { width: 66.66%; }
.col_9 { width: 75%; }
.col_10 { width: 83.33%; }
.col_11 { width: 91.66%; }
.col_12 { width: 100%; }

实例:

 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="style.css">
<title>自适应布局</title>
<style type="text/css">
body {
margin: 0;
}
.header {
background-color: #000;
color: #fff;
height: 5vh;
text-align: center;
line-height: 5vh;
}
.footer {
width: 100%;
background-color: #000;
color: #fff;
height: 5vh;
text-align: center;
line-height: 5vh;
position: fixed;
bottom: 0;
}
.grid-container {
margin: 0 auto
}
.col_3 {
height: 90vh;
background-color: #ddd;
}
.col_9 {
height: 90vh;
background-color: #ccc;
}
</style> <script type="text/javascript">
let width = document.documentElement.getBoundingClientRect().width;
// 1rem = 1vw;
let rem = width/100;
document.documentElement.style.fontSize = rem+'px';
</script>
</head>
<body>
<header class="header">header</header>
<div class="grid-container">
<div class="row">
<div class="col_3">col_3</div>
<div class="col_9">col_9</div>
</div>
</div>
<footer class="footer">footer</footer>
</body>
</html>