Centring tables using CSS

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
f1nutter
Forum Contributor
Posts: 125
Joined: Wed Jun 05, 2002 12:08 pm
Location: London

Centring tables using CSS

Post by f1nutter »

I suppose it's about time that all my pages meet HTML 4.01 standard, before learning XHTML or XML. I notice on W3C that the <center> tag and align attribute in <table> are depricated. I guess I need to do it using style sheets. So, how do I centre a table this way?

Thanks.
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

You can't, use

Code: Select all

<div align="center"></div>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

To do the same thing using CSS:

Code: Select all

<div style="text-align: center;">Insert table here</div>
or set up a class in your stylesheet

Code: Select all

.aligncenter &#123;text-align: center;&#125;
and use that

Code: Select all

<div class="aligncenter">Insert table here</div>
Never say never Takuma :wink:

Mac
CodeEye
Forum Commoner
Posts: 25
Joined: Fri Jul 05, 2002 7:19 am

Post by CodeEye »

your content is suposed to be separate from your style that is the reason why they removed the center tags
MattF
Forum Contributor
Posts: 225
Joined: Sun May 19, 2002 9:58 am
Location: Sussex, UK

Post by MattF »

This may be of some help:

Code: Select all

table &#123;
    align: center;
&#125;
But it will obvoisly just align all tables on the page.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

First of all in CSS it should be:

Code: Select all

text-align: center;
not

Code: Select all

align: center;
and secondly you can't align your tables like that, you need to put them in divs if you want to align them on the page.

Mac
MattF
Forum Contributor
Posts: 225
Joined: Sun May 19, 2002 9:58 am
Location: Sussex, UK

Post by MattF »

Thanks for staightening me out there, it's usally hit and miss for me and CSS but I thought I'd got that one right :?

I'll remember that in future :D
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Sorry was a bit short before (very tired) but CSS can be a bit tricky.

Mac
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Why do you think this doesn't work?

Code: Select all

<!-- TABLE STUFF -->
<td style="text-align: center;">Hello</td>
<!-- TABLE STUFF -->
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Um, that does work who said it didn't?

Mac
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post by Coco »

i never had any trouble with <table align=center>
but i am preferring to use <div> now because i can control everything :P
Post Reply