Over a million developers have joined DZone.

Horizontally Center Your Website Structure Using CSS

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

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.

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

Topics:

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

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}