gpt4 book ai didi

html - CSS图像最大高度不保持纵横比

转载 作者:行者123 更新时间:2023-12-04 01:05:26 26 4
gpt4 key购买 nike

Video preview
我有一个弹出模式,它的位置固定,宽度和高度为 100%。
modal里面的内容有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 时,图像保持纵横比类(class) display: block .但它使宽度保持原来的 img宽度,即使图像是整个 block 的一小部分。
^ See here .

最佳答案

所以我就改了.metalPopup最大高度为 height: 100%;它现在似乎可以工作并且不会拉伸(stretch)图像,因为 .metalImg现在有 max-height 删除 .metalName 的高度(现在是 position: 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>

关于html - CSS图像最大高度不保持纵横比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66629714/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com