我需要的div在任何时候都在页面的中心,无论用户是否调整网页大小。
我试过:边距:自动;边距:0自动;边距-左:自动;边距-右转;
但这三个都不起作用。
HTML:
<div id="grayinnerbackground">
</div>
CSS:
div#grayinnerbackground {
margin: auto;
width:1000px;
background-color: #D9D9D9;
height: 100%;
position: fixed;
z-index: -1;
}
这是我所说的一个例子的小提琴。http://jsfiddle.net/ymvDJ/
谢谢。
发布于 2013-08-17 17:00:39
您可以使用
position: fixed;
left: 50%;
width: 50px;
margin-left: -25px; /* width ÷ 2 */
发布于 2013-08-17 17:00:40
如果do想要固定头寸,那么添加以下规则并删除通常的保证金技巧:
left: 50%;
margin-left: -25px; // half the width of your element
发布于 2013-08-17 16:56:58
删除position: fixed
,将width
更改为50px
,并确保在margin: auto
中的auto
之前有一个0
。
更新:
要使div与窗口一样高,请确保将body
和html
设置为height: 100%;
:
body, html {
height: 100%:
}
https://stackoverflow.com/questions/18294714
复制