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