Masonry not behaving correctly. Why is there a big space between red and blue box?

Hello Experts,

Give the code below:

<html>
<head>
<title>Test: blocks with dynamic heights</title>
<meta charset="ISO-8859-1" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jquerymobile/1.4.5/jquery.mobile.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquerymobile/1.4.5/jquery.mobile.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/masonry/3.3.0/masonry.pkgd.min.js"></script>
<script>
var container = $('#galleryContainer');
//initialize
container.masonry({
	columnWidth: 310,
	itemSelector: '.imgContainer'
});
</script>
<style>
.imgContainer{ float:left; padding: 5px; }
.footer{ clear:both; }
</style>
</head>
<body>
<!-- PAGE 1 begins -->
	<div data-role="page" id="gallery" data-theme="a">			
		<!-- CONTENT -->
		<div data-role="main">
			<div id='galleryContainer'>
				<div class='imgContainer'>
				<div><a href='#'><img src="#" style="background-color:blue;width:300px;height:300px"></a></div>
				</div>
				<div class='imgContainer'>
				<div><a href='#'><img src="#" style="background-color:yellow;width:300px;height:600px"></a></div>
				</div>
				<div class='imgContainer'>
				<div><a href='#'><img src="#" style="background-color:red;width:300px;height:600px"></a></div>
				</div>
			</div>
		</div>
		
		
		<!-- FOOTER -->
		<div data-role="footer" class="footer">
			<div >footer</div>
		</div>	
	</div>
<!-- PAGE 1 ENDS -->	
</body>

Open in new window


when you change the browser size (Right-Left; stop when red img container moves down),
the red block goes below the blue BUT with a big empty space. Shouldn't Masonry framework place it just below blue block (without the big space in between)?

See what I mean?

Thanks for your help.

Regards,
epifanio67Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Robert SchuttSoftware EngineerCommented:
The init code must be run after the DOM has loaded. Easiest way since you're already including jQuery is:
<script>
$(function() {
	var container = $('#galleryContainer');
	//initialize
	container.masonry({
		columnWidth: 310,
		itemSelector: '.imgContainer'
	});
});
</script>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
epifanio67Author Commented:
Thank you Robert.. that was it!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.