Horizontally Center Your Website Structure Using CSS
Every web programmers knows that you can't code a commercial website for a specific browsers or user experience level, much less a screen resolution. Since you can't count on a user having the latest and greatest, you need to use the lowest common denominator of browsers resolution. In today's web world, 1024x768 is usually the lowest browser resolution coded for.
I prefer to create my websites using a fixed width so that I can have complete control over the website layout. A fixed width website looks the same in all browsers which can prevent users with extremely large or small resolutions from experiencing layout issues. Instead of simply allowing the website to align completely left on the screen (resulting in high-resolution users seeing your website take up only half their screen), I center the site. This is a very simple task.
Step One: XHTML
Declare a DOCTYPE. Create an initial "wrap" DIV that will be the website's wrapper.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>My Site</title>
</head>
<body>
<!-- WEBSITE GOES HERE --> </body> </html>
Step Two: CSS
Declare the "wrap" id — you MUST declare a width (otherwise, how would you center it?) Use left and right margins of "auto."
#wrap { width:900px; margin:0 auto; } /* "auto" makes the left and right margins center the wrapper */That's It?
Yes! Centering your website is that easy. This website currently uses this layout. Does my website take up most of your screen? That means you have a low browser resolution.
- Login or register to post comments
- 2807 reads
- Printer-friendly version
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)










Comments
Chris Coyier replied on Tue, 2008/01/22 - 1:28pm