Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Horizontally Center Your Website Structure Using CSS

DZone's Guide to

Horizontally Center Your Website Structure Using CSS

· Web Dev Zone
Free Resource

Prove impact and reduce risk when rolling out new features. Optimizely Full Stack helps you experiment in any application.

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.

With SDKs for all major client and server side platforms, you can experiment on any platform with Optimizely Full Stack.

Topics:

Published at DZone with permission of David Walsh. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}