Style Sheets
  Home arrow Style Sheets arrow Page 2 - Using Auto Margins to Center DIVs with CSS
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 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-02

    Table of Contents:
  • Using Auto Margins to Center DIVs with CSS
  • Building a basic fixed layout with CSS auto margins
  • Adding CSS styles to a sample web document with some structural markup
  • Completing the web page layout

  • 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 to Center DIVs with CSS - Building a basic fixed layout with CSS auto margins


    (Page 2 of 4 )

    The first method that I’m going to discuss regarding the construction of centered web page designs using CSS margins bases its entire functionality on auto margins. Despite its seemingly complex name, this approach is pretty simple to implement; it consists of assigning a value of “auto” to the left and right margins of a selected div to place it at the center of a web document.

    Now that you have a vague idea of how to use auto margins, it’s time to demonstrate how to utilize them for creating a fully-centered web page layout. To do that, I’m going to code a few simple CSS styles, which will be attached to a sample (X)HTML file, similar to the example developed in the preceding article.

    That being explained, here are the CSS code that implements the aforementioned auto-margins approach:


    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,#footer{

    padding: 10px;

    background: #ffc;

    }

    #wrapper{

    width: 800px;

    margin-left: auto;

    margin-right: auto;

    }

    #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;

    }

    #maincol{

    clear: both;

    padding: 10px;

    background: #fff;

    }


    In this particular case, I defined a group of basic CSS selectors, which will be responsible for styling a web document composed of the classic header and footer sections, then a navigation bar, and finally a main area. However, you should pay attention to the way that the DIV identified as “wrapper” has been styled, since this one is responsible for centering the whole web document.

    As you can see, this specific DIV has a fixed width of 800 px, while its respective left and right margins have been assigned a value of “auto,” meaning that it will be positioned at the center of the web document. That’s all. Do you realize how easy it is to construct a centered layout using auto margins? I guess you do!

    The previous CSS styles look rather disarticulated, however, when analyzed separately. Thus, in the section to come I’ll be coding the corresponding structural markup, to help you understand how these two layers can be linked with each other.

    To learn how this integration task will be performed, go ahead and read the following segment.

    More Style Sheets Articles
    More By Alejandro Gervasio


       · This third part of the series demonstrates how to use CSS auto-margins for building...
     

    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