If you want to get VERTICAL centering , I would suggest using a table inside a DIV (as suggested by Cletus above other methods, it can be tedious).
div.centered table {margin: 0 auto;} /* no width needs to be specified for table */ div.centered table td {vertical-align: middle;} /* replace this with vertical-align: top; to disable vertical centering */ <div class="centered" style="border: 1px solid #cccccc;"> <table style="border: 1px solid #ff0000;"> <tbody> <tr><td> Some text here </td></tr> </tbody> </table> </div>
If you are only interested in HORIZONTAL centering (without vertical), you can use only DIV:
div.centered div {margin: 0 auto; width: 300px;} /* some width MUST be specified to center a DIV. */ <div class="centered" style="border: 1px solid #cccccc;"> <div style="border: 1px solid #ff0000;"> Some text here </div> </div>
As you may have noticed, in order to horizontally align the DIV inside the DIV, you also need to specify a fixed width for the internal DIV . It may be somehow not what you want to do, so it would be easier to always use the first solution (one with TABLE) and just remove "vertical-align: middle;" when you want to get only horizontal centering.
I checked this using the document:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
in IE7, FF 3.6, SAFARI 4.0.4
source share