Html div id没有正确对齐

时间:2012-01-20 12:21:15

标签: css html alignment

我的网页上有一个主div,其属性为align="center",但由于某种原因,它没有在中心对齐。

供参考here is the page

CSS:

#page{
    background-color: #4C1B1B;
    width:85%;
    height:500px;

}

HTML:

<body>
   <div id="page" align="center">
      &nbsp;
      <div id="pageleft">
        TEST
      </div>
      <div id="pageright">
        &nbsp;
      </div>
  </div>
</body>

提前致谢!

2 个答案:

答案 0 :(得分:2)

margin:auto添加到#page规则,然后移除align="center"

#page{
   background-color: #4C1B1B;
   width:85%;
   height:500px;
   margin:auto;
}

答案 1 :(得分:1)

To completely centre align the div, the following formula can be applied

left = (100 - (width of the div))/2

Using Jquery this can be done as below

var left = (100 - 85)/2;
$("#page").css("left",left + "%")