Forum Moderators: not2easy

Message Too Old, No Replies

Centering in CSS

Is it possible to really center elements with CSS?

         

deMorte

10:21 am on Oct 12, 2007 (gmt 0)

10+ Year Member



I'm currently making a website with content generated with PHP from MySQL. The generated list is a table (I'm still working my way into CSS and in a hurry, I cant't afford to start remaking the old template).

My question is: Can I center the list table with CSS? I'm currently using CSS to "text-align" the content of the cells to the left and would like the whole table to be in the center of the content cell (yeah, the site template is a table too).

I've tried creating a div before the table with "text-align:center" but this does not work, the table doesn't center. Currently I'm using <div align="center>, but that doesn't feel right.

Marshall

10:28 am on Oct 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



deMorte, yes you can center with css.

The best thing is put the table in question in a <div> that way you will not affect any of the table's css. Next, the css needs to be:

#table_holder {
width: npx or nem or n% where n is the value /* THIS IS A MUST */
margin: 0 auto;
padding: 0;
text-align: left; /*IE fix */
}

Your HTML

<div id="table_holder">
Your table
</div>

This solution is cross browser friendly and you could assign the CSS to the table and eliminate the <div> if you really must.

Marshall