TechRepublic : A ZDNet Tech Community

How Do I...

Host: Mark Kaelin
Contact

How do I... Center a Web page layout using CSS?

The most common LCD monitors on the market today follow the wide-screen format. That wide ratio can play havoc with Web site designers and their layout of Web pages. However, with a little bit of Cascading Style Sheet (CSS) code, you can control your Web page design by centering it on any screen despite its horizontal to vertical ratio.

This blog post is also available in PDF format as a TechRepublic download, which includes the sample code.

The situation

The next time you step into the local Best Buy store, take a look at their selection of computer monitors. You’ll notice that as the screen ratio of monitors has shifted towards a more movie-theater like scale (rectangular) and away from a television ratio of 4:3 (square), the bigger a monitor gets, the wider it gets.

That’s great for watching movies, playing games or running palette-heavy programs such as Photoshop, but what about surfing the Web? Long lines of text are hard and tiring on the eye, which is why you see so many Web sites that center their layouts. This allows the authors to set a fixed width for their content and present it in a dynamic fashion as the user’s browser window expands and contracts in width.

You could, of course, create a fixed-width table and center it on the page, but today’s Web designers and developers are trying to move away from tables altogether and instead control layout entirely with CSS markup. The small bit of CSS code that I am about to show you will get you started with using CSS to create your Web page layouts.

Open your preferred HTML editor and create a new page. Enter the code below (Listing A) to get started.

Listing A

<html>

<head>

<title>Centering a page layout with CSS</title>

</head>

<body>

</body>

</html>

Let’s go ahead and place some formatted text in the Body of the HTML document (Listing B), so we can see the effects of our CSS code as we build it.

Listing B

<html>

<head>

<title>Centering a page layout with CSS</title>

</head>

<body>

<h1>The Gettysburg Address</h1>

<h2>Gettysburg, Pennsylvania<br>

November 19, 1863</h2>

Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure.

</body>

</html>

Save the HTML document, and then preview it in your browser. Play with the width of the browser window to see how the copy expands and shrinks to match the size of the viewing area.

Let’s start building the CSS code and tame all of that copy. Let’s keep this simple and create your CSS code in the head of this document (Listing C) instead of linking to an external style sheet. Place it right immediately after the closing </TITLE> tag.

Listing C

<html>

<head>

<title>Centering a page layout with CSS</title>

<style type="text/css">

<!--

-->

</style>

</head>

<body>

<h1>The Gettysburg Address</h1>

<h2>Gettysburg, Pennsylvania<br>

November 19, 1863</h2>

Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure.

</body>

</html>

Most Web Designers, when declaring a CSS class that is going to be used as a centering element, give the class the name “wrapper” or “container.” I prefer “wrapper,” so that’s what we’ll use here (Listing D).

Listing D

<html>

<head>

<title>Centering a page layout with CSS</title>

<style type="text/css">

<!-

#wrapper 

-->

</style>

</head>

<body>

<h1>The Gettysburg Address</h1>

<h2>Gettysburg, Pennsylvania<br>

November 19, 1863</h2>

Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure.

</body>

</html>

Next, we’ll set the width of the wrapper to 720 pixels (Listing E), so that it will sit nicely on an 800 x 600 monitor. Of course, you could make the width as long or as short as you like to accommodate your design.

Listing E

<html>

<head>

<title>Centering a page layout with CSS</title>

<style type="text/css">

<!-

#wrapper {

      width: 720px;

-->

</style>

</head>

<body>

<h1>The Gettysburg Address</h1>

<h2>Gettysburg, Pennsylvania<br>

November 19, 1863</h2>

Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure.

</body>

</html>

That sets the width of our content, but how do we center it? Simple — the margin attribute shown in Listing F, with a setting of zero (0) and automatic alignment, places all of our content in the center of the screen:

Listing F

<html>

<head>

<title>Centering a page layout with CSS</title>

<style type="text/css">

<!-

#wrapper {

      width: 720px;

      margin: 0 auto;

}

-->

</style>

</head>

<body>

<h1>The Gettysburg Address</h1>

<h2>Gettysburg, Pennsylvania<br>

November 19, 1863</h2>

Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure.

</body>

</html>

Believe it or not, that small amount of code does the job… in most modern browsers. Older versions of IE and Netscape, however, have issues when complying with this CSS markup, and will center-align the text, when what we really want is the text to be left-aligned, but stay put in the center of the window. Don’t worry though; there is a simple fix for these outdated browsers.

Start by defining a text-align attribute for the <BODY> tag of the HTML document, as shown in Listing G. You must place it before the wrapper style, or this will not work, due to the Cascading nature of Cascading Style Sheets!

Listing G

<html>

<head>

<title>Centering a page layout with CSS</title>

<style type="text/css">

<!--

body {

text-align: center;

}

#wrapper {

            width: 720px;

            margin: 0 auto;

}

-->

</style>

</head>

<body>

<h1>The Gettysburg Address</h1>

      <h2>Gettysburg, Pennsylvania<br>

      November 19, 1863</h2>

Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure.

</body>

</html>

Then add the text-align attribute below to the wrapper class and give it a value of left as in Listing H.

Listing H

<html>

<head>

<title>Centering a page layout with CSS</title>

<style type="text/css">

<!-

body {

text-align: center;

}

#wrapper {

      width: 720px;

      margin: 0 auto;

      text-align: left;

}

-->

</style>

</head>

<body>

<h1>The Gettysburg Address</h1>

<h2>Gettysburg, Pennsylvania<br>

November 19, 1863</h2>

Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure.

</body>

</html>

There is one more line of code that needs to be written before we can test our work. Our wrapper will break in Netscape 6 if the browser window is shorter than the width of the wrapper. A min-width attribute (Listing I) in the body style will take care of that.

Listing I

<html>

<head>

<title>Centering a page layout with CSS</title>

<style type="text/css">

<!-

body {

text-align: center;

min-width: 760px;

}

#wrapper {

      width: 720px;

      margin: 0 auto;

      text-align: left;

}

-->

</style>

</head>

<body>

<h1>The Gettysburg Address</h1>

<h2>Gettysburg, Pennsylvania<br>

November 19, 1863</h2>

Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure.

</body>

</html>

Finally, apply the wrapper to the content of the HTML document by placing a <DIV> tag around all of the Gettysburg Address copy as shown in Listing J.

Listing J

<html>

<head>

<title>Centering a page layout with CSS</title>

<style type="text/css">

<!--

body {

text-align: center;

min-width: 760px;

}

#wrapper {

            width: 720px;

            margin: 0 auto;

            text-align: left;

}

-->

</style>

</head>

<body>

<div id="wrapper">

<h1>The Gettysburg Address</h1>

      <h2>Gettysburg, Pennsylvania<br>

      November 19, 1863</h2>

Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure.

</div>

</body>

</html>

Preview the page in several browsers to see how the left-justified text stays put in the center of the browser no matter how small or narrow the browser window is.

Although we used no tables in this example, you could place whatever content you wish within the <DIV> tag — tables included — and the CSS structure would keep your layout in place in the center.

John Lee is a consultant specializing in design and illustration and a freelance technical writer. You can visit his Web site at johnleestudio.com.

Print/View all Posts Comments on this blog

Using a centered div instead donniel@... | 01/03/08
Centered divs umstot.david@... | 01/03/08
hey David, Jaqui | 01/03/08
Solves issues? calculon102@... | 01/03/08
that's odd Jaqui | 01/03/08
Does not work in FF 2.0.0.11, but works in IE6 w.lievers | 01/03/08
Working in FF Rob Howard | 01/03/08
Only worked at listing J jschlesinger@... | 01/07/08
Until you define the wrapper, the code is impotent Sereniti | 01/09/08
in addition to David's reply above Jaqui | 01/03/08
Still wouldn't be valid. umstot.david@... | 01/10/08
Interesting Discussion - so what happens when ... ? Amnezia | 01/04/08
SSDS Tony Hopkinson | 01/04/08
Depends on the use and support techr@... | 01/04/08
Totally Agree .. Snak | 01/07/08
Align attribute aligns contents, not the tag itself. Sereniti | 01/09/08
just one little bone to pick.. Jaqui | 01/03/08
Unless you want it that way tdh2112 | 01/03/08
partly right Jaqui | 01/03/08
I think that there is a difference in whitespace thoughts techr@... | 01/03/08
ahh, there's the difference Jaqui | 01/03/08
Be more aware? Izzmo | 01/03/08
ahh, there it is. techr@... | 01/03/08
You should bear in mind TR's site design like all others is Tony Hopkinson | 01/04/08
Tony, thanks for the insight techr@... | 01/04/08
Then design it to take up only one screen Sereniti | 01/09/08
A different bone Snak | 01/07/08
A funny bone Tony Hopkinson | 01/07/08
Use both deity_chooch | 01/07/08
Thank you Snak | 01/08/08
Pre tag did help, though Sereniti | 01/09/08
RE: How do I... Center a Web page layout aravindajith@... | 01/03/08
Rude comment techr@... | 01/03/08
Actually that was quite polite. Tony Hopkinson | 01/04/08
Your right Tony techr@... | 01/04/08
It was all new to me! not_IT_John | 01/03/08
Well said, John KarrasB | 01/03/08
So? Sereniti | 01/09/08
RE: How do I... Center a Web page layout billmez | 01/03/08
But... why? deity_chooch | 01/03/08
That's the way! foard | 01/03/08
Because... billmez | 01/04/08
RE: How do I... Center a Web page layout brianchamps | 01/03/08
I still use Frontpage :) crose@... | 01/03/08
Code cowboys deity_chooch | 01/03/08
Some of us are very accurate at shooting from the hip Tony Hopkinson | 01/04/08
Frontpage techr@... | 01/03/08
Non-standard code bob@... | 01/03/08
Your Highness ;) crose@... | 01/04/08
Style body tag instead of using div? mdimis | 01/03/08
I agree jozhall@... | 01/03/08
This doesn't seem to work in IE7 M.W.H. | 12/30/08
Yes, avoid Divitis Underground_In_TN | 01/03/08
yup you are missing something Jaqui | 01/03/08
HTML dead? blissb | 01/03/08
that would happen faster Jaqui | 01/03/08
Preach the word to the unbelievers Underground_In_TN | 01/03/08
XHTML doc types Sereniti | 01/09/08
You read it correctly deity_chooch | 01/09/08
use newer standard klewis@... | 01/03/08
that's the point Jaqui | 01/03/08
huh? that's not really the question jmelnik | 01/03/08
Um...Jacqui...what is wrong with html? KarrasB | 01/03/08
in a nutshell.... jmelnik | 01/04/08
Does it work though? Tony Hopkinson | 01/04/08
From the horses mouth techr@... | 01/04/08
Yes and no deity_chooch | 01/03/08
RE: How do I... Center a Web page layout brian.didlake@... | 01/03/08
RE: How do I... Center a Web page layout SamirBeMad | 01/03/08
deprecated jmelnik | 01/03/08
Nice...Thanks (nt) SamirBeMad | 01/04/08
IMHO - HTML still works because: Sereniti | 01/09/08
back to print size again longwayoff | 01/03/08
Good point, longway KarrasB | 01/03/08
So what should a novice learn, XHTML or HTML? kathycookkc@... | 01/03/08
I always tell those learning something new... jmelnik | 01/04/08
Both. Tony Hopkinson | 01/04/08
Thanks for saying it deity_chooch | 01/04/08
Thanks! kathycookkc@... | 01/08/08
A Happy New Year ! I hope a the pragmatic answer dan_somnea@... | 01/07/08
Page not found Sereniti | 01/09/08
RE: How do I... Center a Web page layout ameermoadi@... | 01/04/08
RE: How do I... Center a Web page layout r.rastin@... | 01/05/08
Afterword from the Author studio.johnlee@... | 01/07/08
Since when was "id=" used with CSS classes? si@... | 01/18/08
Thanks leonttibsian@... | 01/31/09
RE: How do I... Center a Web page layout jdaniel59868 | 05/01/08
RE: How do I... Center a Web page layout katstuff | 06/17/09
RE: How do I... Center a Web page layout Hazlet | 11/05/09

What do you think?

White Papers, Webcasts, and Downloads

Recent Entries

TR on Twitter

Top Rated

    Archives

    TechRepublic Blogs



    Administrator's Guide to TCP/IP, Second Edition
    Maintain your critical TCP/IP system and ensure reliable, safe remote access. Get the expert advice and solutions to handle Windows networking, Cisco routing, documentation, and troubleshooting.
    Buy Now
    Quick Reference: Linux Commands
    Reduce stress and speed up resolutions with the easiest command references right at your fingertips. You'll receive a PDF file covering Linux, packed with the most common commands you'll need and use daily.
    Buy Now

    SmartPlanet

    Click Here