css 被截断而不是缩小

问题描述 投票:0回答:4

使用以下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>

注意,根据对这个SO问题的评论,

background-size
应该有帮助,但不适合我。

更新:

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

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

html css
4个回答
5
投票

Answer 已被原作者删除,以抗议 Stackoverflow 将其用户出卖给 OpenAI。


1
投票

您无法显示完美适合任何尺寸容器的图像,您必须丢失一些部分。

您可以做的是选择要显示的图像中最重要的部分,这是通过操作

background-position

来完成的

通常,在大多数情况下,最重要的部分是中心。 所以我们用

background-position: 50% 50%; background-size: cover;

你可以从这里开始


1
投票

有多种方法可以解决此问题,其中最好的方法是使用 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/

找到

0
投票

向主体添加以像素为单位的高度。我添加了 700px,但您可以添加任何您喜欢的内容。

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

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.