This tutorial contains a JavaScript function that can be used to set the same Height for multiple HTML elements in page.
It is useful when you have multiple DIVs (or other HTML tags) with dynamic content (different height), and you want to make them all the same height.


Add the following JavaScript script at the bottom of your HTML document, before the </body>, or after all the tags you want to align.
Then, in this instruction: alignHgs('id1', 'id2', 'id3');, replace the arguments: 'id1', 'id2', 'id3' with the ID of the HTML tags you want to align.
You can add as many IDs as you want, each one between quotes, separated by comma.


Script code:

<script>
// - coursesweb.net

// get the height of the HTML TAGs which ID passed in parameters
// sets the max-height to each tag
function alignHgs() {
 // get the arguments and their number
 var args = alignHgs.arguments;

 // traverse the array with arguments, get the height of each TAG and add it in the 'ar_hgs' array
 var ar_hgs =[];
 for (var i=0; i<args.length; i++) {
 ar_hgs[i] = document.getElementById(args[i]).clientHeight;
 }

 // get the largest number (max height) in the 'ar_hgs' array
 var max_hg = Math.max.apply(null, ar_hgs);

 // set max height to all tags in arguments
 for (var i=0; i<args.length; i++) {
 document.getElementById(args[i]).style.height = max_hg+'px';
 }
}

// calls the function with the IDs of the tags
alignHgs('id1', 'id2', 'id3');
</script>

This script gets the maxim height of the elements with the ID passed as arguments, then it sets that value to each of those tags, when the page is loaded.

If you want to align the heights when a link, or a button is clicked, just delete the alignHgs('id1', 'id2', 'id3'); from the script and add this instruction in that link (or button), with onclick='alignHgs('id1', 'id2', 'id3');'. Se also the example below.


This example has three DIVs with different heights, and a button which calls the alignHgs() function when a user clicks on it:
<!doctype html>
<html>
<head>
<title>title</title>
<style>
div {
 float:left;
 width:30%;
 margin:2px;
 border:2px solid blue;
 background-color:#e7fee8;
}
</style>
</head>
<body>
<h4>example JavaScript Height align</h4>
<p>The browser will display the following result (<span class='si'>click the 'Align' button</span>):</p>

<div id='dv1'>marplo.net - Free courses</div>
<div id='dv2'>
 coursesweb.net - Free Web development courses:<br />
 - video tutorials<br />
 - free lessons and download resources
</div>
<div id='dv3'>
 www.php.net - PHP team website<br />
 - server-side scripting language.
</div>
<br style='clear:both;' />
<button onclick="alignHgs('dv1', 'dv2', 'dv3')">Align</button>

<script>
//from: coursesweb.net/

// get the hight of the HTML TAGs which ID are sent as parameters
// sets the max-height to each tag
function alignHgs() {
 // get the arguments and their number
 var args = alignHgs.arguments;

 // traverse the array with arguments, get the hight of each TAG and add it in the 'ar_hgs' array
 var ar_hgs =[];
 for (var i=0; i<args.length; i++) {
 ar_hgs[i] = document.getElementById(args[i]).clientHeight;
 }

 // get the largest number (max height) in the 'ar_hgs' array
 var max_hg = Math.max.apply(null, ar_hgs);

 // set max height to all tags in arguments
 for (var i=0; i<args.length; i++) {
 document.getElementById(args[i]).style.height = max_hg+'px';
 }
}
</script>
</body>
</html>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Make HTML elements the same height

Last accessed pages

  1. Contact page - CoursesWeb (10968)
  2. Tabs effect with CSS (11675)
  3. Making DIV Contents Scroll Horizontally, with multiple Div`s inside (17574)
  4. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (15674)
  5. SHA512 Encrypt hash in JavaScript (12625)

Popular pages this month

  1. Making DIV Contents Scroll Horizontally, with multiple Div`s inside (1975)
  2. Contact page - CoursesWeb (1923)
  3. Tabs effect with CSS (1923)
  4. Insert, Select and Update NULL value in MySQL (1228)
  5. PHP getElementById and getElementsByTagName (722)