简体   繁体   English

如何将图像放入高度为100%的桌子内?

[英]How to fit an image inside a table with 100% height?

I can't manage to get the image to fit 100% to the height of a css made table. 我无法使图像适应css制成的桌子的高度100%。 As you see below, the table and image is totally ignoring the parent element's dimensions. 如下所示,表格和图像完全忽略了父元素的尺寸。

Everything needs to be dynamically that's why I work with percent. 一切都必须是动态的,这就是为什么我要使用百分比。

 .img { max-width: 50%; height: 100px; // <-- This should be able to be dynamic ( anything ) } .img .table { display: table; width: 100%; height: 100%; } .img .row { display: table-row; } .img .cell { display: table-cell; } .img .row.img-el, .img .row.img-el .cell { height: 100%; } .img img { max-width: auto; height: 100%; } 
 <div class="img pull-left gap-right bigger"> <div class="table"> <div class="row img-el"> <div class="cell"><img src="https://1.bp.blogspot.com/-9QM7ciGXRkQ/V1hsB-wNLBI/AAAAAAAAMoA/eYbSHs00PTAjrI4QAmvYAIGCUe1AuRAnwCLcB/s1600/bryan_cranston_0095.jpg" style="height:100%;float:left" /></div> </div> <div class="row"> <div class="cell img-text">Hello</div> </div> </div> </div> 

Expected result: 预期结果:

在此处输入图片说明

Update: Is it possible to fix it with flex in any way? 更新:是否可以通过flex修复它?

Is this what you need? 这是您需要的吗?

 .img { max-width: 50%; max-height: 100%; } .img .table { display: table; width: 100%; height: 100%; } .img .row { display: table-row; } .img .cell { display: table-cell; } .img .row.img-el, .img .row.img-el .cell { height: 100%; } .img img { max-width: auto; height: 100%; } .cell img { width: 100%; height: auto; } 
 <div class="img pull-left gap-right bigger"> <div class="table"> <div class="row img-el"> <div class="cell"><img src="https://1.bp.blogspot.com/-9QM7ciGXRkQ/V1hsB-wNLBI/AAAAAAAAMoA/eYbSHs00PTAjrI4QAmvYAIGCUe1AuRAnwCLcB/s1600/bryan_cranston_0095.jpg" style="float:left" /></div> </div> <div class="row"> <div class="cell img-text">Hello</div> </div> </div> </div> 

you have to give it a specific height like 200px it cant take a height of 100% of nothing. 您必须给它指定一个特定的高度(例如200px),否则高度不能为100%。 If you had a box with the height of 200px; 如果您有一个高度为200像素的框; you can make the image height be 100% which is 200px 您可以将图像高度设为100%,即200px

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM