gpt4 book ai didi

css - Flexbox 图像高度调整

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

我无法完全调整图像大小。

理想情况下,我希望图像调整大小以便选项适合屏幕,目前它只适合宽度,为什么这样?是否可以通过使用 css 获得我想要的行为,也许需要媒体查询?在谷歌上焦头烂额试图找到类似的问题,但没有运气!

提前致谢!

更新:

Image is not resized

澄清一下,我的问题是图像没有调整大小,如果窗口的高度足够小 - 标题消失,我得到一个滚动条。我希望所有内容都按比例缩小,这样就不需要滚动条了。

.container {
min-height: 100vh;
display: flex;
flex-direction: column;
background-color: aqua;
}

.content {
display: flex;
overflow-x: auto;
flex: 2 1 auto;
flex-wrap: nowrap;
}

.content::-webkit-scrollbar {
display: none;
}

.option {
display: flex;
flex-direction: column;
margin: auto 1em;
width: 90%;
height: 70%;
text-align: justify;
border: 3px solid black;
}

.img {
flex: 1;
}

.title {
flex: 0;
font-weight: bold;
text-align: center;
flex: 1;
margin: auto;
}

.desc {
flex: 0;
}

.header {
flex: 2;
text-align: center;
background-color: red;
}

.footer {
flex: 1;
background-color: brown;
}
<div class="container">
<div class="header">
Fusce pellentesque ante.
</div>
<div class="content">
<div class="option">
<img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
<p class="title">Proin hendrerit.</p>
<p class="desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur fringilla non lacus tincidunt suscipit. Nam nec arcu a erat convallis.</p>
</div>
<div class="option">
<img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
<p class="title">Nullam at.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ut congue eros. Aenean sit amet quam efficitur, lacinia ligula ac.</p>
</div>
<div class="option">
<img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
<p class="title">Sed nec.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam porttitor nisi mauris, ac interdum tellus pulvinar id. Morbi non molestie</p>
</div>
<div class="option">
<img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
<p class="title">Morbi in.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam non rutrum arcu, ac posuere odio. Nunc in dolor eget nisi.</p>
</div>
<div class="option">
<img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
<p class="title">Mauris ac.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque pulvinar orci non consectetur accumsan. Aenean posuere, felis commodo congue pulvinar.</p>
</div>
</div>
<div class="footer">
<p>Quisque venenatis volutpat dictum. Praesent.</p>
</div>
</div>

Fiddle

最佳答案

希望这就是您想要的。如果这是正确的答案!这是我所做的。

  • 将高度和宽度的容器类设置为 100%,并将显示设置为 flexflex-direction: column,以便页眉、页脚和内容类垂直对齐。
  • 现在,如果您将内容类 flex 属性设置为 1,它将占用剩余的宽度和高度。
  • 为选项类提供 display: flex 属性并设置 flex-direction: column 以便图像、标题和说明垂直对齐。
  • 通过将 30px 的 min-height 分配给标题和选项中的 p,其余空间可以分配给图像,我希望这是您想要的。

* {
margin: 0;
padding: 0;
}

.content {
min-height: 100%;
width: 100%;
min-width: 100%;
display: flex;
flex-direction: row;
box-sizing: border-box;
padding: 30px 0;
}

.content .option {
flex: 1;
max-height: 100vh;
min-width: 100%;
display: flex;
flex-direction: column;
}

.content .option img {
height: calc(100% - 160px);
width: 100%;
}
.content .option p {
height: 20px;
}

.content .option p:nth-child(2) {
height: 40px;
}

.header,
.footer {
position: fixed;
left: 0;
right: 0;
background-color: #ccc;
height: 30px;
}

.header {
top: 0;
}

.footer {
bottom: 0;
}
<div class="container">
<div class="header">
Fusce pellentesque ante.
</div>
<div class="content">
<div class="option">
<img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
<p class="title">Proin hendrerit.</p>
<p class="desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur fringilla non lacus tincidunt suscipit. Nam nec arcu a erat convallis.</p>
</div>
<div class="option">
<img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
<p class="title">Nullam at.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ut congue eros. Aenean sit amet quam efficitur, lacinia ligula ac.</p>
</div>
<div class="option">
<img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
<p class="title">Sed nec.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam porttitor nisi mauris, ac interdum tellus pulvinar id. Morbi non molestie</p>
</div>
<div class="option">
<img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
<p class="title">Morbi in.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam non rutrum arcu, ac posuere odio. Nunc in dolor eget nisi.</p>
</div>
<div class="option">
<img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
<p class="title">Mauris ac.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque pulvinar orci non consectetur accumsan. Aenean posuere, felis commodo congue pulvinar.</p>
</div>
</div>
<div class="footer">
<p>Quisque venenatis volutpat dictum. Praesent.</p>
</div>
</div>

关于css - Flexbox 图像高度调整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53091238/

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