CSS图片最大高度无法保持宽高比

5

视频预览

我有一个弹出式模态框,它具有固定的位置、100% 的宽度和高度。

模态框中的内容具有 max-width: 90%;max-height: 90% 属性。

图像也具有这些属性,但当改变高度时它无法保持纵横比,而在改变宽度时可以保持纵横比。(视频已经解释了这一问题)

#popup {
  display: flex;
  justify-content: center;
  align-items: center;
  position: fixed;
  top: 0;
  bottom: 0;
  opacity: 1;
  background: rgba(0, 0, 0, 0.5);
  width: 100%;
  height: 100%;
}

.popupModal {
  max-width: 90%;
  max-height: 90%;
  background: linear-gradient(0deg, #fff, #f0f0f0);
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  transform: translateY(-100px);
  transition: transform 0.5s;
}

.closeBtn {
  position: absolute;
  top: -25px;
  right: -25px;
  border-radius: 50%;
  background-color: #fff;
  font-size: 24px;
  font-weight: bold;
  color: $blue-dark;
  height: 50px;
  width: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  -webkit-box-shadow: 0 0 27px -10px rgba(0, 0, 0, 0.95);
  -moz-box-shadow: 0 0 27px -10px rgba(0, 0, 0, 0.95);
  box-shadow: 0 0 27px -10px rgba(0, 0, 0, 0.95);
  transition: box-shadow 0.2s ease;
}

.closeLine {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  background-color: #072742;
  height: 4px;
  width: 50%;
}

#closeLineOne {
  transform: rotate(45deg);
}

#closeLineTwo {
  transform: rotate(135deg);
}

.closeLine:hover {
  -webkit-box-shadow: none;
  -moz-box-shadow: none;
  box-shadow: none;
}

.popupActive {
  opacity: 1;
  z-index: 5000;
  transition: z-index 0s 0s, opacity 0.5s 0s;
}

.popupModal {
  transform: translateY(0);
}

.metalModal {
  height: 80%;
  background: transparent;
}

.metalPopup {
  display: flex;
  flex-direction: column;
  // It works if I change the max-height to height, but it then stretches out all other images.
  max-height: 100%;
  max-width: 100%;
  position: relative;
}

.metalImg {
  max-width: 100%;
  flex: 1;
  object-fit: cover;
  max-height: 100%;
  width: auto;
  height: auto;
}

.metalName {
  width: 100%;
  height: 70px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-weight: bold;
  color: white;
  font-size: 24px;
  background-color: rgba(0, 0, 0, 0.75);
  position: absolute;
  bottom: 0;
  left: 0;
}
<div id="popup" class="popupActive">
  <div class="popupModal metalModal">
    <div class="metalPopup">
      <div class="closeBtn">
        <span id="closeLineOne" class="closeLine"></span>
        <span id="closeLineTwo" class="closeLine"></span>
      </div>
      <img src="https://i.imgur.com/BeWSEDB.jpg" class="metalImg" />
      <div class="metalName">
        Metal name
      </div>
    </div>
  </div>
</div>

编辑:

当我给.metalPopup类添加display:block属性时,图像会保持纵横比。但是这会使得图像的宽度保持原始的img宽度,即使图像只是整个块的一小部分。

^ 请点击此处查看


尝试将widthheight之一指定为auto,另一个指定为 - T J
4个回答

3

所以我刚刚将 .metalPopup 的最大高度改为 height: 100%;,现在似乎可以工作了,并且不会拉伸图像,因为 .metalImg 现在有了最大高度,它删除了 .metalName 的高度(现在 .metalNameposition:relative,因为如果我想要响应式地居中模态框,它就无法居中,但仍然可以添加相对定位并减去其高度)。

#popup {
  display: flex;
  justify-content: center;
  align-items: center;
  position: fixed;
  top: 0;
  bottom: 0;
  opacity: 1;
  background: rgba(0, 0, 0, 0.5);
  width: 100%;
  height: 100%;
}

.popupModal {
  max-width: 90%;
  max-height: 90%;
  background: linear-gradient(0deg, #fff, #f0f0f0);
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  transform: translateY(-100px);
  transition: transform 0.5s;
}

.closeBtn {
  position: relative;
  top: 25px;
  margin: 0 0 0 auto;
  right: -25px;
  border-radius: 50%;
  background-color: #fff;
  font-size: 24px;
  font-weight: bold;
  color: $blue-dark;
  height: 50px;
  width: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  -webkit-box-shadow: 0 0 27px -10px rgba(0, 0, 0, 0.95);
  -moz-box-shadow: 0 0 27px -10px rgba(0, 0, 0, 0.95);
  box-shadow: 0 0 27px -10px rgba(0, 0, 0, 0.95);
  transition: box-shadow 0.2s ease;
}

.closeLine {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  background-color: #072742;
  height: 4px;
  width: 50%;
}

#closeLineOne {
  transform: rotate(45deg);
}

#closeLineTwo {
  transform: rotate(135deg);
}

.closeLine:hover {
  -webkit-box-shadow: none;
  -moz-box-shadow: none;
  box-shadow: none;
}

.popupActive {
  opacity: 1;
  z-index: 5000;
  transition: z-index 0s 0s, opacity 0.5s 0s;
}

.popupModal {
  transform: translateY(0);
}

.metalModal {
  height: 80%;
  background: transparent;
}

.metalPopup {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-width: 100%;
  position: relative;
}

.metalImg {
  max-width: 100%;
  object-fit: cover;
  max-height: calc(100% - 130px);
}

.metalName {
  width: 100%;
  height: 70px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-weight: bold;
  color: white;
  font-size: 24px;
  background-color: rgba(0, 0, 0, 0.75);
}
<div id="popup" class="popupActive">
  <div class="popupModal metalModal">
    <div class="metalPopup">
      <div class="closeBtn">
        <span id="closeLineOne" class="closeLine"></span>
        <span id="closeLineTwo" class="closeLine"></span>
      </div>
      <img src="https://i.imgur.com/BeWSEDB.jpg" class="metalImg" />
      <div class="metalName">
        Metal name
      </div>
    </div>
  </div>
</div>


1
正如人們所說,有很多種方法可以達到同樣的效果。我喜歡在我的CSS中使用object-fit: cover來實現這種效果。 - PaulProgrammer

2
如果您设置了高度,通常最好将宽度设置为自动,反之亦然。
.yourclass {
max-height: 90%;
max-width: (either unset or auto)
}


我通常遵循这个指南,但由于问题,我已经将其添加到使用它的每个元素中了。 - HenrijsS
你介意在这里更新代码吗?这样我就可以玩弄它并帮助你进一步了。 - Tonic
是的,没有太多变化,因为我仍然希望能得到答案。但这是我拥有的最新代码。 我现在正在调试它。 - HenrijsS
好的,为了让它起作用,我改变的是给.popupmodal设置了max-width: 90%; max-height: auto; - Tonic

1
你可以在CSS中使用aspect-ratio来保持视频的特定比例。
video {
   width: 90%;
   aspect-ratio: 16/9 ; // due to this video will maintain aspect ratio of 16/9
}

这几乎被所有现代浏览器支持,可以让您编写更少的代码。

1
纵横比很好,但它只适用于具有特定纵横比的静态图像。在我的情况下,有50-60张图片,它们的大小完全不同。 - HenrijsS

-1

这个怎么样?对你有效吗?

img {
  width: auto;  
  height: auto;  
  max-width:90%;
  max-height:90%;
}

它已经有那些值了。高度根据宽度进行重新缩放,但反过来则不行。 - HenrijsS

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接