0

I have div tag as first element in html page. I want this element to be centered on a page. Hence, I set left and right margin properties of this div tag. e.g. margin-left:20%

However, margins take effect differently in different browsers and UI alignment differs browser to browser

How can I resolve this?

How to set margins so that they work same in all browsers?

Thanks in advance.

4

3 回答 3

0

DIV 定心

居中相对定位的 div

#div {
  position: relative;  /* Or without position property | default is static */
  width: 960px;        /* Change to your needs */
  margin: 0 auto;
}

您还可以使用例如margin: 0 auto 20px;where 0is top、auto左/右和20px底部。


居中绝对定位的 div

#div {
  position: absolute;
  width: 960px;        /* Change to your needs */
  top: 0;
  left: 50%;
  margin-left: -480px; /* Margin left is - width / 2 */
}
于 2013-09-23T19:17:01.853 回答
0

尝试将以下 css 属性添加到您的 css 类:

margin:0 auto;
于 2013-09-23T19:10:07.633 回答
0

通常这会更好,只要你在 div 上设置了宽度

yourDiv {
    margin: 0 auto;
}

在不了解有关您的 div 的更多信息的情况下,这是您最好的选择。第一个 0 将顶部和底部边距设置为 0,而 auto 用于左右边距。

于 2013-09-23T19:10:13.960 回答