【问题标题】:How to center crop a full screen background image如何居中裁剪全屏背景图像
【发布时间】:2015-10-22 02:09:38
【问题描述】:

Stack Overflow 似乎充满了类似的问题,但我还没有为我的用例找到令人满意的答案。基本上,我的首页顶部需要一个响应式的全屏背景图像。意味着,缩小视口会导致裁剪,而不是拉伸;图像应该居中。我想避免使用 JS。

Aaron 创建了一个fiddle,几乎看起来就像我正在寻找的东西。两个问题:

  • 缩小视口时的奇怪行为(宽度低于 500 像素)
  • position: fixed;

我能够为容器复制solution of Bryce Hanscomb and Gabriela Gabriel(请参阅my fiddle):

但我未能将其扩展到全屏。这是我的代码(见fiddle):

HTML:

<div id="background">
    <img src="//dummyimage.com/600x200/0099cc/fff.png" />
</div>

CSS:

div#background {
  background-color: orange;
  min-height: 100%;
  min-width: 100%;
  overflow: hidden;
  position: absolute;
  top: 0;
  z-index: -1;
}
img {
  left: 50%;
  position: relative;
  transform: translate(-50%, 0);
  min-height: 100%;
  min-width: 100%;
}

问题 #1: 图片未占据其父 div 的全部高度(min-height 设置为 100%)。

问题#2 + #3:在狭窄的视口中,图像在右侧被截断(未居中),并显示水平滚动条。


作为旁注,有人可以告诉我where those 4 pixels come from吗?

【问题讨论】:

  • 我只是想了解更多信息:您希望图像占据背景容器的整个高度和宽度? (在你的小提琴中,这是 100% 的高和宽)如果是这样,请在你的图像上尝试 position:absolute,它将保持居中并填充背景容器
  • 另外,您对使用 CSS background-image 而不是实际的 &lt;img&gt;image 有任何问题吗?
  • 是的,为什么你只使用背景 - 包​​含?
  • 我和@ntgCleaner 有相同的想法/问题,这可能更容易作为css中的背景图像来实现,因为背景图像属性具有非常好的“姐妹属性”,例如背景-大小和背景位置。 Background-size 允许您使用非常有用的值,例如 'cover' 和 'contain'。此外, background-position 允许您使用值 'center center' 将其定位在容器的中间。如果这是一个可行的选择,请告诉我,我会提供更详细的解决方案。
  • 伙计,真不敢相信事情就这么简单。 img 上的position: absolute 实际上起到了作用。 I updated the fiddle.在这么简单的事情上花了这么多时间之后,我希望它对其他人有用......@ntgCleaner,你发布一个我可以接受的答案怎么样?

标签: css image responsive-design


【解决方案1】:

如果您在图像上使用position:absolute,您的图像将填满整个空间,也不会出现不居中的问题

div#background {
  background-color: orange;
  min-height: 100%;
  min-width: 100%;
  overflow: hidden;
  position: absolute;
  top: 0;
  z-index: -1;
}
img {
  left: 50%;
  position: absolute;
  transform: translate(-50%, 0);
  min-height: 100%;
  min-width: 100%;
}

【讨论】:

    【解决方案2】:

    底部 4px 的问题是因为图像总是像文本一样与顶部对齐,这也会在底部添加一些填充以作为文本的基线,以便字母可以悬挂在其余部分下方。

    如果你设置vertical-align: bottom 它应该像这样修复它:

    h1 {
      text-align: center;
    }
    div#container {
      background-color: black;
      height: 200px;
      width: 100%;
      margin: 50px auto;
    }
    div#content {
      background-color: orange;
      min-width: 100%;
      overflow: hidden;
    }
    img {
      left: 50%;
      position: relative;
      transform: translate(-50%, 0);
      vertical-align: bottom;
    }
    <div id="container">
      <div id="content">
        <img src="//dummyimage.com/600x200/0099cc/fff.png" />
      </div>
    </div>

    对于图像的居中对齐,我个人建议实际使用 css background-image,然后像这样设置 background-position

    div#background {
      background-color: orange;
      min-height: 100%;
      min-width: 100%;
      position: absolute;
      top: 0;
      z-index: -1;
      background: url(//dummyimage.com/600x200/0099cc/fff.png) center center no-repeat;
      background-size: cover;
    }
    <div id="background">
    </div>

    【讨论】:

    • 感谢您的宝贵意见。这种背景图片方法看起来是个不错的选择。
    猜你喜欢
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 2020-04-01
    • 2014-03-15
    • 1970-01-01
    • 2014-07-13
    相关资源
    最近更新 更多