html - 设置 img 父级的高度会破坏 flex align-items 和 text-align
问题描述
我有一个拍卖应用程序,其中列表显示在右侧,图像和文本描述。我希望.img
是max-height
和max-width
的90%
。Max-width
有效,但为了让 max-height 工作,我必须设置.img
's 包含块 ( .a_img
) 的高度。一旦我这样做了, .image 的align-items: center;
andtext-align: center;
声明就不再起作用了。发生了什么事,我该如何解决这个问题?谢谢你。
.listings {
display: flex;
margin-bottom: 1rem;
height: 116.8px;
}
.image {
flex: 1.2;
display: flex;
align-items: center;
text-align: center;
max-height: 100%;
}
.text {
flex: 2;
}
.img {
max-height: 90%;
max-width: 90%;
}
.a_img {
height: 100%;
}
<div class="listings">
<div class="image">
<a class="a_img" href="www.google.com">
<img class="img" src="https://cdn.shopify.com/s/files/1/2660/5202/products/shopify-image_0c6f7a25-f7ae-409f-a89b-df63d9c7b463_1400x.jpg?v=1586379733" alt="Product Image">
</a>
</div>
<div class="text">
some text<br>some text<br>some text<br>some text
</div>
</div>
解决方案
您可以简单地将其替换max-height
为height
on .image
,然后将您想要的所有内容更改为预期的输出,如下所示
.listings {
display: flex;
margin-bottom: 1rem;
height: 116.8px;
}
.image {
/*flex: 1;*/
/* altered */
display: flex;
align-items: center;
justify-content: center;
text-align: center;
height: 100%;
/* altered */
}
.text {
flex: 1;
}
.img {
max-height: 90%;
max-width: 90%;
}
.a_img {
height: 100%;
}
<div class="listings">
<div class="image">
<a class="a_img" href="www.google.com">
<img class="img" src="https://cdn.shopify.com/s/files/1/2660/5202/products/shopify-image_0c6f7a25-f7ae-409f-a89b-df63d9c7b463_1400x.jpg?v=1586379733" alt="Product Image">
</a>
</div>
<div class="text">
some text<br>some text<br>some text<br>some text
</div>
</div>
推荐阅读
- python - 模板文件重写 django 中的媒体文件
- selenium - 启动:Windows 打开许多 Edge 实例,每个实例都带有“ERROR:file_io_win.cc(180)] CreateFile settings.dat: Access is denied. (0x5)”
- css - 如何将颜色应用于活动链接并从reactjs中的其他链接中删除
- python - 如何减少 pandas 滚动在多列上运行时间过长的运行时间 - pandas
- tree - 之字形树遍历
- angular - 用户单击 mat-form-field 时如何关闭 mat-select
- python-3.x - 我如何在课堂上接受用户输入。Python
- node.js - 根据Mongoose中的其他属性返回数组值和分组的匹配记录?
- ios - 我可以在 Swift 中制作大的 rightBarButtonItem 吗?
- python - 如何突出显示geojson文件中的不同区域?