【问题标题】:How do I prevent images from being squished?如何防止图像被压扁?
【发布时间】:2020-04-11 00:20:23
【问题描述】:

我正在编写一个简单的英雄部分,每 2 秒自动更改背景中的图像。但是,由于图像具有不同的尺寸,因此它们在更大的屏幕上看起来确实被压扁了。如何解决此问题并使其响应并适应各种屏幕尺寸?

$hero: 100vh;
$white: #fff;
$black: #000;
$blue: #6db8d7;
$grey: #808080;

* {
  box-sizing: border-box;
}
body {
  padding: 0;
  margin: 0;
}
.hero-body {
  height: 100vh;
  width: 100vw;
  position: relative;
  overflow: hidden;
  text-align: center;
}
.hero-text {
  position: absolute;
  bottom: 40%;
  left: 20px;
}
img {
  width: 100%;
  height: $hero;
}
.title,
h4 {
  color: $white;
  font-size: 1em;
  text-shadow: 1px 1px 2px $black;
  text-align: center;
}
button {
  background-color: $blue;
  padding: 10px;
  text-transform: uppercase;
  font-weight: 500;
  color: $white;
  text-align: center;
  border: 1px solid transparent;
  border-radius: 6px;
  box-shadow: 0 3px 6px 0 $grey;
  cursor: pointer;
  &:hover,
  &:focus {
    transform: scale(1.05);
    transition: all 0.5s ease-in-out;
  }
}
<section class="hero">
  <div class="hero-body">
    <img class="bg-img" src="https://source.unsplash.com/alEZLDPPRBU">
    <img class="bg-img" src="https://source.unsplash.com/Ov0u44CyGdM">
    <img class="bg-img" src="https://source.unsplash.com/8beTH4VkhLI">
    <div class="hero-text">
      <h1 class="title">
        Delight, Fresh, Fruity
      </h1>
      <h4>Come join us for our organic cones!</h4>
      <button>View flavors</button>
    </div>
  </div>
</section>

【问题讨论】:

  • 如果只针对现代浏览器,您可以覆盖容器:img { width: 100%; height: 100%; object-fit: cover;(其工作方式与 background-size: cover; 类似),但您可能希望将文本设为静态,并且而是将图像设为position: absolute;
  • 谢谢!这完美地工作。 @chriskirknielsen

标签: html css


【解决方案1】:

我建议将高度设置为自动,宽度为 100%,以保持图像成比例。您可以为移动设备添加媒体查询以将宽度设置为例如50%(取决于设备的大小 - 您决定)并将高度设置为自动。 您还可以使用新的picture 标签来适应各种屏幕尺寸。

示例图片标签用法:

<picture>
  <source media="(min-width: 650px)" srcset="img_pink_flowers.jpg">
  <source media="(min-width: 465px)" srcset="img_white_flower.jpg">
  <img src="img_orange_flowers.jpg" alt="Flowers" style="width:auto;">
</picture>

希望对你有帮助

【讨论】:

  • 对不同的屏幕尺寸使用不同的图片似乎是最好的解决方案。
  • 是的,图片标签可以容纳这个。记住优化图像的好处也很重要。 (不是问题的一部分,但很高兴注意到,我认为..)
【解决方案2】:

object-fit: cover 设置为&lt;img&gt;

:root {
  --hero: 100vh;
  --white: #fff;
  --black: #000;
  --blue: #6db8d7;
  --grey: #808080;
}

* {
  box-sizing: border-box;
}

body {
  padding: 0;
  margin: 0;
}

.hero-body {
  height: 100vh;
  width: 100vw;
  position: relative;
  overflow: hidden;
  text-align: center;
}

.hero-text {
  position: absolute;
  bottom: 40%;
  left: 20px;
}

img {
  width: 100%;
  height: var(--hero);
  object-fit: cover;
}

.title,
h4 {
  color: var(--white);
  font-size: 1em;
  text-shadow: 1px 1px 2px var(--black);
  text-align: center;
}

button {
  background-color: var(--blue);
  padding: 10px;
  text-transform: uppercase;
  font-weight: 500;
  color: var(--white);
  text-align: center;
  border: 1px solid transparent;
  border-radius: 6px;
  box-shadow: 0 3px 6px 0 var(--grey);
  cursor: pointer;
}

button:hover,
button:focus {
  transform: scale(1.05);
  transition: all 0.5s ease-in-out;
}
<section class="hero">
  <div class="hero-body">
    <img class="bg-img" src="https://source.unsplash.com/alEZLDPPRBU">
    <img class="bg-img" src="https://source.unsplash.com/Ov0u44CyGdM">
    <img class="bg-img" src="https://source.unsplash.com/8beTH4VkhLI">
    <div class="hero-text">
      <h1 class="title">
        Delight, Fresh, Fruity
      </h1>
      <h4>Come join us for our organic cones!</h4>
      <button>View flavors</button>
    </div>
  </div>
</section>

【讨论】:

  • 哇,它就像一个魅力。我以前从未听说过这个。谢谢!
  • @ChuChu,乐于助人。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-14
  • 1970-01-01
  • 2022-11-18
  • 1970-01-01
相关资源
最近更新 更多