Current location: Hot Scripts Forums » General Web Coding » CSS » How do I center an ID'd table using CSS?


How do I center an ID'd table using CSS?

Reply
  #1 (permalink)  
Old 10-19-05, 02:43 PM
likegluelikecrew likegluelikecrew is offline
New Member
 
Join Date: Oct 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
How do I center an ID'd table using CSS?

I'm using Dreamweaver to create my CSS and I didn't see an align option in the dialog box. If I had a table named #tabledefault for example, what would be the coding to get it centered and a width of 90%?

Also, I noticed when I applied the cell padding to my table that I ID'd as tabledefault, the table that I had nested within it also got the cell padding applied to it, which I don't want. How do I get around that? Thanks big time!

We Are 138

Last edited by likegluelikecrew; 10-19-05 at 02:48 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 10-26-05, 04:46 AM
rimian rimian is offline
Newbie Coder
 
Join Date: Jun 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
you have to nest your table inside an element that is centred or text-align the <body> of your document to centre. this only works in ie as text-align wont align anything but text in firefox.

try something like:
<div style="text-align: center;">
<table style="width: 90%;">
<tr>
<td>hello world</td>
</tr>
</table>
</div>

hope that helps
__________________
Web Developer
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 10-27-05, 02:35 PM
rid rid is offline
Newbie Coder
 
Join Date: Oct 2005
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
i think you only need:

Code:

  <table align=center width=90% bgcolor=orange>
    <tr>
      <td>This is my table</td>
    </tr>
    </table>

Quote:
Originally Posted by rimian
you have to nest your table inside an element that is centred or text-align the <body> of your document to centre. this only works in ie as text-align wont align anything but text in firefox.

try something like:
<div style="text-align: center;">
<table style="width: 90%;">
<tr>
<td>hello world</td>
</tr>
</table>
</div>

hope that helps
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 04-28-09, 02:14 AM
car-zone car-zone is offline
New Member
 
Join Date: Apr 2009
Location: USA
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
How do I center an ID'd table using CSS?

This is really just a reminder for myself. When using the DOM to dynamically hide and show table elements (like a table row), you can't use display = "block" to bring a table row back to life in Firefox; although, it works okay in IE. It's best to simply use display = "" so that Firefox correctly resets it to table-row and IE can still do what it does.I imagine other browsers behave similarly to Firefox.

Last edited by car-zone; 04-28-09 at 02:18 AM. Reason: Signature
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 04-30-09, 05:57 AM
harish harish is offline
Newbie Coder
 
Join Date: Apr 2009
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
Method 1
To center a table, you need to set the margins, like this:

Code:
  table.center {margin-left:auto; margin-right:auto;}
And then do this:
Code:
  <table class="center">
    ...
  </table>
At this point, Mozilla and Opera will center your table. Internet Explorer 5.5 and up, however, needs you to add this to your CSS as well:
Code:
  body {text-align:center;}
Method 2

If you want your table to be a certain percentage width, you can do this:
Code:
  table#table1 {width:70%; margin-left:15%; margin-right:15%;}
And then in your HTML/XHTML, you would do this:
Code:
  <table id="table1">
    ...
  </table>
Note that I was using an id to describe the table. You can only use an id once on a page. If you had many tables on a page that you wanted to be the same width and centered, you would do this in your CSS:
Code:
  table.center {width:70%; margin-left:15%; margin-right:15%;}
And this in your HTML:
Code:
  <table class="center">
    ...
  </table>

  <table class="center">
    ...
  </table>
Method 3

If you want your table to be of fixed width, define your CSS like this:
Code:
  div.container {width:98%; margin:1%;}
  table#table1 {text-align:center; margin-left:auto; margin-right:auto; width:100px;}
  tr,td {text-align:left;}
Set "width:100px" to whatever width you need.

"text-align: center" is there for Internet Explorer, which won't work without it. Unfortunately, "text-align: center" will center all the text inside your table cells, but we counter that by setting "tr" and "td" to align left.

In your HTML, you would then do this:
Code:
  <div class="container">
    <table id="table1">
      ...
    </table>
  </div>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
CSS - Center Body dinalsi CSS 2 10-19-05 02:15 PM
Table Through CSS xtreme231 CSS 1 08-11-05 02:26 PM
Problem with a sort table js function tdubyou JavaScript 0 05-03-04 10:19 AM
auto table resize derick_2k JavaScript 4 04-26-04 03:32 PM
Newbie MySQL fccolon PHP 2 03-16-04 11:54 AM


All times are GMT -5. The time now is 08:58 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.