css 截断而不是缩小

Lea*_*cim 2 html css

使用以下 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>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

请注意,根据对此问题的 background-size评论应该有所帮助,但不适用于我的情况。

更新:

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

background-size: contain;
background-repeat: no-repeat;
Run Code Online (Sandbox Code Playgroud)

我的浏览器显示的(截取的)图像宽度为 600 像素

Bri*_*ian 5

其他人所说的background-size: cover;都是background-position: center center正确的。

基本上,你有两个选择。

  1. 您可以强制图像永远不会被裁剪。最好的例子是<img>. 它会被挤压并调整大小,但不会被裁剪。这样做的缺点是您必须保持图像的原始长宽比。

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

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