margin-left:auto;margin-right:auto; 不起作用的原因

1.  是否添加

过渡的(Transitional):要求非常宽松的DTD,它允许你继续使用HTML4.01的标识(但是要符合xhtml的写法),完整代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

2. 

.divCenter {
/* float: left; */
225px;
margin-left: auto;
margin-right: auto;
}

请设置元素宽度, 不要增加float属性。

例子如下直接使用就是居中

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<style>
.divCenter {
/* float: left; */

display:block;

325px;
height:200px;
margin-left: auto;
margin-right: auto;
background:#eee;
}
</style>

<div style='float:left;1000px;height:300px;background:#666;'>
<div class='divCenter'></div>
</div>

原文地址:https://www.cnblogs.com/yuzhould/p/12522705.html