CSS水平居中的固定div?
问题描述:
#menu {
position: fixed;
width: 800px;
background: rgb(255, 255, 255); /* The Fallback */
background: rgba(255, 255, 255, 0.8);
margin-top: 30px;
}
我知道这个问题是一百万次,但我不能找到一个解决我的情况。
我有一个div,应该固定在屏幕上,即使页面被滚动,它应该总是保持CENTERED在屏幕中间!
I know this question is a million times out there, however I can't find a solution to my case. I've got a div, which should be fixed on the screen, even if the page is scrolled it should always stay CENTERED in the middle of the screen!
div应该有 500px
width,应该距离顶部(margin-top) 30px
The div should have 500px
width, should be 30px
away from the top (margin-top), should be horizontally centered in the middle of the page for all browser sizes and should not move when scrolling the rest of the page.
这是否可能?
答
请参阅@MaciejKrawczyk更新解答,不需要固定宽度。
left: 50%;
margin-left: -400px; /* Half of the width */