css:margin:0 auto在ie8下面不能置中的解決辦法

來源:互聯網
上載者:User
今天寫了個div,用margin:0 auto;來定義他的屬性,讓他置中,結果,竟然無效。
  一開始以為是css裡的代碼衝突了,檢查了好幾遍,沒問題,然後用Firefox和Google瀏覽器測試,置中了。。。
  看來是瀏覽器安全色性的問題,突然想起現在用了win7,ie已經升級到了ie8,看來問題就處在ie8的相容性上。百度之。。。。

  特將解決方案複製到下面:
  IE6,7,Firefox下實現置中一般用:margin: auto 來實現,但在IE8下,這是不夠的。現提供兩種解決的辦法:

  方法一(推薦):

  首先頁首必須設定文件類型:

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

且在head內添加:

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/> 即可!


  方法二:

  body設定 {text-align:center;width:100%;}
  將所有div全部包括在<div id="wrap">中,wrap如下設定:

 #wrap     {  text-align:left;  width:***;   margin:0  auto;   }

  且在head內添加:

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>

  即可!

  方法三:

  用<center></center>取代二中的wrap層。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.