Click here to Skip to main content
15,886,718 members
Articles / Programming Languages / Javascript
Tip/Trick

Equal Height Columns By jQuery

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
18 Apr 2015CPOL 9K   2   1
A jQuery function to set all columns in a div with equal height

Introduction

This tip shows how to set all columns in a div container to equal height by a single jQuery function.

Using the Code

First, add the following HTML markup to a web page for testing purposes.

HTML
    <div class="col-container">
        <div class="col col1">
This is a sentence to test jQuery column equal height function.
This is a sentence to test jQuery column equal height function.
This is a sentence to test jQuery column equal height function.
        </div>
        <div class="col col2">
This is a sentence to test jQuery column equal height function.
This is a sentence to test jQuery column equal height function.
This is a sentence to test jQuery column equal height function.
This is a sentence to test jQuery column equal height function.
This is a sentence to test jQuery column equal height function.
This is a sentence to test jQuery column equal height function.
This is a sentence to test jQuery column equal height function.
This is a sentence to test jQuery column equal height function.
This is a sentence to test jQuery column equal height function.
        </div>
        <div class="col col3">
This is a sentence to test jQuery column equal height function.
This is a sentence to test jQuery column equal height function.
This is a sentence to test jQuery column equal height function.
This is a sentence to test jQuery column equal height function.
This is a sentence to test jQuery column equal height function.
This is a sentence to test jQuery column equal height function.
        </div>
    </div>

Second, add some CSS code for the HTML markup.

CSS
.col-container
{
    width: 80%;
    margin: 0 auto;
}

.col-container:after
{
    content: "";
    display: block;
    clear: both;
}

.col
{
    float: left;
    width: 33.3%;
}

.col1
{
    background-color: #FF0000;
}

.col2
{
    background-color: #00FF00;
}

.col3
{
    background-color: #0000FF;
}

All columns inside the container are floated to left. CSS :after selector is used to add an empty content after the container and clear the float. This trick makes subsequent content display normally. Otherwise, the following content may overlap with these columns.

Last, add the jQuery code to set these columns with equal height.

JavaScript
$(document).ready(function () {
    // Define function to set the all columns to the height of the tallest column
    var equalHeight = function () {
        // Reset the height of each column to default calculated by browser
        $('.col').css('height', 'auto');
        var maxHeight = 0;
        // Get maximum height
        $('.col').each(function () {
            if ($(this).height() > maxHeight) {
                maxHeight = $(this).height();
            }
        });
        // Set each column height to the maximum height
        $('.col').css('height', maxHeight);
    };
    // Set equal height on page load
    equalHeight();
    // Set equal height when the container of these columns is resized
    $('.col-container').resize(function () {
        equalHeight();
    });
});

The function equalHeight first sets the height of each column to auto. This allows the browser to automatically calculate its height. Then it iterates through all columns and finds the maximum height. Last, it sets all columns to the maximum height.

All columns inside a div are set to equal height when the page is loaded and when the div container is resized.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Canada Canada
I am working with ASP.NET related technologies.

Comments and Discussions

 
AnswerThanks forma share this Pin
Antonio Gago Moledo20-Apr-15 20:32
Antonio Gago Moledo20-Apr-15 20:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.