简体   繁体   English

HTML div ID无法正确对齐

[英]Html div id not aligning properly

I have a main div on my page that has the attribute align="center" , yet for some reason it is not aligning in the center. 我的页面上有一个主div,其属性为align="center" ,但由于某种原因,它不在中心对齐。

for reference here is the page . 供参考的是页面

CSS: CSS:

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

}

HTML: HTML:

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

Thanks in advance! 提前致谢!

Add margin:auto to the #page rule instead and remove the align="center" margin:auto添加到#page规则中,然后删除align="center"

#page{
   background-color: #4C1B1B;
   width:85%;
   height:500px;
   margin:auto;
}
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 + "%")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM