gpt4 book ai didi

html - 如何在保持图像纵横比的同时增加图像高度

转载 作者:太空宇宙 更新时间:2023-11-04 06:19:09 25 4
gpt4 key购买 nike

我想将图片的高度增加到 400 像素。但是,图像不会在保持纵横比的同时填充 div。

我之前为图像添加了 height:100%,同时为父 div 添加了 400px 的固定高度,然后为图像添加了 object-fit: cover。但是,在调整页面大小时,图像不会保持其比例,而是会挤压/折叠。

任何帮助都会很棒。谢谢。

#test {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}

#test h2 {
font-family: 'Poppins';
text-transform: uppercase;
font-weight: 600;
font-size: 1.3em;
color: #333;
margin-bottom: 20px;
}

#picwrapper {
width: 85%;
}

@media only screen and (max-width: 986px) {
#picwrapper {
flex-direction: column;
}
}

#picwrapper {
display: flex;
flex-wrap: wrap;
}

.third {
width: 33.3333333333%;
position: relative;
}

@media only screen and (max-width: 986px) {
.third {
width: 100%;
}
}

.third img {
max-width: 100%;
height: auto;
display: block;
padding-top: 10%;
/* 4:3 Aspect Ratio */
}

.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 100%;
width: 100%;
opacity: 0;
transition: .5s ease;
background: linear-gradient(rgba(25, 25, 25, 0.9), rgba(25, 25, 25, 0.9));
}

.overlay-text {
font-family: 'Poppins';
font-weight: 500;
}

.third:hover .overlay {
opacity: 1;
}

.overlay-text {
color: white;
font-size: 20px;
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
text-align: center;
}
<section id="test">
<div id="picwrapper">
<div class="third">
<img src="https://cdn.zmescience.com/wp-
content/uploads/2018/11/Magnificent_CME_Erupts_on_the_Sun_-_August_31.jpg">
<a href="AUDI/audi.html">
<div class="overlay">
<h1 class="overlay-text">Parkash Sandhu</h1>
</div>
</a>
</div>
<div class="third">
<img src="https://solarsystem.nasa.gov/system/news_items/main_images/853_ph3_waxing_gibbous_2k.jpg">
<a href="#">
<div class="overlay">
<h1 class="overlay-text">Flo Music</h1>
</div>
</a>
</div>
<div class="third">
<img src="https://cdn.zmescience.com/wp-content/uploads/2018/11/Magnificent_CME_Erupts_on_the_Sun_-_August_31.jpg">
<a href="#">
<div class="overlay">
<h1 class="overlay-text">British Athletics</h1>
</div>
</a>
</div>
<div class="third">
<img src="https://solarsystem.nasa.gov/system/news_items/main_images/853_ph3_waxing_gibbous_2k.jpg">
<a href="AUDI/audi.html">
<div class="overlay">
<h1 class="overlay-text">Audi</h1>
</div>
</a>
</div>
<div class="third">
<img src="https://cdn.zmescience.com/wp-
content/uploads/2018/11/Magnificent_CME_Erupts_on_the_Sun_-
_August_31.jpg">
<a href="Virgin Atlantic">
<div class="overlay">
<h1 class="overlay-text">Virgin Atlantic</h1>
</div>
</a>
</div>
</div>
</section>

最佳答案

在接下来的内容中,我将对您所追求的做出一些假设。

我假设您希望图像始终保持其纵横比 (4:3),但仍会随着屏幕的增大/缩小按比例缩放。

在下面,您会发现您的代码的另一种实现方式,但我认为它可以满足您的需求。至少,也许它会让您朝着正确的方向前进。

(顺便说一句,感谢 Andy Bell 的宽高比技术。请参阅此处:https://andy-bell.design/wrote/creating-an-aspect-ratio-css-utility/)

[class*="aspect-ratio-"] {
display: block;
position: relative;
width: 100%;
}

[class*="aspect-ratio-"] > * {
display: block;
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
}

.aspect-ratio-tv {
padding-top: 75%; /* 4:3 */
}

.gallery {
display: flex;
flex-wrap: wrap;
list-style: none;
padding: 0;
margin: 0;
}

.gallery {
width: 100%;
}

.gallery li {
flex-basis: 100%;
}

@media screen and (min-width: 580px) {
.gallery li {
flex-basis: 50%;
}
}

@media screen and (min-width: 960px) {
.gallery li {
flex-basis: 33.33333%;
}
}

.gallery img {
object-fit: cover;
}

.overlay {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
opacity: 0;
transition: 0.5s ease;
background: linear-gradient(rgba(25, 25, 25, 0.9), rgba(25, 25, 25, 0.9));
cursor: pointer;
display: flex;
justify-content: center;
align-items: center;
}

.overlay-text {
color: white;
font-size: 20px;
text-decoration: none;
}

.overlay:hover {
opacity: 1;
text-decoration: none;
}
<ul class="gallery">
<li>
<div class="aspect-ratio-tv">
<img src="https://source.unsplash.com/phvbkGThElM/800x600" alt="A neon ferris wheel" loading="lazy" />
<a href="#0" class="overlay">
<h1 class="overlay-text">
TEST HEADING
</h1>
</a>
</div>
</li>
<li>
<div class="aspect-ratio-tv">
<img src="https://source.unsplash.com/H_mTtLykvKs/800x682" alt="A dimly lit drum kit" loading="lazy" />
<a href="#0" class="overlay">
<h1 class="overlay-text">
TEST HEADING
</h1>
</a>
</div>
</li>
<li>
<div class="aspect-ratio-tv">
<img src="https://source.unsplash.com/Hc42xXu_WOg/800x567" alt="Blueberries, close up" loading="lazy" />
<a href="#0" class="overlay">
<h1 class="overlay-text">
TEST HEADING
</h1>
</a>
</div>
</li>
<li>
<div class="aspect-ratio-tv">
<img src="https://source.unsplash.com/MfynxC5_tiU/800x999" alt="High angle waterfall" loading="lazy" />
<a href="#0" class="overlay">
<h1 class="overlay-text">
TEST HEADING
</h1>
</a>

</div>
</li>
<li>
<div class="aspect-ratio-tv">
<img src="https://source.unsplash.com/7ZTx1iA7a7Q/800x397" alt="Sunset coastal scence" loading="lazy" />
<a href="#0" class="overlay">
<h1 class="overlay-text">
TEST HEADING
</h1>
</a>

</div>
</li>
<li>
<div class="aspect-ratio-tv">
<img src="https://source.unsplash.com/pRvy1j4aINE/800x785" alt="High angle shot of a recording studio" loading="lazy" />
<a href="#0" class="overlay">
<h1 class="overlay-text">
TEST HEADING
</h1>
</a>
</div>
</li>
</ul>

请看这里拿笔:https://codepen.io/anon/pen/oOeBOj

关于html - 如何在保持图像纵横比的同时增加图像高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55660944/

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