HTML responsive design: about float left

Hello Experts,

Give the example code below:

.imgContainer{ float:left; padding: 5px; }
.footer{ clear:both; }
<!-- PAGE 1 begins -->
	<div data-role="page" id="gallery" data-theme="a">			
		<!-- CONTENT -->
		<div data-role="main">
			<div class="galleryContainer">
				<div class='imgContainer'>
				<div><a href='#'><img src="#" style="background-color:blue;width:300px;height:300px"></a></div>
				<div class='imgContainer'>
				<div><a href='#'><img src="#" style="background-color:yellow;width:300px;height:600px"></a></div>
				<div class='imgContainer'>
				<div><a href='#'><img src="#" style="background-color:red;width:300px;height:600px"></a></div>
		<!-- FOOTER -->
		<div data-role="footer" class="footer">
			<div >footer</div>
<!-- PAGE 1 ENDS -->	

Open in new window

when you change the browser size (Right-Left; stop when red img container moves down), the red imgContainer goes below the blue BUT with a big empty space. When creating a responsive design, is there a way to place the red imgContainers just below the blue imgContainer?

Who is Participating?

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

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.

David S.Commented:
Not if they're all floated in the same direction, e.g. left. If you float the yellow one to the right or use display:inline-block for some of them instead of floating all of them, you can get much closer to what you want. You may want to make those style changes within a media query block.
Robert SchuttSoftware EngineerCommented:
I'm not sure that is exactly possible with CSS alone, unless the heights of these boxes are fixed, then the previous comment may get you there.

There is a javascript plugin for this if you need to have blocks with dynamic heights:

I've set up a jsFiddle with your html/css, the link to the plugin ( // ) and the example javascript from the above website changed for your elements:
var msnry = new Masonry( '.galleryContainer', { // note it only works for 1
  columnWidth: 310, // 300 + 2 * 5 (padding)
  itemSelector: '.imgContainer'

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:
good suggestion Robert... thank you..
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

From novice to tech pro — start learning today.