Style Sheets
  Home arrow Style Sheets arrow Page 3 - 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 - Adding CSS styles to a sample web document with some structural markup


    (Page 3 of 4 )

    In the prior section, I demonstrated how to build a centered web page layout by means of CSS auto-margins. This process would be rather incomplete, though, if I didn't show you the structural markup that complements the correct implementation of this approach.

    With that idea in mind, below I listed a basic (X)HTML file, which will come in useful for illustrating how to utilize auto-margins in a concrete case. The file looks like this:


    <!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 fixed layout (uses auto margins)</title>

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

    </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="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="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>


    Essentially, the above (X)HTML file is the same that I used in the two previous parts of the series, when I discussed the utilization of the “text-align” CSS property, so you shouldn’t have major inconveniences understanding its simple structure.

    As depicted above, this file is comprised of header and footer areas, in addition to a simple link bar and a contents section. It’s that simple.

    Okay, now that I've created the bare bones structure of a web page, the last thing that I’m going to show you in this article will consist of linking this markup to the CSS styles that you learned in the preceding section, in this way completing the construction of a centered web document layout using CSS auto-margins.

    This procedure will be discussed in detail in the following section. Thus, click on the link below and keep reading. We’re almost done!

    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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek