View Full Version : How to center a div ?

11-12-2010, 03:21 PM

I am a PHP coder and not very good at CSS. I wonder if there is away to center a div without using table or center tags ?:rolleyes:


Tingoc (http://www.tingoc.com)

Chris Hick
11-12-2010, 03:50 PM
Put this within your div tags..

margin: 0 auto;
width: #; /* replace the # with a proper value. */

11-12-2010, 03:51 PM
Hello bulldorc,
To center an element you need three things:

a valid DocType
an element with a width
that elements left/right margins set to auto

Here's a simple example:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
html, body {background: #FC6;}
#container {
height: 400px;
width: 800px;
margin: 30px auto;
background: #999;
<div id="container">
<!--end container--></div>

11-12-2010, 04:56 PM
Great. Thank you both for quick responses

Chris Hick
11-14-2010, 07:15 PM
Don't forget to placed this as resolved. :thumbsup:

edit opening post>go advanced>set the prefix before the title as resolved