Centering Multiple Floated Elements

The need was to have multiple floated fixed-width elements, sometimes two, sometimes three, to stay centered on the page without needing to adjust the html code.  Not as easy as one would think.  Of course its easy if you know how many elements you are going to have, and thus know the width that the floated elements need -- just put the floated divs within another wrapper div of fixed width and center that div.  Its a bit harder when you don\'t know the actual width of the wrapper div.  The trick is to tell the wrapper div to behave like a table, so instead of expanding to a width of 100%, it only expands to the width of the divs that it contains:



         width: 200px;  			
         height: 300px; 			
         float: left; 		
         margin: 0 auto; 			
        display: table; 			 		

<div class="wrap"> 	
    <div class="box">Left</div>
    <div class="box">Right</div>

I can add another floated div to this, and all three of them should still stay centered on the page.

Related Posts

Fixed Position Div Isn't Staying Fixed When Scrolling on Android

My nice fixed position toolbar wasn't sticking as it should as I scrolled through on my mobile browser window...
Read Post

Simple Sticky Element

The user scrolls down the page and once the scrolling reaches the top of an element of my choosing, that element sticks to the top of the window, so it's always visible from that point on...
Read Post

Chrome displays a different height than what Chrome says the height should be.

I was changing the height of an element, but no matter where I specified the height, it wasn't displaying what I was putting in
Read Post

A Nicer Select Dropdown

Better select boxes are all over the internet, but I actually couldn't find one that I liked, and that worked for what I needed...
Read Post