219
#menu {
    position: fixed;
    width: 800px;
    background: rgb(255, 255, 255); /* The Fallback */
    background: rgba(255, 255, 255, 0.8);
    margin-top: 30px;
}

我知道这个问题存在一百万次,但是我找不到解决我的情况的方法。我有一个 div,它应该固定在屏幕上,即使页面滚动它也应该始终保持在屏幕中间!

div 应该有500px宽度,应该30px远离顶部(margin-top),应该在所有浏览器尺寸的页面中间水平居中,并且在滚动页面的其余部分时不应该移动。

那可能吗?

4

8 回答 8

704

这里的答案已经过时了。现在您可以轻松地使用 CSS3 转换,而无需硬编码 margin。这适用于所有元素,包括没有宽度或动态宽度的元素。

水平中心:

left: 50%;
transform: translateX(-50%);

垂直中心:

top: 50%;
transform: translateY(-50%);

水平和垂直:

left: 50%;
top: 50%;
transform: translate(-50%, -50%);

兼容性不是问题:http ://caniuse.com/#feat=transforms2d

于 2015-09-21T11:49:30.793 回答
168
left: 50%;
margin-left: -400px; /* Half of the width */
于 2010-07-01T11:45:04.020 回答
60

如果使用 inline-blocks 是一种选择,我会推荐这种方法:

.container { 
    /* fixed position a zero-height full width container */
    position: fixed;
    top: 0; /* or whatever position is desired */
    left: 0;
    right: 0;
    height: 0;
    /* center all inline content */
    text-align: center;
}

.container > div {
    /* make the block inline */
    display: inline-block;
    /* reset container's center alignment */
    text-align: left;
} 

我在这里写了一篇简短的文章:http ://salomvary.github.com/position-fixed-horizo​​ntally-centered.html

于 2012-04-27T14:28:52.950 回答
37

编辑 2016 年 9 月:虽然仍然偶尔对此进行投票是件好事,但因为世界已经发展,我现在会选择使用变换的答案(并且有大量的投票)。我不会再这样做了。

另一种不必计算保证金或需要子容器的方法:

#menu {
    position: fixed;   /* Take it out of the flow of the document */
    left: 0;           /* Left edge at left for now */
    right: 0;          /* Right edge at right for now, so full width */ 
    top: 30px;         /* Move it down from top of window */
    width: 500px;      /* Give it the desired width */ 
    margin: auto;      /* Center it */
    max-width: 100%;   /* Make it fit window if under 500px */ 
    z-index: 10000;    /* Whatever needed to force to front (1 might do) */
}
于 2014-02-27T15:32:37.037 回答
8

可以通过这种方式将 div 水平居中:

html:

<div class="container">
    <div class="inner">content</div>
</div>

CSS:

.container {
    left: 0;
    right: 0;
    bottom: 0; /* or top: 0, or any needed value */
    position: fixed;
    z-index: 1000; /* or even higher to prevent guarantee overlapping */
}

.inner {
    max-width: 600px; /* just for example */
    margin: 0 auto;
}

使用这种方式,您将始终使您的内部块居中,此外,它可以轻松转换为真正的响应式(在示例中,它只会在较小的屏幕上流畅),因此在问题示例和所选答案中没有限制.

于 2013-11-27T01:44:51.087 回答
7

这是另一个两格解决方案。试图保持简洁而不是硬编码。首先,预期的html:

<div id="outer">
  <div id="inner">
    content
  </div>
</div>

以下css背后的原理是定位“outer”的某一侧,然后利用它假设“inner”的大小来相对移动后者。

#outer {
  position: fixed;
  left: 50%;          // % of window
}
#inner {
  position: relative;
  left: -50%;         // % of outer (which auto-matches inner width)
}

这种方法类似于 Quentin 的方法,但内部可以是可变大小的。

于 2014-07-17T17:54:30.640 回答
5

...或者您可以将菜单 div 包装在另一个中:

    <div id="wrapper">
       <div id="menu">
       </div>
    </div>


#wrapper{
         width:800px;
         background: rgba(255, 255, 255, 0.8);
         margin:30px auto;
         border:1px solid red;
    }

    #menu{
        position:fixed;
        border:1px solid green;
        width:300px;
        height:30px;
    }
于 2010-07-01T11:49:37.300 回答
-2

这是使用全屏包装 div 时的 flexbox 解决方案。justify-content 将其子 div 水平居中,而 align-items 将其垂直居中。

<div class="full-screen-wrapper">
    <div class="center"> //... content</div>
</div>

.full-screen-wrapper { 
  position: fixed;
  display: flex;
  justify-content: center;
  width: 100vw;
  height: 100vh;
  top: 0;
  align-items: center;
}

.center {
  // your styles
}
于 2019-08-21T17:42:06.560 回答