Style Sheets
  Home arrow Style Sheets arrow Page 4 - Using Auto Margins with Multiple Columns t...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
STYLE SHEETS

Using Auto Margins with Multiple Columns to Center DIVs with CSS
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2009-04-16

    Table of Contents:
  • Using Auto Margins with Multiple Columns to Center DIVs with CSS
  • Building a centered web document layout with CSS auto-margins
  • Adding CSS styles to the previous (X)HTML page
  • Tying the CSS styles to the structural markup

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using Auto Margins with Multiple Columns to Center DIVs with CSS - Tying the CSS styles to the structural markup


    (Page 4 of 4 )


    As you saw in the previous section, a simple implementation of CSS auto-margins permits us to build centered web page layouts with ease, regardless of the number of columns included in the page.

    Nonetheless, it's necessary to tie the previous CSS styles to the structural markup of the web document created in the first segment of this tutorial, so you can see more clearly how this document is rendered on screen.

    Bearing this concept in mind, here's a brand new (X)HTML file, which assembles these two layers and shows the finished layout. Take a look at it, please:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <title>Example on centering DIVS with a liquid layout (uses CSS auto margins)</title>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    <style type="text/css">

    body{

    padding: 0;

    margin: 0;

    background: #999;

    }

    h2{

    margin: 0;

    font: bold 18px Arial, Helvetica, sans-serif;

    color: #000;

    }

    p{

    font: normal 12px Arial, Helvetica, sans-serif;

    color: #000;

    }

    #header{

    padding: 10px;

    background: #ffc;

    }

    #wrapper{

    width: 70%;

    margin-left: auto;

    margin-right: auto;

    background: #fff;

    }

    #navbar{

    padding: 10px;

    background: #fff;

    }

    #navbar ul{

    list-style: none;

    padding: 0;

    margin: 0;

    }

    #navbar li{

    display: inline;

    padding: 0;

    margin: 0;

    }

    #navbar a:link,#navbar a:visited{

    margin-left: 20px;

    font: normal 12px Arial, Helvetica, sans-serif;

    color: #000;

    text-align: center;

    text-decoration: none;

    }

    #navbar a:hover{

    background: #fc0;

    }

    #leftcol{

    float: left;

    width: 20%;

    padding: 10px;

    }

    #maincol{

    float: left;

    width: 50%;

    padding: 10px;

    }

    #rightcol{

    float: right;

    width: 20%;

    padding: 10px;

    }

    #footer{

    clear: both;

    padding: 10px;

    background: #ffc;

    }

    </style>

    </head>

    <body>

    <div id="wrapper">

    <div id="header">

    <h2>This is the header of the web page</h2>

    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas enim. Nulla facilisi. Vestibulum accumsan augue vulputate justo. Fusce faucibus. Sed blandit, neque sed lacinia nonummy, diam quam imperdiet justo, at dictum augue nunc a neque. Sed urna lacus, tincidunt at, aliquam id, fringilla id, felis. Vivamus feugiat molestie quam. Sed id dolor. Sed ac purus id sapien.</p>

    </div>

    <div id="navbar">

    <ul>

    <li><a href="#">Home</a></li>

    <li><a href="#" id="about">About Us</a></li>

    <li><a href="#">Services</a></li>

    <li><a href="#">Products</a></li>

    <li><a href="#">Contact</a></li>

    </ul>

    </div>

    <div id="leftcol">

    <h2>This is the left column of the web page</h2>

    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas enim. Nulla facilisi. Vestibulum accumsan augue vulputate justo. Fusce faucibus. Sed blandit, neque sed lacinia nonummy, diam quam imperdiet justo, at dictum augue nunc a neque. Sed urna lacus, tincidunt at, aliquam id, fringilla id, felis. Vivamus feugiat molestie quam. Sed id dolor. Sed ac purus id sapien.</p>

    </div>

    <div id="maincol">

    <h2>This is main section of the web page</h2>

    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas enim. Nulla facilisi. Vestibulum accumsan augue vulputate justo. Fusce faucibus. Sed blandit, neque sed lacinia nonummy, diam quam imperdiet justo, at dictum augue nunc a neque. Sed urna lacus, tincidunt at, aliquam id, fringilla id, felis. Vivamus feugiat molestie quam. Sed id dolor. Sed ac purus id sapien.</p>

    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas enim. Nulla facilisi. Vestibulum accumsan augue vulputate justo. Fusce faucibus. Sed blandit, neque sed lacinia nonummy, diam quam imperdiet justo, at dictum augue nunc a neque. Sed urna lacus, tincidunt at, aliquam id, fringilla id, felis. Vivamus feugiat molestie quam. Sed id dolor. Sed ac purus id sapien.</p>

    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas enim. Nulla facilisi. Vestibulum accumsan augue vulputate justo. Fusce faucibus. Sed blandit, neque sed lacinia nonummy, diam quam imperdiet justo, at dictum augue nunc a neque. Sed urna lacus, tincidunt at, aliquam id, fringilla id, felis. Vivamus feugiat molestie quam. Sed id dolor. Sed ac purus id sapien.</p>

    </div>

    <div id="rightcol">

    <h2>This is the right column of the web page</h2>

    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas enim. Nulla facilisi. Vestibulum accumsan augue vulputate justo. Fusce faucibus. Sed blandit, neque sed lacinia nonummy, diam quam imperdiet justo, at dictum augue nunc a neque. Sed urna lacus, tincidunt at, aliquam id, fringilla id, felis. Vivamus feugiat molestie quam. Sed id dolor. Sed ac purus id sapien.</p>

    </div>

    <div id="footer">

    <h2>This is the footer section of the web page</h2>

    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas enim. Nulla facilisi. Vestibulum accumsan augue vulputate justo. Fusce faucibus. Sed blandit, neque sed lacinia nonummy, diam quam imperdiet justo, at dictum augue nunc a neque. Sed urna lacus, tincidunt at, aliquam id, fringilla id, felis. Vivamus feugiat molestie quam. Sed id dolor. Sed ac purus id sapien.</p>

    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas enim. Nulla facilisi. Vestibulum accumsan augue vulputate justo. Fusce faucibus. Sed blandit, neque sed lacinia nonummy, diam quam imperdiet justo, at dictum augue nunc a neque. Sed urna lacus, tincidunt at, aliquam id, fringilla id, felis. Vivamus feugiat molestie quam. Sed id dolor. Sed ac purus id sapien.</p>

    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas enim. Nulla facilisi. Vestibulum accumsan augue vulputate justo. Fusce faucibus. Sed blandit, neque sed lacinia nonummy, diam quam imperdiet justo, at dictum augue nunc a neque. Sed urna lacus, tincidunt at, aliquam id, fringilla id, felis. Vivamus feugiat molestie quam. Sed id dolor. Sed ac purus id sapien.</p>

    </div>

    </div>

    </body>

    </html>


    Definitely, understanding how the above (X)HTML file works isn't rocket science, is it? Yet, despite its simple structure, it comes in useful for illustrating how CSS auto-margins can be used in conjunction with a multi-column web page to construct a centered layout.

    In addition to listing the previous code sample, below I included an screen capture that shows how this web page design is displayed on the browser:



    Now that you have at your disposal a functional example that demonstrates the actual functionality of CSS auto-margins, feel free to use it as a starting point for developing your own centered web document layouts. The experience will be fun, trust me!

    Final thoughts

    In this fifth part of the series, you hopefully learned how to make use of CSS auto-margins for building a centered, multi-column web page design. As you saw for yourself, this process is pretty straightforward and only requires a basic background in CSS and (X)HTML. That's all.

    In the forthcoming article, I'll be introducing yet another simple CSS approach, called "evened margins," which can also be used for creating centered designs in a truly painless way.

    Therefore, if you wish to learn how this technique will be utilized, don't miss the next part!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · This tutorial shows how to expand the use of CSS auto-margins, in order to build a...
     

    STYLE SHEETS ARTICLES

    - Image Replacement CSS Techniques
    - Using BlueTrip`s Success, Notice and Error C...
    - More Uses for the Thin and Caps CSS Classes ...
    - Styling Definition Lists with the BlueTrip C...
    - Styling Unordered and Ordered HTML Lists wit...
    - Using the BlueTrip CSS Framework`s Thin and ...
    - Adding Borders to Web Page Columns with Blue...
    - Introducing the BlueTrip CSS Framework
    - Using a Background Grid to Assist Web Page L...
    - Extending the Rule Of Thirds for Web Page La...
    - A Two-Column Web Page Layout Based on the Ru...
    - Using the Rule Of Thirds for Web Page Layout
    - Swapping Columns Using the Divine Ratio for ...
    - Using the Golden Ratio in Liquid Web Page De...
    - Fundamental Design Principles for Web Page L...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek