【问题标题】:css cutting off instead of scaling downcss 切断而不是缩小
【发布时间】:2017-08-16 04:17:47
【问题描述】:

使用以下 css,我正在尝试将附加图像用作背景图像。我希望图像能够以可扩展的方式调整大小,因为我将浏览器缩小了,但是,在 600 像素时,女性的头部不再可见。换句话说,css不是减少图像内容的比例(即缩小),而是简单地切断图像的右侧。有没有办法在缩小浏览器大小时缩小图像而不是让 css 切断它的右侧?

html {
    height: 100%;
}
body {
    font-family: Lato, sans-serif;
    color: #333;
    font-size: 15px;
    line-height: 23px;
    font-weight: 300;
}
.hero-section {
    width: 100%;
    background-image: url('./3479295472_7d97d686e4_b_couple.jpg');
    background-position: 50% 45%;
    background-size: cover;
}

<div class="hero-section">
  <div class="hero-overlay-section">
    <div class="container hero-container w-container">
    <h1 class="hero-title" data-ix="hero-fade-in" style="opacity: 1; transform: translateX(0px) translateY(0px) translateZ(0px); transition: opacity 500ms, transform 500ms;">Jim</h1>
    <h1 class="and hero-title" data-ix="hero-fade-in-2" style="opacity: 1; transition: opacity 1500ms;">&amp;</h1>
<h1 class="hero-title" data-ix="hero-fade-in-3" style="opacity: 1; transform: translateX(0px) translateY(0px) translateZ(0px); transition: opacity 500ms, transform 500ms;">Karen</h1>
<div class="hero-divider-line" data-ix="hero-fade-in-4" style="opacity: 1; width: 120px; transition: opacity 500ms, width 500ms;"></div>
    <div class="hero-date-wrapper">
    <h2 class="hero-date-title" data-ix="hero-fade-in-5" style="opacity: 1; transition: opacity 500ms;">November 5th, 2018</h2>
    </div>
</div>
</div>
</div>

注意,根据this SO question 上的评论,background-size 应该会有所帮助,但我的情况并非如此。

更新:

我也尝试过,得到了相同的结果(一旦我减小浏览器大小,女人的头就被砍掉了)。

background-size: contain;
background-repeat: no-repeat;

【问题讨论】:

  • 注意,图片来自flickr.com/photos/heroiclife/3479295472/in/…的创作共用
  • 你能发布你的 html 代码来解决这个问题吗?
  • @MarouenMhiri 发布了它
  • 您仍然希望背景图像填充高度对吗?您不能真正让图像填充高度和宽度,同时保持纵横比,而不是从图像中裁剪掉一些东西。还是要从左侧而不是右侧裁剪图像?
  • 从左侧切割而不是从右侧切割将背景位置更改为 100% 45%

标签: html css


【解决方案1】:

其他人关于background-size: cover;background-position: center center 的说法是正确的。

基本上,您有两种选择。

  1. 您可以强制图像永远不会被裁剪。最好的例子是&lt;img&gt; 的默认行为。它会挤压和调整大小,但不会被裁剪。这样做的缺点是您必须保持图像的原始纵横比。

  2. 您可以忽略图像的纵横比,并告诉它填充容器,无论比例如何。这是background-size: cover; background-position: center center;,解决方案。您可以调整位置,以免重要部分被裁剪。

如果您必须保持纵横比,并且让它填充容器,那么唯一的解决方案是顶部/底部或左侧/右侧的黑条(例如在 4:3 电视上观看宽屏电影)。

【讨论】:

  • 干得好布赖恩。
【解决方案2】:

您无法将图像显示为与任何尺寸的容器完美匹配,您必须丢失一些部分。

您可以做的是选择要显示的图像中最重要的部分,这是通过操纵background-position来完成的

通常,在大多数情况下,最重要的部分是中心。 所以我们使用background-position: 50% 50%; background-size: cover;

你可以从这里开始。

【讨论】:

    【解决方案3】:

    有几种方法可以解决此问题,其中最好的方法是使用 HTML5。 代码如下:

    html { 
      background: url(images/bg.jpg) no-repeat center center fixed; 
      -webkit-background-size: cover;
      -moz-background-size: cover;
      -o-background-size: cover;
      background-size: cover;
    }
    

    代码的来源和其他方法和演示可以在https://css-tricks.com/perfect-full-page-background-image/找到

    【讨论】:

      【解决方案4】:

      为主体添加一个以像素为单位的高度。我添加了 700px,但是你可以添加任何你喜欢的东西。

      在英雄部分,将您的 CSS 更改为以下内容:

          .hero-section {
          width: 100%;
          height: 100%;
          background-image: url('./3479295472_7d97d686e4_b_couple.jpg');                         
          background-size: contain;
          background-repeat: no-repeat;
         }
      

      似乎关键是指定高度和宽度以及背景大小和背景重复指令。

      可以根据您的喜好调整高度和宽度,并且图像会缩放。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-11-26
        • 1970-01-01
        • 1970-01-01
        • 2013-06-20
        • 2017-08-23
        • 1970-01-01
        • 2013-11-23
        相关资源
        最近更新 更多