Style Sheets
  Home arrow Style Sheets arrow Page 3 - An Evened Margin Approach to Centering DIV...
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

An Evened Margin Approach to Centering DIVs with CSS
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 4
    2009-04-23

    Table of Contents:
  • An Evened Margin Approach to Centering DIVs with CSS
  • Using evened CSS margins with a fixed design: defining a sample web document
  • Working with evened CSS margins
  • Putting CSS styles and (X)HTML markup together

  • 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


    An Evened Margin Approach to Centering DIVs with CSS - Working with evened CSS margins


    (Page 3 of 4 )

    As I said in the introduction, the logic that drives the evened margins approach is quite simple to grasp. It bases its functionality on specifying identical values (in percentage, pixels, ems, etc.) for the left and right margins of the DIV that needs to be centered. Period.

    Even though this alignment method demonstrates a lot of common sense and relatively little “science,” it works remarkably well on many browsers. But, let’s wrap up our discussion of theory and see how the previous web page can be properly styled to look like a neat centered layout.

    To do that, I’m going to code a set of CSS styles which will rely on the concept of “evened margins” for building this specific layout. Here they are:


    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{

    margin-left: 100px;

    margin-right: 100px;

    }

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

    }


    As you can see, the above CSS code is responsible for styling all the elements included in the sample web page created before, such as its header and footer sections, <h2> selectors and paragraphs, and the navigation bar. Nonetheless, the most relevant detail to notice here is how evened margins have been applied to the man wrapping DIV.

    In this case, a value of 70px has been specified for the left and right margins of this DIV, centering the remaining elements on the web document. That was very easy to code and read, wasn’t it?

    Okay, now that you hopefully understood how evened margins were utilized for constructing a centered, fixed design, it’s time to see how the prior styles can be attached to the pertinent structural markup, by means of one single (X)HTML file.

    To see how this process will be accomplished, you’ll have to click on the link below and read the following section.

    More Style Sheets Articles
    More By Alejandro Gervasio


       · This sixth installment of the series discusses how to build a fixed centered web...
       · I should have kept reading before commenting! It appears you've got the <link> issue...
       · Apologies accepted. It happens very often.Regards.
     

    STYLE SHEETS ARTICLES

    - Using the Custom -moz-border-radius Property...
    - Using CSS3`s Border-Radius Property to Build...
    - Adding Semantic Meaning to Styled Code Block...
    - Styling Code blocks with CSS: Using pre HTML...
    - Building Rounded Corners with CSS3
    - Finishing a Casual Navigation Bar with CSS S...
    - Defining a Navigation Bar`s Hover State with...
    - Styling a Blog`s Links Bar with CSS Sprites
    - Creating an Artistic Blog Header with CSS Sp...
    - Defining the Active State of Menu Sections f...
    - Styling the Hover State of a CSS Sprite-Base...
    - Building CSS Sprite-Based Navigation Bars
    - Creating Framed Pictures with CSS
    - Using a CSS Shading Effect to Decorate Images
    - Decorating Images with CSS







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