yanknudtskov
1/19/2016 - 10:32 AM

Fluid Same Size Rows

Fluid Same Size Rows

<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset='UTF-8'>

	<title>Equal Height in Rows</title>

	<link rel='stylesheet' href='css/style.css'>

	<style>
	  #page-wrap > div { display: block; float: left; margin: 0 10px 10px 0; background: blue; width: 50px; }
	  #page-wrap > div:nth-child(1) { height: 100px; }
	  #page-wrap > div:nth-child(2) { height: 120px; }
	  #page-wrap > div:nth-child(3) { height: 110px; }
	  #page-wrap > div:nth-child(4) { height: 140px; }
	  #page-wrap > div:nth-child(5) { height: 60px; }
	  #page-wrap > div:nth-child(6) { height: 80px; }
	  #page-wrap > div:nth-child(7) { height: 50px; }
	  #page-wrap > div:nth-child(8) { height: 110px; }
	</style>

	<script src='//ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js'></script>
	<script>

		// these are (ruh-roh) globals. You could wrap in an
		// immediately-Invoked Function Expression (IIFE) if you wanted to...
		var currentTallest = 0,
		    currentRowStart = 0,
		    rowDivs = new Array();

		function setConformingHeight(el, newHeight) {
			// set the height to something new, but remember the original height in case things change
			el.data("originalHeight", (el.data("originalHeight") == undefined) ? (el.height()) : (el.data("originalHeight")));
			el.height(newHeight);
		}

		function getOriginalHeight(el) {
			// if the height has changed, send the originalHeight
			return (el.data("originalHeight") == undefined) ? (el.height()) : (el.data("originalHeight"));
		}

		function columnConform() {

			// find the tallest DIV in the row, and set the heights of all of the DIVs to match it.
			$('#page-wrap > div').each(function() {

				// "caching"
				var $el = $(this);

				var topPosition = $el.position().top;

				if (currentRowStart != topPosition) {

					// we just came to a new row.  Set all the heights on the completed row
					for(currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) setConformingHeight(rowDivs[currentDiv], currentTallest);

					// set the variables for the new row
					rowDivs.length = 0; // empty the array
					currentRowStart = topPosition;
					currentTallest = getOriginalHeight($el);
					rowDivs.push($el);

				} else {

					// another div on the current row.  Add it to the list and check if it's taller
					rowDivs.push($el);
					currentTallest = (currentTallest < getOriginalHeight($el)) ? (getOriginalHeight($el)) : (currentTallest);

				}
				// do the last row
				for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) setConformingHeight(rowDivs[currentDiv], currentTallest);

			});

		}


		$(window).resize(function() {
			columnConform();
		});

		// Dom Ready
		// You might also want to wait until window.onload if images are the things that
		// are unequalizing the blocks
		$(function() {
			columnConform();
		});

	</script>
</head>

<body>

<div id="demo-top-bar">

  <div id="demo-bar-inside">

    <h2 id="demo-bar-badge">
      <a href="/">CSS-Tricks Example</a>
    </h2>

    <div id="demo-bar-buttons">
          </div>

  </div>

</div>
	<div id="page-wrap">

		  <div></div>
		  <div></div>
		  <div></div>
		  <div></div>
		  <div></div>
		  <div></div>
		  <div></div>
		  <div></div>

	</div>

 <style type="text/css" style="display: none !important;">
	* {
		margin: 0;
		padding: 0;
	}
	body {
		overflow-x: hidden;
	}
	#demo-top-bar {
		text-align: left;
		background: #222;
		position: relative;
		zoom: 1;
		width: 100% !important;
		z-index: 6000;
		padding: 20px 0 20px;
	}
	#demo-bar-inside {
		width: 960px;
		margin: 0 auto;
		position: relative;
		overflow: hidden;
	}
	#demo-bar-buttons {
		padding-top: 10px;
		float: right;
	}
	#demo-bar-buttons a {
		font-size: 12px;
		margin-left: 20px;
		color: white;
		margin: 2px 0;
		text-decoration: none;
		font: 14px "Lucida Grande", Sans-Serif !important;
	}
	#demo-bar-buttons a:hover,
	#demo-bar-buttons a:focus {
		text-decoration: underline;
	}
	#demo-bar-badge {
		display: inline-block;
		width: 302px;
		padding: 0 !important;
		margin: 0 !important;
		background-color: transparent !important;
	}
	#demo-bar-badge a {
		display: block;
		width: 100%;
		height: 38px;
		border-radius: 0;
		bottom: auto;
		margin: 0;
		background: url(/images/examples-logo.png) no-repeat;
		background-size: 100%;
		overflow: hidden;
		text-indent: -9999px;
	}
	#demo-bar-badge:before, #demo-bar-badge:after {
		display: none !important;
	}
</style>
</body>

</html>