Home arrow Style Sheets arrow Page 3 - Using the Text-align CSS Property to Center DIVs
STYLE SHEETS

Using the Text-align CSS Property to Center DIVs


Building fully-centered web page layouts with CSS can be more challenging than it appears at first glance. In this seven-part article series, we walk you through a variety of techniques that reduce the hassles you may face in achieving these layouts. In this second part of the series, we'll focus on a new way to use the text-align property.

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
March 26, 2009
TABLE OF CONTENTS:
  1. · Using the Text-align CSS Property to Center DIVs
  2. · Creating a centered three-column web page layout: the structural markup
  3. · Styling the (X)HTML file: constructing a fully-centered web page layout
  4. · Merging the CSS styles and structural markup in one single file

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Using the Text-align CSS Property to Center DIVs - Styling the (X)HTML file: constructing a fully-centered web page layout
(Page 3 of 4 )

In the section that you just read, I created a primitive (X)HTML file, which was made up of the typical header and footer sections, as well as three main columns. Having at our disposal such a useful markup, it’s time to begin coding the set of CSS styles that will construct a fully-centered web page layout.

That being said, please take a look at the following CSS declarations, which take advantage of the functionality of the “text-align” property to create this type of page design. Here they are:


body{

text-align: center;

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{

text-align: left;

margin-left: 20%;

margin-right: 20%;

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;

} 


Asides from styling the main columns of the previous (X)HTML file as well as the header and sections, you should pay close attention to the styles tied to the “body” selector and the “wrapper” container, since these two elements are actually key to properly centering the remaining DIVs.

On one hand, the body element is responsible for centering all of the main columns, while on the other hand, the general wrapping DIV is charged with resetting this alignment for all of the other containers. In both cases, it’s clear to see how useful the “text-align” property can be, particularly when it comes to building this kind of web page layout.

All in all, at this point I've demonstrated how to construct a fully-centered elastic page design, which is composed of three principal columns. So what's next? Well, in truth the previous CSS styles look rather disarticulated when analyzed separately. Thus, in the last section of this article I’ll be attaching them to the prior structural markup, in this manner completing the development of this web page layout.

However, to see how this process will be accomplished, you’ll have to read the next segment. It’s only one click away.


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 10 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials