text-align: center:对内容进行居中处理
margin: 0 auto:对盒子进行居中处理
区别:
内容居中只对内容有效,例如文字、图片,对盒子模型无效;
盒子居中只对盒子模型有效,对盒子中的内容无效。
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>标题</title>
<style>
.box1 {
height: 300px;
width: 300px;
background-color: blue;
margin: 0 auto;
text-align: center;
}
.box2 {
width: 100px;
height: 100px;
background-color: red;
}
img {
height: 100px;
width: 200px;
}
</style>
</head>
<body>
<div class="box1">
<p>我是内容</p>
<img src="111.jpeg">
<div class="box2"></div>
</div>
</body>
</html>