Jump Menu : Content | Explore | Comments | Search | Home | Sitemap | Contact | Login | Access.

Absolutely Centered Layout

Absolute Centering with CSS & (X)HTML

Designing an absolutely centered layout involves centering a division both horizontally and vertically. When this is done, the centered division (or other element) is centered according to the browser window. To accomplish this, use this (X)HTML:

<div id="wrapper">
 <div id="center">
 </div>
</div>

And employ this CSS (commented with explanations):

body {
	background-color: #333; /* cosmetic */
	margin: 0px; /* required */
}
div#wrapper {
	background-color: red; /* cosmetic */
	height: 0px; /* set to taste */

	/* required */
	position: absolute;
	overflow: visible;
	display: block;
	width: 100%;
	left: 0px;
	top: 50%;
}
div#center {
	background-color: #666; /* cosmetic */
	border: 3px solid #FFF; /* cosmetic */

	overflow: auto; /* set to taste */

	position: absolute; /* required */
	left: 50%; /* required */

	margin-left: -200px; /* half of width */
	width: 400px; /* width of div */

	height: 300px; /* height of div */
	top: -150px; /* half of height */
}

Taken together and used properly, these two blocks of code will produce an (X)HTML layout with an absolute center that is centered both vertically and horizontally.

Update: Thanks to a comment left by Max, we now explain an alternate method of horizontally and vertically centering layouts via CSS. The alternate method utilizes floated divs to solve the disappearing scrollbar issue.

Absolute Centering with Frames

This method centers content via frames:

<frameset cols="*,777,*">
 <frame src="http://example.tld/empty.html">

 <frameset rows="*,333,*>
  <frame src="http://example.tld/empty.html">
  <frame src="http://example.tld/centered-content.html">
  <frame src="http://example.tld/empty.html">
 </frameset>

 <frame src="http://example.tld/empty.html">
</frameset>

Absolute Centering with Tables

To center content within any (X)HTML document, try this:

<table width="100%" height="100%" align="center" cellspacing="0" cellpadding="0" border="0">
 <tr>
  <td align="center" valign="middle">

   <!-- content goes here -->

  </td>
 </tr>
</table>

New & Improved Absolute Centering with Tables

Absolute centering in Firefox and other standards-compliant browsers is not as difficult as it may seem. This next method is 100% valid (X)HTML/CSS and works with any !DOCTYPE (i.e., loose, transitional, or strict) on virtually any modern browser:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html><head><title>Absolutely Centered Table Layout</title>
<style type="text/css">
html, body { 
	height: 100%;
	padding: 0;
	margin: 0;
}
table { 
	text-align: center;
	height: 100%;
	width: 100%; 
	border: 0;
}
</style>
</head>
<body>
 <table>
  <tr>
   <td><!-- [ content goes here ] --></td>
  </tr>
 </table>
</body>
</html>

Absolutely Centered Page via Tables

This method may not appeal to the (X)HTML purist, but if you need a cross-browser method of centering an entire page, this code will most certainly do the trick. The idea here is to wrap the entire web document within an absolutely centered table. Thus, the first three lines must be placed before all page content, while the last three lines must be placed after all page content.

<table border="0" cellpadding="0" cellspacing="0" width="100%" height="100%">
 <tr>
  <td align="center" valign="middle">

   <!-- Place the entire web document here for absolute centering -->

  </td>
 </tr>
</table>

Related articles

About this article

This is article #203, posted by Perishable on Monday, September 18, 2006 @ 08:14am. Categorized as Presentation, Structure, and tagged with css, html, layout, markup, xhtml. Updated on May 05, 2008. Visited 25699 times. 19 Responses »

BookmarkTrackbackCommentSubscribeExplore

« IE Scrollbar Colors • Up • Customize WordPress Quicktags »


19 Responses

1 • December 7, 2006 at 5:30 am — Sulman Bhatti says:

Your solution “With Tables” worked perfectly well. Thank you so much for saving me hours of tiresome searching and endless trial and error.

2 • December 9, 2006 at 7:10 pm — Perishable says:

Happy to help — I am glad it worked well for you!

3 • March 22, 2007 at 9:43 am — quake says:

The thing with tables will never work in Firefox if you have proper DOCTYPE set.

However, I what to do about it (check my website).

4 • March 25, 2007 at 3:49 pm — Perishable says:

quake,
Thanks for the tip! I snooped around your site (including source code) for awhile and did not find anything concerning absolute centering via tables. So, we went ahead and provided our own solution. Please refer to the above article for a standards-compliant method of centering content absolutely via a table-based layout.
Cheers,
Jeff

5 • September 7, 2007 at 7:49 am — 15PK says:

thanks my friend,
you saved me with your centering with CSS

6 • September 8, 2007 at 1:04 pm — Perishable says:

Glad to be of service!

7 • September 18, 2007 at 5:36 am — max says:

hi .. the div solution works well, only one problem maybe somebody can help:

if you make the browser window smaller then the div is -> the scrollbar doenst fit right und you cant scroll over the whole area …

example here:

http://www.berghindjoseph.com/testweb/home.html

maybe some body knows a solution for this?

thank you and greetings, max

8 • September 18, 2007 at 6:30 am — Max says:

solution there … :o)

http://d-graff.de/fricca/center.html

9 • September 18, 2007 at 9:20 am — Perishable says:

Thank you for pointing this out, Max. I will look over the differences between the two solutions, post on the results, and possibly update this post as well. Also, I coded the /testweb/ link in your first comment because its target seems to have been removed. Let me know if this is not the case and I will restore the link. Otherwise, thanks for sharing your findings with us!

10 • September 18, 2007 at 1:31 pm — max says:

sorry i’m in a hurry …

the link:

http://www.berghindjoseph.com/testweb/home3.html

cheerz, max *

11 • September 18, 2007 at 9:29 pm — Perishable says:

No sweat — Thank you kindly!

12 • November 12, 2007 at 12:15 pm — [IGU]Goddess says:

Thank you ever so much for this solution!! It works like a charm for me, just what i needed!

13 • November 12, 2007 at 5:03 pm — Perishable says:

You are very welcome! ;)

14 • November 15, 2007 at 1:59 pm — Joel Nylund says:

Hi, im not clear if this works in the situation where you have a page that scrolls vertically, will it center the div on the visible portion of the page?

15 • November 18, 2007 at 8:58 am — Perishable says:

Joel,

If I remember correctly, this method will center content according to the width and height of the web page as displayed in compliant browsers. So, if your page is 50 screens high, the content will be centered according to the entire height (i.e., 50 screens). If you think about this for a moment, it makes sense because it would be impossible to center 50 screens of content within the visible portion of the browser.

16 • February 14, 2008 at 2:12 am — Ryan Monaghan says:

I could kiss you. I’ve been tirelessly searching through dreamweaver and css manuals trying to figure out how to do this. I knew it could be done, though many said it couldn’t. Thank you again, SO SO MUCH!

17 • February 17, 2008 at 8:52 am — Perishable says:

You are most welcome, Ryan — happy to help! ;)

18 • February 22, 2008 at 12:51 pm — BluDiE says:

Thanks a bunch, working on a couple sites right now, and i couldn’t get them to center for the life of me. QUITE THE LIFESAVER!

19 • February 26, 2008 at 4:58 pm — Perishable says:

My pleasure, BluDiE — thanks for the feedback! ;)

Drop a comment


Set CSS to lite theme
Set CSS to dark theme