CSS-Floating DIVs

This tutorial is related to CSS layout techniques. If you seek something like the following, then this tutorial might be useful to you.

 

Simple Div-ing

Your body1 text here.

 

 

 

 

 

 

Your body2 text here.

 

 

 

 

 

 

 

 

 

 

Your body3 text here.

 

Open Notepad or your plain text editor software and type/copy&paste the following HTML code:

Snippet 1

  1. <div class="box_container">

  2. <div class="box_header">Simple Div-ing</div>

  3. <div class="box_content">

  4. <div class="panel1s">

  5. <div class="panel1_sub">

  6. Your body text here.

  7. </div><!--End panel1_sub -->

  8. </div><!--End panel1 -->

  9. <div class="panel2s">

  10. <div class="panel2_sub">

  11. Your body text here.

  12. </div><!--End panel2_sub -->

  13. </div><!--End pane2s -->

  14. <div class="panel3s">

  15. <div class="panel3_sub">

  16. Your body text here.

  17. </div><!--End panel3_sub -->

  18. </div><!--End panel3s -->

  19. </div><!--End box_content -->

  20. <div class="box_footer"> © You!</div>

  21. </div> <!--End of box_container-->

Here's the CSS styling

Snippet 2

  1. *{/*This is optional, I like to remove all browser
    default settings and then add my own later.*/

  2. margin:0; padding:0;

  3. }

  4. .box_container{ /*Once you give this a defined width, you can also add
    margin: auto auto; to centre the container*/

  5. width: 400px;

  6. color: #fff;

  7. font-family:Arial, Helvetica, sans-serif;

  8. font-size:12px;

  9. }

  10. .box_header{

  11. background:#003333;

  12. text-align: center;

  13. }

  14. .panel1s, .panel2s, .panel3s{

  15. float: left;

  16. width: 133px;

  17. margin:auto auto;

  18. }

  19. .panel1s{

  20. background: #336600;

  21. }

  22. .panel2s{

  23. background:#FF6600;

  24. }

  25. .panel3s{

  26. background: #663399;

  27. width: 134px; /*Over writes previous 133px width so that the panel
    meets the edge of the box_container*/

  28. }

  29. .panel1_sub, .panel2_sub, .panel3_sub{

  30. padding: .3em .3em;

  31. width: 100px;

  32. margin:auto auto;

  33. }

  34. .box_footer{

  35. /* clear: both; drops the div below the floating ones. You could have just used clear: left; but this only clears divs floating to the left, which is what we need, but if you added a div to float right, then you wouldn't have to worry about editing the code. clear:both; clears the DIV in question from all previously floating divs.*/

  36. clear:both;

  37. background:#000033;

  38. text-indent: 10px;

  39. }

You would have noticed that I added .panel#s_sub divs to the .panel#s and none for the .box_header and .box_footer, you are allowed to add more sub/nested divs. I only did for demonstration purposes, sub/nested divs allows you to format inside text and the like without messing with the main structure. In the example, I gave the .panel#_sub paddings, which would keep the text and any other elements away from the edges.

If you wanted to keep spacing between your panels, then you could have just given your .panel2s, and .panel3s margin-left (or margin-right) settings. Whenever you add margins, you must also consider your widths of your floating divs, any panel that breaks beyond the container will drop. Let's say you added a margin-left: 40px; to .panel2s, then this would push .panel3s to the edge, once .panel3s passes the boundering width of the .box_container, it will no longer float next to it's previous brother, en otras palabras, .panel3s will drop! So therefore, the wrapping container, in this tutorial .box_container, should have a width large enough to contain its child DIVs/panels.

The limits are endless with what you can do. Hopefully this tutorial gives you ideas on how to take things further. Ofcourse, your own site would have larger widths, the demonstrated one is just small to fit neatly in this page. :)

For additional info, it is important that you give your main container (in this tutorial it's .box_container) a fixed width, because if a user toggles with the window size, making the window size smaller than the size of your panel containers, then you will notice that the panels will drop, and you'll lose your float:left; effect. So do remember, that floating divs need fixed width properties, and by fixed I mean don't use width: auto or width: xxx%, use px units or so.

Cheers.