【问题标题】:CSS align fullscreen image with max-height to middleCSS将全屏图像与最大高度对齐到中间
【发布时间】:2015-10-23 11:41:30
【问题描述】:

我正在尝试创建一个类似画廊的简单图像查看器,并且我想将图像与页面的中心和中间对齐。

这可以简单地使用tabletable-row 完成,但仅限于图像小于页面100% 的情况。在第二种情况下,我可以简单地通过text-align: centremax-height: 100% 对齐它。

这是我的代码:https://jsfiddle.net/rtv393z7/

<div style="position: fixed; width: 100%; height: 100%; top: 0; left: 0; background-color: rgba(0,0,0,0.6);">
  <div style="width: 100%; height: 100%; text-align: center;">
    <img style="max-height: 100%; max-width: 100%; box-sizing: border-box; padding: 30px;" src="http://i.imgur.com/uRGZ0EE.jpg">
  </div>
</div>

但是如何让它在这两种情况下都能正常工作呢?

【问题讨论】:

    标签: css image alignment vertical-alignment


    【解决方案1】:

    你可以像这样使用弹性盒子:

    .flex-content{
      display: flex;
      align-items: center;
      justify-content: center;
    }
    <div style="position: fixed; width: 100%; height: 100%; top: 0; left: 0; background-color: rgba(0,0,0,0.6);">
      <div class="flex-content" style="width: 100%; height: 100%; text-align: center;">
        <img style="max-height: 100%; max-width: 100%; box-sizing: border-box; padding: 30px;" src="http://i.imgur.com/uRGZ0EE.jpg">
      </div>
    </div>

    Here a working JSFiddle example

    或者你可以像这样使用position: absolutetransform

    .center-content {
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        -webkit-transform: translateY(-50%);
    }
    <div style="position: fixed; width: 100%; height: 100%; top: 0; left: 0; background-color: rgba(0,0,0,0.6);">
        <div  class="center-content"  style="width: 100%; text-align: center;">
            <img style="max-height: 100%; max-width: 100%; box-sizing: border-box; padding: 30px;" src="http://i.imgur.com/uRGZ0EE.jpg">
        </div>
    </div>

    还请注意,我从 .center-content 中删除了 height: 100% 内联样式。

    Here the JSFiddle for this alternative

    上面的例子在 FF 中不起作用,我在 Chrome 中看到过。

    所以我这样修复它:

    .center-content {
        position: relative;    
    }
    
    img{    
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        -webkit-transform: translate(-50%, -50%);
    }
    <div style="position: fixed; width: 100%; height: 100%; top: 0; left: 0; background-color: rgba(0,0,0,0.6);">
        <div  class="center-content"  style="width: 100%; height: 100%;">
            <img style="max-height: 100%; max-width: 100%; box-sizing: border-box; padding: 30px;" src="http://i.imgur.com/uRGZ0EE.jpg">
        </div>
    </div>

    And here the JSFiddle

    【讨论】:

    • 嗯,它看起来工作得很好,但有时图像会被拉伸(主要是在以纵向分辨率查看时)。这只是浏览器错误,还是如何避免?它发生在 Firefox 和 Chrome 浏览器中,但我无法找出确切的条件......
    • @Aslanex 您在 JSFiddle 中看到了这种行为吗?
    • 是的,在 JSFiddle 和我自己的网站上都进行了测试。替代方法也不起作用 - 当页面高度小于图像时,图像不会缩小,但会保持裁剪。
    • @Aslanex 对不起,我不明白你想要的行为,你能添加更多你想要实现的细节吗,无论如何:“当页面高度小于图像时,图像不会缩小,但仍会被裁剪。”在替代解决方案中可能是因为你在 'img' 中有 'padding: 30px' 删除它,如果你想要的话,让我不要。
    • 图像应在页面上保持完全可见,同时按比例缩小。但是,当图像小于页面分辨率时,它应该保持其原始大小并定位到页面的垂直和水平中心。
    猜你喜欢
    • 2016-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-14
    • 2011-08-06
    • 1970-01-01
    • 2014-03-30
    • 2014-02-04
    相关资源
    最近更新 更多