使用一个包裹层将页面主题包裹起来,设置width和margin就可以使整个页面居中。
width是页面内容区的宽度,margin是外边距,将左右外边距设为auto后,变可以自动计算左右外边距导致页面居中。
<html>
<head>
<style>
* {padding: 0;margin: 0;}
.box1 {background-color: red;height: 150px;}
.box2 {background-color: green;height: 150px;}
.box3 {background-color: blue;height: 150px;}
.wrap {width: 600px;margin: 0 auto;}
</style>
</head>
<body>
<div class="wrap">
<div class="box1">box1</div>
<div class="box2">box2</div>
<div class="box3">box3</div>
</div>
</body>
</html>