Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > HTML > how to align text in a <td>?

Reply
Thread Tools

how to align text in a <td>?

 
 
John Salerno
Guest
Posts: n/a
 
      06-01-2006
Ridiculously simple, but I can't figure it out. How do you make the text
in <td> elements align to the right of the table cell?

I'm looking at CSS properties for positioning but I just don't see
anything. Vertical-align helped me get it to the bottom, but now I need
it right-aligned.

Thanks.
 
Reply With Quote
 
 
 
 
David Dorward
Guest
Posts: n/a
 
      06-01-2006
John Salerno wrote:

> Ridiculously simple, but I can't figure it out. How do you make the text
> in <td> elements align to the right of the table cell?


Same as any other element, with the CSS text-align property.

--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Home is where the ~/.bashrc is
 
Reply With Quote
 
 
 
 
John Salerno
Guest
Posts: n/a
 
      06-01-2006
David Dorward wrote:
> John Salerno wrote:
>
>> Ridiculously simple, but I can't figure it out. How do you make the text
>> in <td> elements align to the right of the table cell?

>
> Same as any other element, with the CSS text-align property.
>


Shucks, I was looking in the wrong place, I guess.

Thanks for the response!

Just a quick followup: Is it good practice to make a separate class just
to accomplish this alignment, if you don't want all <td>s to be
right-aligned? I did class="textlabel" and then a td.textlabel property.
It doesn't seem very elegant though, for some reason.

Thanks.
 
Reply With Quote
 
Alan J. Flavell
Guest
Posts: n/a
 
      06-01-2006
On Thu, 1 Jun 2006, John Salerno wrote:

> David Dorward wrote:
> > Same as any other element, with the CSS text-align property.

....
> Just a quick followup: Is it good practice to make a separate class
> just to accomplish this alignment,


Well, the td align="right" attribute isn't exactly deprecated yet
(unlike the align attribute on many other elements), but I'd agree
with D.D that it's more hygienic to deal with this via the stylesheet.

> if you don't want all <td>s to be right-aligned?


It may be that you'd want to hang other presentational proposals off
these particular cells - if not now, then maybe later - and defining
an appropriate selector is the way to get set up for that. If you see
what I'm getting at.

> I did class="textlabel" and then a td.textlabel property.


Congratulations on not doing the obvious (but inappropriate)
' class="right" ' which so many would do at this point!

> It doesn't seem very elegant though, for some reason.


Agreed. But it *is* the safest way to get wide acceptability
across browsers and versions.

If you want to be clever, you can style table columns without having
to use classes, by coding selectors like td+td+td, which works fine
for browsers which implement CSS2; but back-level browsers like IE
aren't quite there yet, their newest version is still in beta, and
there will be plenty of folks using older versions for years yet.
It's up to you to decide whether the styling is important enough to
care about those back-level folks - they'll still be able to view the
content, just that it won't look quite the way you want...
http://ppewww.ph.gla.ac.uk/~flavell/tests/tablefun.html (this is just
a test - in the practical situation the styles would be moved out
into a stylesheet, of course).

If you want to take it to extremes, try something like this earlier
experiment: http://ppewww.ph.gla.ac.uk/~flavell/tests/poules2.html ,
although that does make /some/ use of classes (odd/even) to keep the
CSS selectors from getting entirely out of hand.

<digression>
*According to the HTML specification*, the alignment which is
specified by <col align="..."> is /supposed/ to carry through to the
cells of that column. Unfortunately, this rather odd backwater of the
HTML specification is /so/ out of line with the way that CSS works,
that there's something of a clash in browser implementations, and it's
not really something that can be used in practice. In fact it's a
long-standing (possibly in fact *the* oldest) open bug in the Mozilla
bugzilla.
 
Reply With Quote
 
John Salerno
Guest
Posts: n/a
 
      06-01-2006
Alan J. Flavell wrote:

>> It doesn't seem very elegant though, for some reason.

>
> Agreed. But it *is* the safest way to get wide acceptability
> across browsers and versions.


Uh oh, situation: I need the text in some of these cells also to be red,
but I can't specify more than one class per <td> element, right? How
should I handle that?
 
Reply With Quote
 
Sid Ismail
Guest
Posts: n/a
 
      06-01-2006
On Thu, 01 Jun 2006 20:13:10 GMT, John Salerno
<> wrote:

: Alan J. Flavell wrote:
:
: >> It doesn't seem very elegant though, for some reason.
: >
: > Agreed. But it *is* the safest way to get wide acceptability
: > across browsers and versions.
:
: Uh oh, situation: I need the text in some of these cells also to be red,
: but I can't specify more than one class per <td> element, right? How
: should I handle that?


Use class and ID.

Sid


 
Reply With Quote
 
Alan J. Flavell
Guest
Posts: n/a
 
      06-01-2006
On Thu, 1 Jun 2006, John Salerno wrote:

> Uh oh, situation: I need the text in some of these cells also to be
> red, but I can't specify more than one class per <td> element,
> right?


Actually you can. class="alarm text" would match both .alarm
and .text selectors in your stylesheet.
http://www.w3.org/TR/REC-html40/stru...tml#adef-class

So you could style the .text as right-align, and the .alarm as red.

Some older browser/versions might not grok that, but I think it's
pretty well covered nowadays.

You could of course assign two different classes - say, text and
atext, one of which is styled for only right-alignment, and the other
is red *and* right-alignment. Your choice, really, but the latter is
going to be somewhat better supported.

Naturally you should pick class names which mean something to your
particular situation. I was just illustrating possibilities.
 
Reply With Quote
 
John Salerno
Guest
Posts: n/a
 
      06-01-2006
Alan J. Flavell wrote:

> Actually you can. class="alarm text" would match both .alarm
> and .text selectors in your stylesheet.
> http://www.w3.org/TR/REC-html40/stru...tml#adef-class


Ah, I think I had misread this: "Note: Only one class attribute can be
specified per HTML element!"

One attribute, but it can contain multiples names! Thanks!
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
<div align=center> also affects align of dynamic dropdown item text Steve Richter ASP .Net 1 06-24-2007 07:11 PM
left align within centre align Paul Lautman HTML 3 03-03-2006 02:45 PM
text-align vs align tshad HTML 1 06-23-2005 10:29 PM
how to Align text left & vertical align middle Kay ASP .Net 2 07-25-2003 08:32 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57