居中位置:固定元素

发布于 2024-08-17 00:43:49 字数 715 浏览 10 评论 0原文

我想制作一个以屏幕为中心的 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 使该框在屏幕中居中?

I would like to make a position: fixed; popup box centered to the screen with a dynamic width and height. I used margin: 5% auto; for this. Without position: fixed; it centers fine horizontally, but not vertically. After adding position: fixed;, it's even not centering horizontally.

Here's the complete set:

.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>

How do I center this box in screen with CSS?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(24

我一向站在原地 2024-08-24 00:43:49

如果您的 div 具有已知的宽度和高度,那么您基本上需要将 topleft 设置为 50% 以使左上角居中该分区的。您还需要将 margin-topmargin-left 设置为 div 高度和宽度的负一半,以将中心移向 div 的中间。

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

或者,如果您的 div 具有动态/未定义的宽度和/或高度,则将 transform 设置为 div 相对宽度和高度的负半部分,而不是 margin

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

或者,如果您的 div 至少具有固定宽度,并且您并不真正关心垂直居中,那么您也可以添加 left: 0right: 0margin-leftmargin-rightauto 的元素,以便具有固定宽度的固定定位元素知道其左边和右边的位置右偏移开始。在你的情况下:

position: fixed;
width: 500px;
margin: 5% auto; /* Only centers horizontally not vertically! */
left: 0;
right: 0;

If your div has a known width and height, then you basically need to set top and left to 50% to center the left-top corner of the div. You also need to set the margin-top and margin-left to the negative half of the div's height and width to shift the center towards the middle of the div.

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

Or, if your div has a dynamic/undefined width and/or height, then instead of the margin, set the transform to the negative half of the div's relative width and height.

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

Or, if your div has at least a fixed width and you don't really care about centering vertically, then you can instead also add left: 0 and right: 0 to the element having a margin-left and margin-right of auto, so that the fixed positioned element having a fixed width knows where its left and right offsets start. In your case thus:

position: fixed;
width: 500px;
margin: 5% auto; /* Only centers horizontally not vertically! */
left: 0;
right: 0;
宫墨修音 2024-08-24 00:43:49

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

这是一种将具有动态宽度的元素水平居中的现代方法 - 它适用于所有现代浏览器; 可以在此处查看支持

更新示例

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

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

更新的示例

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

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

I want to make a popup box centered to the screen with dynamic width and height.

Here is a modern approach for horizontally centering an element with a dynamic width - it works in all modern browsers; support can be seen here.

Updated Example

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

For both vertical and horizontal centering you could use the following:

Updated Example

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

You may wish to add in more vendor prefixed properties too (see the examples).

潜移默化 2024-08-24 00:43:49

或者只需将 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 中正确运行(您当然应该拥有它......!)

Or just add left: 0 and right: 0 to your original CSS, which makes it behave similarly to a regular non-fixed element and the usual auto-margin technique works:

.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;
}

Note you need to use a valid (X)HTML DOCTYPE for it to behave correctly in IE (which you should of course have anyway..!)

相思故 2024-08-24 00:43:49

添加一个容器,例如:

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

然后将您的盒子放入此 div 中即可完成工作。

编辑:正如评论中提到的,内部内容需要设置为 display: inline-block 假设有两个 div,例如:

    <div class="outer">
        <div class="inner">
             content goes here
        </div>
    </div>

那么内部的 CSS 需要是:

    .outer {
        position: fixed;
        text-align: center;
        left: 0;
        right: 0;
    }
    .inner {
        display: inline-block;
    }

与外部一起div 具有 left: 0; right:0;text-align: center 这会将内部 div 居中对齐,而不显式指定内部 div 的宽度。

Add a container like:

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

Then put your box into this div will do the work.

Edit: as mentioned in the comments, the inner content needs to be set to display: inline-block assuming there're two divs like:

    <div class="outer">
        <div class="inner">
             content goes here
        </div>
    </div>

Then the CSS for the inner needs to be:

    .outer {
        position: fixed;
        text-align: center;
        left: 0;
        right: 0;
    }
    .inner {
        display: inline-block;
    }

Together with the outer div having a left: 0; right:0; and text-align: center this will align the inner div centered, without explicitly specifying the width of the inner div.

樱花落人离去 2024-08-24 00:43:49

中心固定位置元件
(我知道的简单且最好的方法)

position:fixed;
top: 0; left: 0;
transform: translate(calc(50vw - 50%));

用于水平居中和垂直(如果高度与宽度相同)

position:fixed;
top: 0; left: 0;
transform: translate(calc(50vw - 50%), calc(50vh - 50%));

当在居中元素内部的弹性盒中使用边距时,这两种方法都不会限制居中元素的宽度小于视口宽度

Center fixed position element
(the simple & best way I know)

position:fixed;
top: 0; left: 0;
transform: translate(calc(50vw - 50%));

For centering it horizontally & vertically (if height is same as width)

position:fixed;
top: 0; left: 0;
transform: translate(calc(50vw - 50%), calc(50vh - 50%));

Both of these approaches will not limit centered element's width less than viewport width, when using margins in flexbox, inside centered element

绳情 2024-08-24 00:43:49

只需添加:

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

Just add:

left: calc(-50vw + 50%);
right: calc(-50vw + 50%);
margin-left: auto;
margin-right: auto;
世界等同你 2024-08-24 00:43:49
#modal {
    display: flex;
    justify-content: space-around;
    align-items: center;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
}

它里面可以是任何具有不同宽度、高度或不具有不同宽度、高度的元素。
全部都居中。

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

inside it can be any element with diffenet width, height or without.
all are centered.

半边脸i 2024-08-24 00:43:49

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

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

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

(100%高度,在使用display:table-cell时不起作用;(需要将某些东西垂直居中))...

不管怎样,它有效:)

This solution does not require of you to define a width and height to your popup div.

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

And instead of calculating the size of the popup, and minus half to the top, javascript is resizeing the popupContainer to fill out the whole screen...

(100% height, does not work when useing display:table-cell; (wich is required to center something vertically))...

Anyway it works :)

画骨成沙 2024-08-24 00:43:49
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'});
}
left: 0;
right: 0;

Was not working under IE7.

Changed to

left:auto;
right:auto;

Started working but in the rest browsers it stop working!
So used this way for IE7 below

if ($.browser.msie && parseInt($.browser.version, 10) <= 7) {                                
  strAlertWrapper.css({position:'fixed', bottom:'0', height:'auto', left:'auto', right:'auto'});
}
为人所爱 2024-08-24 00:43:49

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

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

I used vw (viewport width) and vh (viewport height). viewport is your entire screen. 100vw is your screens total width and 100vh is total height.

.class_name{
    width: 50vw;
    height: 50vh;
    border: 1px solid red;
    position: fixed;
    left: 25vw;top: 25vh;   
}
红尘作伴 2024-08-24 00:43:49

您基本上可以将其包装到另一个 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>

You can basically wrap it into another div and set its position to 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>

迷爱 2024-08-24 00:43:49

我只是使用这样的东西:

.c-dialogbox {
    --width:  56rem;
    --height: 32rem;

    position: fixed;

    width:  var(--width);
    height: var(--height);
    left:   calc( ( 100% - var(--width) ) / 2 );
    right:  calc( ( 100% - var(--width) ) / 2 );
    top:    calc( ( 100% - var(--height) ) / 2 );
    bottom: calc( ( 100% - var(--height) ) / 2 );
}

它使对话框水平和垂直居中,我可以使用不同的宽度和高度来适应不同的屏幕分辨率,使其响应媒体查询。

如果您仍然需要为不支持 CSS 自定义属性或 calc() 的浏览器提供支持(请检查 caniuse),则不是一个选项。

I just use something like this:

.c-dialogbox {
    --width:  56rem;
    --height: 32rem;

    position: fixed;

    width:  var(--width);
    height: var(--height);
    left:   calc( ( 100% - var(--width) ) / 2 );
    right:  calc( ( 100% - var(--width) ) / 2 );
    top:    calc( ( 100% - var(--height) ) / 2 );
    bottom: calc( ( 100% - var(--height) ) / 2 );
}

It centers the dialog box both horizontally and vertically for me, and I can use different width and height to fit different screen resolutions to make it responsive, with media queries.

Not an option if you still need to provide support for browsers where CSS custom properties or calc() are not supported (check on caniuse.)

浅语花开 2024-08-24 00:43:49

这对我来说效果最好:

    display: flex;
    justify-content: center;
    align-items: center;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;

This one worked the best for me:

    display: flex;
    justify-content: center;
    align-items: center;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
谁把谁当真 2024-08-24 00:43:49

你可以设置这样的东西

.modal {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;

}

You could set something like this

.modal {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;

}
烟酒忠诚 2024-08-24 00:43:49

要固定位置,请使用以下命令:

div {
    position: fixed;
    left: 68%;
    transform: translateX(-8%);
}

To fix the position use this :

div {
    position: fixed;
    left: 68%;
    transform: translateX(-8%);
}
別甾虛僞 2024-08-24 00:43:49

简单,试试这个

position: fixed;
width: 500px;
height: 300px;
top: calc(50% - 150px);
left: calc(50% - 250px);
background-color: red;

simple, try this

position: fixed;
width: 500px;
height: 300px;
top: calc(50% - 150px);
left: calc(50% - 250px);
background-color: red;
腻橙味 2024-08-24 00:43:49

一种可能的答案

<!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>

One possible 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>
聆听风音 2024-08-24 00:43:49

尝试将其用于无法正确居中的水平元素。

width: calc (width: 100% - 宽度,无论其他偏离中心的位置)

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

width: calc(100% - 200px);

Try using this for horizontal elements that won't center correctly.

width: calc (width: 100% - width whatever else is off centering it)

For example if your side navigation bar is 200px:

width: calc(100% - 200px);
╰つ倒转 2024-08-24 00:43:49

当您不知道要居中的对象的大小并且希望它在所有屏幕尺寸中居中时,这会非常有效:

.modal {
  position: fixed;
  width: 90%;
  height: 90%;
  top: 5%;           /* (100 - height) / 2 */
  left: 5%;          /* (100 - width) / 2 */
}

This works wonderfully when you don't know the size of the thing you are centering, and you want it centered in all screen sizes:

.modal {
  position: fixed;
  width: 90%;
  height: 90%;
  top: 5%;           /* (100 - height) / 2 */
  left: 5%;          /* (100 - width) / 2 */
}
甜警司 2024-08-24 00:43:49

我用的很简单。例如,我有一个位置:固定的导航栏,因此我调整它以在边缘留出一个小空间,如下所示。

nav {
right: 1%;
width: 98%;
position: fixed;
margin: auto;
padding: 0;
}

这个想法是取宽度的剩余百分比“在本例中为 2%”并使用它的一半。

What I use is simple. For example I have a nav bar that is position : fixed so I adjust it to leave a small space to the edges like this.

nav {
right: 1%;
width: 98%;
position: fixed;
margin: auto;
padding: 0;
}

The idea is to take the remainder percentage of the width "in this case 2%" and use the half of it.

甜尕妞 2024-08-24 00:43:49

遇到这个问题,所以我得出结论,使用(不可见的)容器是最好的选择(基于答案@Romulus Urakagi Ts'ai)。使用flexbox实现:(

.zoom-alert {
  position: fixed;
  justify-content: center;
  display: flex;
  bottom: 24px;
  right: 0;
  left: 0;
  z-index: 100000;
  width: 100%;

  &__alert {
    flex: 0 0 500px;
    padding: 24px;
    background-color: rgba(212, 193, 105, 0.9);
    border: 1px solid rgb(80, 87, 23);
    border-radius: 10px;
  }
}

语法是SCSS,但可以轻松修改为纯CSS)

固定居中警报

Had this problem so I concluded that using a (invisible) container is the best option (based on answer @Romulus Urakagi Ts'ai). To make it with flexbox:

.zoom-alert {
  position: fixed;
  justify-content: center;
  display: flex;
  bottom: 24px;
  right: 0;
  left: 0;
  z-index: 100000;
  width: 100%;

  &__alert {
    flex: 0 0 500px;
    padding: 24px;
    background-color: rgba(212, 193, 105, 0.9);
    border: 1px solid rgb(80, 87, 23);
    border-radius: 10px;
  }
}

(the syntax is SCSS but can be easily modified to pure CSS)

fixed centered alert

葬心 2024-08-24 00:43:49

具有

position:fixed

属性的 div 的中心元素Html 和 Css 代码

.jqbox_innerhtml {
   position: fixed;
   width:100%;
   height:100%;
    display: flex;
    justify-content: space-around;
    align-items: center;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    border: 5px solid #ccc;
    background-color: #fff;
}
<div class="jqbox_innerhtml">
    This should be inside a horizontally
    and vertically centered box.
</div>

Center element of a div with the property of

position:fixed

Html and Css code

.jqbox_innerhtml {
   position: fixed;
   width:100%;
   height:100%;
    display: flex;
    justify-content: space-around;
    align-items: center;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    border: 5px solid #ccc;
    background-color: #fff;
}
<div class="jqbox_innerhtml">
    This should be inside a horizontally
    and vertically centered box.
</div>

难理解 2024-08-24 00:43:49

另一个简单的解决方案是将元素的 width 设置为 fit-content 并将 leftright 设置为0px

width: fit-content;
position: fixed;
left: 0px;
right: 0px;

如果您不知道元素的宽度,这非常有用。

Another simple solution is to set the width of the element to fit-content and set the left and right to 0px;

width: fit-content;
position: fixed;
left: 0px;
right: 0px;

This is useful if you don't know the width of the element.

笑看君怀她人 2024-08-24 00:43:49

唯一万无一失的解决方案是使用表align=center,如下所示:

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

我不敢相信世界各地的人们浪费大量愚蠢的时间来解决像居中div这样的基本问题。 css 解决方案并不适用于所有浏览器,jquery 解决方案是一种软件计算解决方案,由于其他原因而不是一个选项。

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

The only foolproof solution is to use table align=center as in:

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

I cannot believe people all over the world wasting these copious amount to silly time to solve such a fundamental problem as centering a div. css solution does not work for all browsers, jquery solution is a software computational solution and is not an option for other reasons.

I have wasted too much time repeatedly to avoid using table, but experience tell me to stop fighting it. Use table for centering div. Works all the time in all browsers! Never worry any more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文