中心对准的固定位置股利 [英] center aligning a fixed position div

查看:150
本文介绍了中心对准的固定位置股利的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让已位置一个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.

这篇关于中心对准的固定位置股利的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆