html - Div 全屏响应 : vertical centering

标签 html css fullscreen center centering

我有一个包含图像的 div。

我的图像比例不适合常规屏幕,因此我希望它是全宽的,因为我知道会有垂直溢出。

问题是我无法将其垂直居中。

我不能使用负边距,因为我希望这个系统能够响应。

我考虑过使用 jquery 脚本计算屏幕分辨率,然后设置负边距顶部,但如果有人知道如何在 css 中执行此操作,那就太好了。

谢谢你的帮助

最佳答案

您可以通过在 y 轴上变换图像来完成此操作:

img{
    top:50%; // move the top of the image to the vertical center of the parent
    transform:translateY(-50%); // offset the top of the image by half its height, thereby 'true' vertical centering it within the parent
}

注意。如果您在执行此操作时遇到问题,您可能需要为图像和父级设置一个位置

关于html - Div 全屏响应 : vertical centering,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31135455/

相关文章:

配置文件框的 html 模板

html - 输入按钮在 bootstrap div 中占据 100%

javascript - 二级全屏叠加导航

javascript - 在 Coinflip 游戏中切换图像

html - 使用显示 :inline 未将 Div 对齐成一行

html - 当浏览器变小时,如何使包含文本的 div 扩展其高度?

xna - 如何在 xna 中设置窗口/屏幕尺寸?

asp.net-mvc - 使用 MVC 3.0 与继续使用 .NET 4+ 的长期/短期风险因素?

php javascript 警报框必须单击 2 次才能显示

c++ - 当 DPI 感知、全屏和无框时,在 WM_NCACTIVATE 上删除客户区