中心对准的固定位置股利股利、位置、中心

2023-09-11 07:35:50 作者:隐形王冠i

我试图让已位置一个div:在我的网页固定中心对齐

我一直都能够使用这种黑客与绝对定位的div做

I've always been able to do it with absolutely positioned divs using this "hack"

左:50%;宽度:400像素;保证金左:-200px

...其中保证金左值的div宽度的一半。

...where the value for margin-left is half the width of the div.

似乎这样没有工作的固定位置的div,而不是它只是把它们用自己最左边的角落50%,而忽略了保证金左声明。

This doesn't seem to work for fixed position divs, instead it just places them with their left-most corner at 50% and ignores the margin-left declaration.

如何解决这个问题,所以我可以居中对齐固定定位的元素任何想法?

Any ideas of how to fix this so I can center align fixed positioned elements?

我会扔在奖金M&功放,M,如果你能告诉我一个更好的办法来中心对齐比我上面提到的方式绝对定位元素

And I'll throw in a bonus M&M if you can tell me a better way to center align absolutely positioned elements than the way I've outlined above.

推荐答案

对于那些有同样的问题,而是一个负责任的设计,你也可以使用:

For the ones having this same problem, but with a responsive design, you can also use:

width: 75%;
position: fixed;
left: 50%;
margin-left: -37.5%;

这样做将始终保持你的固定 DIV 为中心的屏幕上,即使有响应式设计。

Doing this will always keep your fixed div centered on the screen, even with a responsive design.