固定div的CSS水平居中?

mat*_*att 170 html css centering

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

我知道这个问题已经有一百万次,但我无法找到解决方案.我有一个div,应该固定在屏幕上,即使页面滚动它应该始终保持在屏幕中间的中心!

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

那可能吗?

Mac*_*zyk 477

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

水平中心:

left: 50%;
transform: translateX(-50%);
Run Code Online (Sandbox Code Playgroud)

垂直中心:

top: 50%;
transform: translateY(-50%);
Run Code Online (Sandbox Code Playgroud)

水平和垂直:

left: 50%;
top: 50%;
transform: translate(-50%, -50%);
Run Code Online (Sandbox Code Playgroud)

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

  • +1这个答案表明了stackoverflow的一个问题:那些对他们的日子有好处并且被正确接受的旧答案可以自豪地站在最顶层,给人留下他们仍然合适的印象,而对于像这样的新世界的伟大答案,必须反对赔率的斗争. (73认同)
  • @NickRice 100%同意.这个答案应该是新接受的答案.初级开发者甚至不应该*看到*当前接受的答案! (8认同)
  • 是的,Chrome错误地模糊了转化的内容.文字也模糊不清.但这是使固定元素居中而没有硬编码和使用包装元素的唯一解决方案.如果您不关心背景的指针事件,您可以使用全屏包装器和flexbox或@ salomvary的解决方案实现相同的效果. (4认同)
  • 这会在内容元素的框阴影中产生模糊效果. (3认同)
  • @matt请接受这个.滚动远远看到这个. (2认同)

Que*_*tin 165

left: 50%;
margin-left: -400px; /* Half of the width */
Run Code Online (Sandbox Code Playgroud)

  • 调整浏览器窗口大小时,它无法正常工作. (24认同)
  • @Bahodir:你确定吗?调整大小对我来说是正确的.我认为这个'-400`是由于div的宽度设置为'800`. (3认同)
  • 我之所以投票,不是因为这是一个糟糕的答案 - 这是一个很好的答案,但因为它不再是,而下一个最高的答案需要所有的帮助,它可以被视为最好的答案现在.昆汀:我认为在答案本身中对这个效果的评论进行编辑是个好主意 - 然后我会推翻我的投票. (3认同)
  • 完全同意-这不是解决方案!*从不*以这种方式硬编码某些东西。-1 (2认同)

sal*_*ary 57

如果使用内联块是一个选项,我会建议这种方法:

.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;
} 
Run Code Online (Sandbox Code Playgroud)

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


Nic*_*ice 28

编辑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) */
}
Run Code Online (Sandbox Code Playgroud)


小智 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;
    }
Run Code Online (Sandbox Code Playgroud)


Den*_*s V 5

有可能以这种方式对div进行水平居中:

HTML:

<div class="container">
    <div class="inner">content</div>
</div>
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

使用这种方式,您将始终将内部块置于中心位置,此外,它可以很容易地转换为真实的响应(在示例中,它将在较小的屏幕上流动),因此不会像问题示例和所选答案中那样受到限制.


小智 5

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

<div id="outer">
  <div id="inner">
    content
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

下面css背后的原理是定位"外部"的某一侧,然后使用它假设"内部"的大小来相对移动后者的事实.

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

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