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

Published: 17th August 2008
Views: N/A

(www.imagination.gs)

This article discusses how to centre (or "center" in US English) a DIV block, whether it contains text, graphics, or a mixture of both, using standards-compliant Cascading Style Sheets (CSS). The technique is useful not just for creating pleasing designs, but also for times when you want to create a fixed width single column layout with the main content placed in the centre of the page, and not flushed to one side.



Assumptions

I will assume in this article that you have some basic knowledge of how to write HTML and CSS.



If you don't even have a website yet, and have arrived at this article looking for tips on designing one, please start with my article on How to Start/Create Your Own Website instead.



Steps to Centering a DIV Block without Centering the Text Inside

Let's assume that you have a div block as follows:





This is a DIV block that is to be centred. I don't

want the text to be centred, though, just the block.



At first glance, you may think that the following CSS rule will work.



text-align: center ;

However, the rule centres the text inside the box, rather than the block itself. While there are times when you may want to do this, our intention here is to centre the entire block but leave the internal contents formatted as it was before. As such, the above code is not what we want.



Instead, the standard trick to centering a block in CSS is to do the following:



Specify a width for the DIV block.

Set both its left and right margins to auto.

Both steps are necessary -- that is, you cannot just set the margins to auto without specifying the width of the block and expect this method to work.



The technique works because when both margins are set to auto, web browsers are required by the CSS standard to give them equal width.



For example, if you want your div block to have a width of 700 pixels, the following code will centre the block.



#content {

width: 700px ;

margin-left: auto ;

margin-right: auto ;

}

Incidentally, the width doesn't need to be in pixels. Other units like em or percentage will work just as well.



Demo

At the time this article was written, you can see an example of this code in action on the Feedback Form Wizard Demo Site. The main body of that site has a width of 730 pixels, and is centred using the method given here, so that it appears in the middle of the browser window.



(Note: if your browser window is opened too small, or your monitor has too low a resolution, you will of course not be able to see the effect.)



Browser Support

The code above has been tested with IE 6, 7, Firefox, Opera and Safari.



Note that this article only deals with the centering of a DIV block using CSS. If you want to center a table, you will have to work around bugs in IE 6 and 7. In such a case, please see my article How to Centre a Table Using CSS in Nvu and KompoZer instead. Although that article is directed at Nvu and KompoZer users, the code itself is given in CSS and HTML so you should have no trouble following.



Conclusion

The method above is the standard technique for centering a DIV block using standards-compliant CSS. With this, you can centre your blocks without using deprecated HTML tags like
. And the code even works in buggy browsers like IE.

by Christopher Heng.

(www.imagination.gs)

Report this article Ask About This Article


Loading...
More to Explore