简体   繁体   English

如何使图片的最大高度等于包含文本的同级图片?

[英]How to make image max height equal to its sibling that contains a text?

I have the following html: 我有以下html:

<div class="container">
    <div class="post">
        <div class="row">
            <div class="col-md-6 content">
                <p>
                     //Text From Json File
                </p>
            </div>
            <div class="col-md-6 post-img">
                <img src='http://placehold.it/1000/200' />
            </div>
        </div>
    </div>  
</div>

I want the image inside the div with class post-img not to exceed the hight of the div with class content . 我希望带有post-img类的div内的图像不超过带有class content的div的高度。

I tried: 我试过了:

.post 
{
    display:table;
    width:100%;
}
.post .row
{
    display:table-row;
}
.post .row > div
{
    display: table-cell;

}

but the image exceed the other div . 但图像超出了另一个div。

You can achieve the effect you want with combination of flex and absolute positioning 通过将弹性和绝对定位相结合,可以实现所需的效果

 .container { display: flex; justify-content: stretch; } .text { width: 50%; } .image { // this is so that the .image has any height padding-bottom: 1px; position: relative; width: 50%; } .image img { position: absolute; max-height: 100%; left: 0 } 
 <div class="container"> <div class="text"> Sed nec libero urna. Proin laoreet molestie turpis. Nullam elementum molestie viverra. Praesent sit amet leo eget libero dapibus efficitur. Fusce pulvinar est massa, vel vestibulum velit consectetur vitae. Nam at dolor libero. Fusce eleifend nisl quis vehicula ultrices. estie turpis. tur.i </div> <div class="image"> <img src="http://via.placeholder.com/350x150"> </div> </div> 

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

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