在所有分辨率下将图像垂直居中
我需要能够将所有常见分辨率的图像垂直居中。 SO 上的很多人之前已经问过这个问题,90% 的人都给出了这个教程 http://www.jakpsatweb.cz/css/css-vertical- center-solution.html
作为答案。 然而,当我在 FF 下的 1280 x 1024 分辨率显示器上查看时,它并不居中。更糟糕的是,它在 IE7 中严重崩溃。所以,这绝对不是答案。
我在追逐不可能的梦想吗? 该解决方案必须适用于 FF 和 IE 6/7
该解决方案可以是任何可行的解决方案,尽管有点纯粹主义者,我更喜欢 div 而不是表格:)
特别是我需要它用于这个网站 http://www.codecookery.com/test/index.html
如您所见,这是一个幻灯片,需要居中。
I need to be able to center the image vertically for all the common resolutions.
A lot of ppl here on SO have already asked this question before, and 90% of then give this tutorial
http://www.jakpsatweb.cz/css/css-vertical-center-solution.html
as an answer.
However, when viewed at my 1280 by 1024 res monitor in FF, it's not centered. And worse yet, it breaks horribly in IE7. So, it's definitely NOT the answer.
Am I chasing the impossible dream?
The solution has to work for FF and IE 6/7
the solution can be anything that would work, though being a bit of a purist, I would prefer a div over table:)
specifically i need it for this site
http://www.codecookery.com/test/index.html
as you see, it's a slideshow, that needs to be centered.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信这个解决方案就是您正在寻找的。我无法访问 IE 来测试它(这就是我使用 Linux 所得到的),但我相当有信心它可以工作。至于表格上的 div,完全没有包装器怎么样?不仅如此,如果图像大于显示器的分辨率,它会很好地缩小。如果您希望边缘有一点填充(对于极大的图像和/或小分辨率),您可以调整最大高度和最大宽度。
CSS:
和 HTML:
注意:
I believe this solution is what you're looking for. I don't have access to IE to test it (that's what I get for using Linux), but I'm fairly confident it works. And in regards to a div over a table, how about no wrapper at all? Not only that, but if the image is larger than the resolution of the monitor, it will scale down quite nicely. You can adjust the max-height and max-width if you want a little padding along the edges (for extremely large images and/or small resolutions).
The CSS:
And the HTML:
Notes:
如果可以选择固定高度的 div,那么您可以使用前 50% 的绝对位置,然后使用负边距来定位 div。我在FF3.6、IE6、IE8和Chrome中测试了以下内容。
If a fixed-height div is an option then you can use position absolute with top 50% and then use a negative margin to position the div. I tested the following in FF3.6, IE6, IE8 and Chrome.
这里的代码没有 jakpsatweb.cz 的代码那么可怕。
The code here is less horrible than that of jakpsatweb.cz.