【发布时间】: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;">&</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%