Become a webdsign master


How to Centre a DIV Block Using CSS (www.imagination.gs)

(
The technique works because when both margins
This article discusses how to centre (orare set to auto, web browsers are required by
"center" in US English) a DIV block, whetherthe  CSS  standard  to give them equal width.
it contains text, graphics, or a mixture of
both, using standards-compliant CascadingFor example, if you want your div block to
Style Sheets (CSS). The technique is usefulhave a width of 700 pixels, the following
not just for creating pleasing designs, butcode  will  centre  the  block.
also for times when you want to create a
fixed width single column layout with the#content {width: 700px ;margin-left: auto
main content placed in the centre of the;margin-right:  auto  ;
page,  and  not  flushed  to  one  side.
}
Assumptions
Incidentally, the width doesn't need to be in
I will assume in this article that you havepixels. Other units like em or percentage
some basic knowledge of how to write HTML andwill  work  just  as  well.
CSS.
Demo
If you don't even have a website yet, and
have arrived at this article looking for tipsAt the time this article was written, you can
on designing one, please start with mysee an example of this code in action on the
article on How to Start/Create Your OwnFeedback Form Wizard Demo Site. The main body
Website  instead.of that site has a width of 730 pixels, and
is centred using the method given here, so
Steps to Centering a DIV Block withoutthat it appears in the middle of the browser
Centering  the  Text  Insidewindow.
Let's assume that you have a div block as(Note: if your browser window is opened too
follows:small, or your monitor has too low a
resolution, you will of course not be able to
This is a DIV block that is to be centred. Isee  the  effect.)
don'twant the text to be centred, though,
just  the  block.Browser  Support
At first glance, you may think that theThe code above has been tested with IE 6, 7,
following CSS rule will work.text-align:Firefox,  Opera  and  Safari.
center  ;
Note that this article only deals with the
However, the rule centres the text inside thecentering of a DIV block using CSS. If you
box, rather than the block itself. Whilewant to center a table, you will have to work
there are times when you may want to do this,around bugs in IE 6 and 7. In such a case,
our intention here is to centre the entireplease see my article How to Centre a Table
block but leave the internal contentsUsing CSS in Nvu and KompoZer instead.
formatted as it was before. As such, theAlthough that article is directed at Nvu and
above  code  is  not  what  we  want.KompoZer users, the code itself is given in
CSS and HTML so you should have no trouble
Instead, the standard trick to centering afollowing.
block  in  CSS  is  to  do  the  following:
Conclusion
Specify  a  width  for  the  DIV  block.
The method above is the standard technique
Set  both its left and right margins to auto.for centering a DIV block using
standards-compliant CSS. With this, you can
Both steps are necessary -- that is, youcentre your blocks without using deprecated
cannot just set the margins to auto withoutHTML tags like . And the code even works in
specifying the width of the block and expectbuggy browsers like IE.by Christopher Heng.
this  method  to  work.



1 A B C D E 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128