简体   繁体   English

如何使列中的图像缩小以适合另一列

[英]How to get image in a column to shrink to fit the other column

https://codepen.io/jihyundotkim/pen/eYJMzJR https://codepen.io/jihyundotkim/pen/eYJMzJR

HTML HTML

<div class="columns">
  <div class="column">
    <p>Brownie cake sweet. Cake bear claw gingerbread. Carrot cake macaroon jujubes candy candy canes cake. Brownie jelly beans liquorice.</p>

<p>Topping pie dessert danish. Jelly-o cotton candy soufflé chocolate cake topping toffee sugar plum lollipop donut. Danish muffin pudding marshmallow lollipop bear claw halvah icing. Icing jelly-o gummi bears.</p>

<p>Topping oat cake pastry bonbon muffin. Cotton candy chocolate bar bear claw chocolate bar lollipop cake. Halvah sugar plum apple pie. Cookie danish oat cake cupcake tootsie roll powder.</p>

<p>Croissant pudding cupcake topping fruitcake chupa chups candy canes liquorice. Chupa chups tiramisu brownie sesame snaps jujubes chocolate bar fruitcake. Oat cake donut sweet roll. Powder marzipan soufflé toffee.</p>

<p>Chocolate pie cookie chocolate cake chocolate cake cotton candy gummi bears topping jelly beans. Jelly-o gummies chocolate. Apple pie chocolate candy powder. Jelly-o halvah pastry carrot cake bear claw sweet roll.</p>
  </div>
  <div class="column">
    <img src="https://i.kym-cdn.com/photos/images/newsfeed/001/295/524/cda.jpg" />
  </div>
</div>

CSS CSS

.columns {display: flex}
.column {flex: 1 1 50%}
img {width: 100%; height: 100%; object-fit: cover;}

2 columns, left one with text and right one with image. 2列,左一列文字,右一列图片。

When the text is longer than the image, the image expands to fit the text column's height, which is good.当文本比图像长时,图像会扩大以适应文本列的高度,这很好。 When the image ratio is long and there's not as much text, there's a gap between the bottom line of text and image.当图像比例较长且文字不多时,文字和图像的底线之间会出现间隙。 Is there CSS a way to force the image on the right to resize to the height of left column?是否有 CSS 强制右侧图像调整到左列高度的方法?

Thanks in advance提前致谢

Below CSS will force the height of the image the same as the left column. CSS 下面将强制图像的高度与左列相同。

.columns {
  display: flex;
}
.column {
  flex: 1 1 50%;
  display: flex;
  flex-direction: column;
}

img {
  flex: 1 1 0;
  object-fit: contain;
  overflow: auto;
}

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

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