2

我无法将 div 放在页面的顶部中心。我为此使用 CSS,但这里没有任何效果。

#loader {
  position: relative;
  margin: 0 auto;
  text-align: left;
  clear: left;
  height: auto;
  z-index: 0;
}
<body>
  <div id="loader" style='display: none'><img src='img/basic/loader4.gif' /></div>
</body>

我已经为此花费了 2 个小时,但仍然无法弄清楚。

4

8 回答 8

4

首先display:none;使用 id=loader 从您的 div 中删除并添加text-align:center;到它

#loader{  
    position:relative;
    margin:0 auto;
    clear:left;
    height:auto;
    z-index: 0;
    text-align:center;/* Add This*/
}​

工作演示:http: //jsfiddle.net/surendraVsingh/FCEhD/

于 2012-06-27T10:32:07.460 回答
2

您需要为 div 指定一个特定的宽度,否则它将使用所有可用的宽度,并且边距无效。例子:

#loader{  
  margin: 0 auto;
  width: 200px;
}
于 2012-06-27T10:25:51.253 回答
2

width如果你想使用,你必须指定属性margin: 0 auto;

如果您不知道宽度,只需添加display: table属性即可。

#out {
   display: table;
   margin: 0 auto;
}
于 2012-06-27T10:26:36.440 回答
1

在上面的例子中,每个人都假设他想要“全宽”。

这适用于绝对全宽/高度父容器,但会自动将宽度限制为仅内容宽度

父容器

display: block; 
position: absolute;
clear: both;
width: 100%;
height: 100%;
border: solid thin red;

子容器

display: block;
position: absolute;
top:0;
transform: translateX(50%);
padding-left:.5em;
padding-right:.5em;
margin-left:auto;
margin-right:auto;
right:50%;
于 2018-01-29T08:43:37.757 回答
0
#loader{  
   margin: 0px auto;
   width: 100px;
}
于 2012-06-27T10:25:58.640 回答
0

<div id="loader" style='display: none' align="center">

按 ctrl+space 不确定语法,在 css 中边距和填充必须设置为 0

于 2012-06-27T10:27:10.107 回答
0
#loader
{
    position: absolute;
    width: xxpx;
    height: xxpx;
    margin:0px auto ;

}

或者你可以使用

margin-left:xx;  and `margin-top:0px;`
于 2012-06-27T10:31:33.100 回答
0
#loader {
    display: none;
    clear: both;
    margin: 0px auto;
    width: [a number] px;
}

当然你也应该注意浏览器缓存。清除浏览器缓存并再次测试

于 2012-07-29T10:14:58.240 回答