简体   繁体   English

在伸缩列中将图像设置为始终为全高

[英]Set image to always be full height in flex column

I have two columns set to 50%, left with text and right with image that should fill whole column vertically. 我有两列设置为50%,左边是文本,右边是图像,它们应垂直填充整列。 The problem is that when text is a little bigger, there is a gap below the image. 问题是,当文本稍大时,图像下方会出现间隙。 Is it possible to always have the image occupy the full height (but in same ratio)? 是否可以始终使图像占据整个高度(但比例相同)? For example, the image could get wider than its column, in that case I could just set overflow hidden. 例如,图像可能变得比其列宽,在这种情况下,我可以将溢出设置为隐藏。

 .flex { display: flex; border: 3px solid black; padding: 10px; } .col { border: 1px dashed #aaa; } .left { padding: 30px; } 
 <div class="flex"> <div class="col left"> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Molestias eos fugiat deserunt ullam tempore? Aspernatur eligendi dolores explicabo officiis adipisci, incidunt distinctio tempore culpa, esse cumque atque repellendus eius delectus fugit quia odit ut porro laborum alias. Aliquam et est neque ut, rem ab omnis? Culpa rerum, vel ad magnam iusto explicabo at consequatur deserunt quo repellendus. Sequi nulla nemo a magni voluptates. Nemo mollitia, ut ex temporibus voluptatem incidunt nostrum quo, quod reprehenderit omnis! Sequi nulla nemo a magni voluptates. </p> </div> <div class="col right"> <img src="http://placehold.it/550x250" alt=""> </div> </div> 

You have to set following CSS properties to image 您必须将以下CSS属性设置为image

height:100%; width:auto;

 .flex { display: flex; border: 3px solid black; padding: 10px; } .col { border: 1px dashed #aaa; overflow-x: hidden; } .left { padding: 30px; } .right img { height: 100%; width: auto; } 
 <div class="flex"> <div class="col left"> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Molestias eos fugiat deserunt ullam tempore? Aspernatur eligendi dolores explicabo officiis adipisci, incidunt distinctio tempore culpa, esse cumque atque repellendus eius delectus fugit quia odit ut porro laborum alias. Aliquam et est neque ut, rem ab omnis? Culpa rerum, vel ad magnam iusto explicabo at consequatur deserunt quo repellendus. Sequi nulla nemo a magni voluptates. Nemo mollitia, ut ex temporibus voluptatem incidunt nostrum quo, quod reprehenderit omnis! Sequi nulla nemo a magni voluptates. </p> </div> <div class="col right"> <img src="http://placehold.it/550x250" alt=""> </div> </div> 

The quickest way to get the image to fill the vertical height of the container would be to make the parent a flex container. 使图像填充到容器的垂直高度的最快方法是使父对象成为flex容器。 This automatically applies align-items: stretch to the children (in this case, the image). 这将自动应用align-items: stretch到子项(在这种情况下为图像)。

.right { display: flex; }

From that point, you could use various methods to set the correct aspect ratio to the image. 从那时起,您可以使用各种方法为图像设置正确的宽高比。 You could use percentage height, if you have defined heights on the parent / ancestors. 如果您在父/祖先上定义了高度,则可以使用百分比高度。

Or you could use the object-fit property. 或者,您可以使用object-fit属性。

 .flex { display: flex; border: 3px solid black; padding: 10px; } .col { border: 1px dashed #aaa; } .left { padding: 30px; } .right { display: flex; } img { object-fit: cover; } 
 <div class="flex"> <div class="col left"> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Molestias eos fugiat deserunt ullam tempore? Aspernatur eligendi dolores explicabo officiis adipisci, incidunt distinctio tempore culpa, esse cumque atque repellendus eius delectus fugit quia odit ut porro laborum alias. Aliquam et est neque ut, rem ab omnis? Culpa rerum, vel ad magnam iusto explicabo at consequatur deserunt quo repellendus. Sequi nulla nemo a magni voluptates. Nemo mollitia, ut ex temporibus voluptatem incidunt nostrum quo, quod reprehenderit omnis! Sequi nulla nemo a magni voluptates. </p> </div> <div class="col right"> <img src="http://placehold.it/550x250" alt=""> </div> </div> 

Note that object-fit is not natively supported in Internet Explorer. 请注意,Internet Explorer本机不支持object-fit For more details and a workaround see: Why isn't object-fit working in flexbox? 有关更多详细信息和解决方法,请参见: 为什么对象适合在flexbox中不起作用?

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

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