<div style="margin:0 auto;height:300px;width:200px;background-color:#333">应该居中了吧</div>

解决方案 »

  1.   

    如果要层在页面正中,单纯用样式很难搞定,要结合JS或VBS搞定应该比较容易。
      

  2.   

    好了,我试着写了个,基本可以解决问题:
    <html><head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>层居中</title>
    <style>
    #dd{
    position:absolute;
    border:1 solid red;
    top:expression((this.parentElement.offsetHeight-this.offsetHeight)/2);
    left:expression((this.parentElement.offsetWidth-this.offsetWidth)/2);
    }
    </style>
    </head><body>
    <div id="dd">我在中间了 ^_^</div>
    </body></html>不过有个前提,就是不能加
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    这样的DTD声明
      

  3.   

    this.parentElement.offsetHeight
    换成
    parentElement.parentElement.offsetHeight
    就可以了,请问这个parentElement是什么意思啊?怎么两个?