中心位置:固定元素

问题描述 投票:357回答:14

我想制作一个position: fixed;弹出框,以屏幕为中心,具有动态宽度和高度。我为此使用了margin: 5% auto;。如果没有position: fixed;,它可以水平居中,但不能垂直居中。添加position: fixed;后,它甚至不会水平居中。

这是完整的集合:

.jqbox_innerhtml {
    position: fixed;
    width: 500px;
    height: 200px;
    margin: 5% auto;
    padding: 10px;
    border: 5px solid #ccc;
    background-color: #fff;
}
<div class="jqbox_innerhtml">
    This should be inside a horizontally
    and vertically centered box.
</div>

如何使用CSS将此框置于屏幕中心?

css css-position centering
14个回答
530
投票

你基本上需要将topleft设置为50%以居中div的左上角。您还需要将margin-topmargin-left设置为div的高度和宽度的负半部分,以将中心移向div的中间。

因此,提供<!DOCTYPE html>(标准模式),这应该做:

position: fixed;
width: 500px;
height: 200px;
top: 50%;
left: 50%;
margin-top: -100px; /* Negative half of height. */
margin-left: -250px; /* Negative half of width. */

或者,如果您不关心垂直居中和旧浏览器(如IE6 / 7),那么您也可以将left: 0right: 0添加到具有margin-leftmargin-rightauto的元素中,以便固定定位元素具有固定宽度知道它的左右偏移从哪里开始。在你的情况下:

position: fixed;
width: 500px;
height: 200px;
margin: 5% auto; /* Will not center vertically and won't work in IE6/7. */
left: 0;
right: 0;

同样,如果您关心IE,这仅适用于IE8 +,并且这仅在水平而非垂直居中。


1
投票

我使用vw(视口宽度)和vh(视口高度)。视口是你的整个屏幕。 100vw是你的屏幕总宽度,100vh是总高度。

.class_name{
    width: 50vw;
    height: 50vh;
    border: 1px solid red;
    position: fixed;
    left: 25vw;top: 25vh;   
}

0
投票

一个可能的answer

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="UTF-8">
    <title>CSS Center Background Demo</title>
    <style type="text/css">
        body {
            margin: 0;
            padding: 0;
        }

        div.centred_background_stage_1 {
            position: fixed;
            z-index:(-1 );
            top: 45%;
            left: 50%;
        }

        div.centred_background_stage_2 {
            position: relative;
            left: -50%;

            top: -208px;
            /* % does not work.
               According to the
               http://reeddesign.co.uk/test/points-pixels.html
               6pt is about 8px

               In the case of this demo the background
               text consists of three lines with
               font size 80pt.

               3 lines (with space between the lines)
               times 80pt is about
               ~3*(1.3)*80pt*(8px/6pt)~ 416px

               50% from the 416px = 208px
             */

            text-align: left;
            vertical-align: top;
        }

        #bells_and_wistles_for_the_demo {
            font-family: monospace;
            font-size: 80pt;
            font-weight: bold;
            color: #E0E0E0;
        }

        div.centred_background_foreground {
            z-index: 1;
            position: relative;
        }
    </style>
</head>
<body>
<div class="centred_background_stage_1">
    <div class="centred_background_stage_2">
        <div id="bells_and_wistles_for_the_demo">
            World<br/>
            Wide<br/>
            Web
        </div>
    </div>
</div>
<div class="centred_background_foreground">
    This is a demo for <br/>
    <a href="http://stackoverflow.com/questions/2005954/center-element-with-positionfixed">
        http://stackoverflow.com/questions/2005954/center-element-with-positionfixed
    </a>
    <br/><br/>
    <a href="http://www.starwreck.com/" style="border: 0px;">
        <img src="./star_wreck_in_the_perkinnintg.jpg"
             style="opacity:0.1;"/>
    </a>
    <br/>
</div>
</body>
</html>

0
投票

尝试将此用于不能正确居中的水平元素。

width:calc(宽度:100% - 其他任何偏离中心的宽度)

例如,如果您的侧面导航栏是200px:

width: calc(100% - 200px);

0
投票
#modal {
    display: flex;
    justify-content: space-around;
    align-items: center;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
}

里面可以是任何宽度,高度不同的元素。都集中在一起。


-11
投票

唯一简单的解决方案是使用table align = center,如:

<table align=center><tr><td>
<div>
...
</div>
</td></tr></table>

我无法相信全世界的人们都在浪费这些丰富的时间去解决像居中这样的基本问题。 css解决方案不适用于所有浏览器,jquery解决方案是一种软件计算解决方案,并且由于其他原因而不是一种选择。

我反复浪费了太多时间以避免使用桌子,但经验告诉我要停止对抗它。使用表格来居中div。在所有浏览器中始终有效!再也不用担心了。


258
投票

我想制作一个以动态宽度和高度为中心的弹出框。

这是一种现代方法,用于水平居中具有动态宽度的元素 - 它适用于所有现代浏览器; support can be seen here

Updated Example

.jqbox_innerhtml {
    position: fixed;
    left: 50%;
    transform: translateX(-50%);
}

对于垂直和水平居中,您可以使用以下内容:

Updated Example

.jqbox_innerhtml {
    position: fixed;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
}

您可能还希望添加更多供应商前缀属性(请参阅示例)。


127
投票

或者只是将left: 0right: 0添加到原始CSS中,这使得它的行为类似于常规的非固定元素,并且通常的自动边距技术可以工作:

.jqbox_innerhtml
{
  position: fixed;
  width:500px;
  height:200px;
  background-color:#FFF;
  padding:10px;
  border:5px solid #CCC;
  z-index:200;
  margin: 5% auto;
  left: 0;
  right: 0;
}

请注意,您需要使用有效的(X)HTML DOCTYPE才能在IE中正确运行(无论如何你当然应该......)


21
投票

添加一个容器,如:

div {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  text-align: center;
}

然后把你的盒子放进这个div就可以了。


14
投票

只需添加:

left: calc(-50vw + 50%);
right: calc(-50vw + 50%);
margin-left: auto;
margin-right: auto;

5
投票

此解决方案不需要您为弹出div定义宽度和高度。

http://jsfiddle.net/4Ly4B/33/

而不是计算弹出窗口的大小,减去一半到顶部,javascript正在调整popupContainer的大小以填充整个屏幕...

(100%高度,使用显示时不起作用:table-cell;(需要垂直居中))...

无论如何它工作:)


4
投票
left: 0;
right: 0;

不是在IE7下工作。

变成

left:auto;
right:auto;

开始工作但在其他浏览器中它停止工作!所以这样用于IE7下面

if ($.browser.msie && parseInt($.browser.version, 10) <= 7) {                                
  strAlertWrapper.css({position:'fixed', bottom:'0', height:'auto', left:'auto', right:'auto'});
}

2
投票

你基本上可以将它包装到另一个div并将其position设置为fixed

.bg {
  position: fixed;
  width: 100%;
}

.jqbox_innerhtml {
  width: 500px;
  height: 200px;
  margin: 5% auto;
  padding: 10px;
  border: 5px solid #ccc;
  background-color: #fff;
}
<div class="bg">
  <div class="jqbox_innerhtml">
    This should be inside a horizontally and vertically centered box.
  </div>
</div>

1
投票

要修复这个位置,请使用: -

div {
    position: fixed;
    left: 68%;
    transform: translateX(-8%);
}
© www.soinside.com 2019 - 2024. All rights reserved.