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; */
width: 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;

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

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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM