Things You'll Need:
- A text editor or HTML editor
-
Step 1
To align text within a normal block of text such as a paragraph or a blockquote, use the text-align property.
-
Step 2
Left, right and center aligned textThe value choices for the test-align property are left, right, center and justify. Since justified text is not very readable on the Web, the illustration only shows left, right and center. In this illustration, each paragraph has a black border to demonstrate the size of the block element on the page if no set width is applied to the element.
-
Step 3
Normally an element is assigned to an id or class. The text alignment of any items in that id or class is set with a rule such as one of these:
#left {
text-align: left;
}
#right {
text-align: right;
}
#center {
text-align: center;
} -
Step 1
You may want to center an entire layout or a large division of a layout. In such cases you use a different CSS technique.
-
Step 2
You apply the CSS rule to the larger element selector you want centered. It might be the body element, a wrapper div or a container div.
-
Step 3
Centering by this method does not affect text alignment. The centered element will retain the default left alignment of text within the centered element.
-
Step 4
A centered layoutThe following is an illustration of an entire body element (with a black border for clarification purposes) centered on a page. A width must be set for the element, then the left and right margins are set to auto. Here's how:
body {
width: 80%;
margin-right: auto;
margin-left: auto;
border: 1px solid black;
}













Comments
dellfalconer said
on 10/2/2009 Thanks. Found what I wanted to do quickly and easily after reading this article.
Veesites said
on 2/17/2009 Balbert, The fule margin:0px auto; sets the top and bottom margin to 0 and the left and right to auto. It is exactly what I was talking about.
balbert said
on 2/17/2009 For centering the entire page, is there any difference in using margin:0px auto; vs setting margin-left and margin-right to auto?For centering a page it seems most people wrap the entire "page" in a container div and then set that container div to margin:0px auto;