简体   繁体   中英

make image fit height of bootstrap 3 grid

In a bootstrap 3 grid, one of the columns contains an image. How can one make it 100% height of another column? (with biggest height)

img-responsive will fit the width, but not the height. Here's a fiddle: http://jsfiddle.net/mariusandreiana/V2Hy6/17/

<div class="container">
    <div>some content</div>    
    <div class="row bg">
        <div class="col-xs-1">Text</div>
        <div class="col-xs-6">
            <p>
                <h1>John Dough</h1>
                1155 Saint. St. #33
                <br/>Orlando, FL 32765</p>
        </div>
        <div class="col-xs-5">
            <img class="img-responsive" src="https://c402277.ssl.cf1.rackcdn.com/photos/2842/images/hero_small/shutterstock_12730534.jpg?1352150501">
        </div>
    </div>
    <div>more content</div>
</div>

Desired result: image's height is from "John Dough" to "Orlando", and the width should be proportional.

One option would be to use javascript to set img's height, but can it be done only via CSS? Would a table be a better fit than the grid?

Note: the "John Dough"'s contents are unknown, it's final height is known only at runtime.

You have to set a max-height to the parent :

Fiddle : http://jsfiddle.net/V2Hy6/19/

CSS :

.bg {
    background-color: #FFAAAA;
    height:100%;
}

.img-container{
    height:100%;
    max-height:200px;
}

img{
    height:100% !important;
}

HTML :

<div class="container">
    <div>some content</div>    
    <div class="row bg">
        <div class="col-xs-1">Text</div>
        <div class="col-xs-6">
            <p>
                <h1>John Dough</h1>
                1155 Saint. St. #33
                <br/>Orlando, FL 32765</p>
        </div>
        <div class="col-xs-5 img-container">
            <img class="img-responsive" src="https://c402277.ssl.cf1.rackcdn.com/photos/2842/images/hero_small/shutterstock_12730534.jpg?1352150501">
        </div>
    </div>
    <div>more content</div>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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