Home arrow Style Sheets arrow Page 3 - An Evened Margin Approach to Centering DIVs with CSS
STYLE SHEETS

An Evened Margin Approach to Centering DIVs with CSS


In this sixth installment of a seven-part series on centering DIVs with CSS, I’ll be discussing how to build a fixed centered web page design by using evened CSS margins. You may want to use this technique for browser compatibility reasons, as I explain in the article. Let's get started.

Author Info:
By: Alejandro Gervasio
Rating: 3 stars3 stars3 stars3 stars3 stars / 4
April 23, 2009
TABLE OF CONTENTS:
  1. · An Evened Margin Approach to Centering DIVs with CSS
  2. · Using evened CSS margins with a fixed design: defining a sample web document
  3. · Working with evened CSS margins
  4. · Putting CSS styles and (X)HTML markup together

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

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.


blog comments powered by Disqus
STYLE SHEETS ARTICLES

- CSS Combinators: Working with Child Combinat...
- CSS Combinators: Using General Siblings
- Intro to CSS Combinators
- CSS Semicircles and Web Page Headers
- Drawing Circular Shapes with CSS3 and Border...
- More CSS Pagination Link Templates
- CSS Pagination Links
- Animated CSS3 Image Gallery: Advanced Transi...
- CSS3 Animated Image Gallery: Transitions
- CSS3 Properties: Fixed Heights with box-sizi...
- CSS3 Properties: Altering Strokes and 3D Eff...
- CSS3 Properties: Text-Stroke
- CSS3 Transitions: Width and Height Properties
- Creating a Drop Down Menu in CSS3
- Intro to CSS Transitions

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials