How to align content from different columns at the same height?

时间:2019-04-08 13:48:16

标签: css twitter-bootstrap

I need to align my buttons to the same height. I already changed the height of the columns to the height amount of the longest column (493px). I just need to move the button down, so that it is at the same height of the longest column.

.col-sm-6.col-md-4.boxes {
    background-color: red;
    height: 493px;
    /* height: 100%; */
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<div class="container">
    <div class="row">
        <div class="col-sm-6 col-md-4 boxes">
            <figure class="panel panel-default">
                <div class="panel-heading">
                    <img src="img/image.jpg" class="img-responsive">
                </div>
                <div class="panel-body">
                    <h3 class="panel-title">TITLE sample</h3>
                    SAMPLE TEXT .........
                </div>
                <div class="panel-footer"><a href="sample.aspx" class="btn btn-primary btn-block">BUTTON SAMPLE</a></div>
            </figure>
        </div>
        <div class="col-sm-6 col-md-4 boxes">
            <figure class="panel panel-default">
                <div class="panel-heading">
                    <img src="img/image.jpg" class="img-responsive">
                </div>
                <div class="panel-body">
                    <h3 class="panel-title">TITLE sample</h3>
                    SAMPLE TEXT .........
                    SAMPLE TEXT ........
                </div>
                <div class="panel-footer"><a href="sample.aspx" class="btn btn-primary btn-block">BUTTON SAMPLE</a></div>
            </figure>
        </div>
       <div class="col-sm-6 col-md-4 boxes">
            <figure class="panel panel-default">
                <div class="panel-heading">
                    <img src="img/image.jpg" class="img-responsive">
                </div>
                <div class="panel-body">
                    <h3 class="panel-title">TITLE sample</h3>
                    SAMPLE TEXT .........
                    SAMPLE TEXT ........
                </div>
                <div class="panel-footer"><a href="sample.aspx" class="btn btn-primary btn-block">BUTTON SAMPLE</a></div>
            </figure>
        </div>
    </div>
</div>

2 个答案:

答案 0 :(得分:1)

May be this will work for you.

.col-sm-6.col-md-4.boxes {
    background-color: red;
    height: auto;
}

答案 1 :(得分:0)

I would suggest using bootstrap built-in classes. No need to set absolute pixel values. With the following classes the column will automatically adjust to the taller one.

Give <figure> elements these classes :

h-100 to set their height to 100%.
d-flex to set them to display: flex
flex-column to set them to flex-direction: column.

Then give panel-body the class flex-grow-1 so that it fills all the available space and pushes the footer/button down.

Then, for better style, give a margin-bottom (such as mb-3) to .panel-footer

.col-sm-6.col-md-4.boxes {
    background-color: red;
    height: 493px; /* kept for height */
    /* height: 100%; */
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<div class="container">
    <div class="row">
        <div class="col-sm-6 col-md-4 boxes">
            <figure class="panel panel-default h-100 d-flex flex-column">
                <div class="panel-heading">
                    <img src="img/image.jpg" class="img-responsive">
                </div>
                <div class="panel-body flex-grow-1">
                    <h3 class="panel-title">TITLE sample</h3>
                    SAMPLE TEXT .........
                </div>
                <div class="panel-footer mb-3"><a href="sample.aspx" class="btn btn-primary btn-block">BUTTON SAMPLE</a></div>
            </figure>
        </div>
        <div class="col-sm-6 col-md-4 boxes">
            <figure class="panel panel-default h-100 d-flex flex-column">
                <div class="panel-heading">
                    <img src="img/image.jpg" class="img-responsive">
                </div>
                <div class="panel-body flex-grow-1">
                    <h3 class="panel-title">TITLE sample</h3>
                    SAMPLE TEXT .........
                    SAMPLE TEXT ........
                </div>
                <div class="panel-footer mb-3"><a href="sample.aspx" class="btn btn-primary btn-block">BUTTON SAMPLE</a></div>
            </figure>
        </div>
       <div class="col-sm-6 col-md-4 boxes">
            <figure class="panel panel-default h-100 d-flex flex-column">
                <div class="panel-heading">
                    <img src="img/image.jpg" class="img-responsive">
                </div>
                <div class="panel-body flex-grow-1">
                    <h3 class="panel-title">TITLE sample</h3>
                    SAMPLE TEXT .........
                    SAMPLE TEXT ........
                </div>
                <div class="panel-footer mb-3"><a href="sample.aspx" class="btn btn-primary btn-block">BUTTON SAMPLE</a></div>
            </figure>
        </div>
    </div>
</div>

See a full example on this JSFiddle