簡體   English   中英

如何將圖像放入高度為100%的桌子內?

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

我無法使圖像適應css制成的桌子的高度100%。 如下所示,表格和圖像完全忽略了父元素的尺寸。

一切都必須是動態的,這就是為什么我要使用百分比。

 .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> 

預期結果:

在此處輸入圖片說明

更新:是否可以通過flex修復它?

這是您需要的嗎?

 .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> 

您必須給它指定一個特定的高度(例如200px),否則高度不能為100%。 如果您有一個高度為200像素的框; 您可以將圖像高度設為100%,即200px

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM