views:

59

answers:

6

Frequently I am aligning text such as:

To: 07/02/2010
From: 07/02/2010

Id like it to be displayed like this:

To:   07/02/2010
From: 07/02/2010

So what is the quickest/easiest/best way to do this? CSS? using a few nbsp (would work if its mono spacing) or using tables. Normally if I am not in a anti-hating table mood, ill use tables.

What do you recommend?

+1  A: 

Use a definition list or white-space nowrap.

vise
+3  A: 

Definitely definition list (<dl>).

<dl>
    <dt>From:</dt><dd>07/02/2010</dd>
    <dt>To:</dt><dd>07/02/2010</dd>
</dl>

/* CSS */

dl {
    overflow: hidden;
}

dt {
    width: 50px;
    float: left;
}
Crozin
That's even less semantically true than a table. A date is not the definition of "To" or "From". Why not just use a table?
Vincent McNabb
Not very simple at all.
BHare
Why not just use divisions? No need to use the wrong element, these aren't definitions.
animuson
@Vincent Not only are `<dl/>` s not in any way restricted to "definitions", but a date is in fact a definition of To: or From:
lucideer
@lucideer No. The definition of "To:" would be "The date/time message was sent", not the actual date/time it was sent. Refer http://www.oxforddictionaries.com/definition/definition?view=uk
Vincent McNabb
@vincent Definition lists are sort of ambiguous in that sense, and because of that ambiguity many people use them liberally for situations like this where you need to represent key/value pairs and style them relationally. It's semantically plausible to treat the key as the term and the value as the definition for that term.
Superstringcheese
@Vincent The dictionary definition of the word "To", yes. The contextual definition however is the ACTUAL date/time message was sent.Also, if you actually read the W3C HTML spec. you'll see they are not even strictly restricted to definitions - it even cites an example of their use in representing dialogue (where `<dt/>` = speaker and `<dd/>` = line)
lucideer
+2  A: 

I'd recommend tables. It really is the best way, especially seeing as it really is tabular data there, and HTML doesn't support tab stops.

But it really is silly to avoid tables for the sake of avoiding tables. Unless you want the option later to style like so:

To:          From:
07/02/2010   07/02/2010

You could do something like this, if for some reason you didn't want to use tables:

CSS

.sideheading { width: 3em; float: left; }

HTML

<div class="sideheading">To:</div>07/02/2010
<div class="sideheading">From:</div>07/02/2010

Or use a definition list (but if the reason you are avoiding tables is due to semantics, then DLs would be avoided for the same thing).

But of course, it's about the layout, no customer or web surfer is ever going to care how you do it, as long as they can read it!

Vincent McNabb
Thanks. I always thought tables were made for displaying and formatting data, not really used for formatting a header or title. Why doesn't HTML support tab stops? I guess if you consider pre...but that doesn't keep the same styling.
BHare
A definition list is perfect here, nothing un-semantic about it. A table would also be good. Both tables and `<dl/>` s are certainly far far more semantic than a plain div (though your class-names do help).
lucideer
@Brian Tables are ONLY for formatting relational data, but this is a perfect example of that as the dates relate directly to the corresponding "To" and "From" labels. A table does seem slightly heavy though - a `<dl/>` would involve less code.
lucideer
I would say using tables is probably the most flexible method. You can use definition lists or labels or plain divs, but in all those cases you have to specify the width of the label, whereas tables will shrink to the data (if the table has no width defined, of course).
DisgruntledGoat
A: 

Padding with &nbsp;s sounds messy. How about something like this:

<span class="header">To:</span> 07/02/2010
<span class="header">From:</span> 07/02/2010

.header { display: inline-block; width: 5em;}

In this case, though, I'd actually say tables are appropriate; it does look like tabular data, with a column of headers.

Vineet
A: 

This has come up at work many times and I ended up creating some styling for a 2-column table which hides borders. Technically, this is tabular data, but a table with only 2 rows and 2 columns is pretty lame considering the amount of markup needed to achieve it within spec.

I've often regretted creating the class, as now everyone uses it far too much and I have to constantly be on the lookout for it in our code reviews. If you don't anticipate that problem, it's a semantically-correct solution, and slightly more elegant than the hoops you'll jump through with DL's, spans, etc.

Superstringcheese
+1  A: 

I've seen this problem before, a quick google search:

http://www.google.com/search?q=css+forms

...brought me here:

http://www.webcredible.co.uk/user-friendly-resources/css/css-forms.shtml

...and I copypasted the HTML and CSS into this:

<html>
    <head>
        <style>
        label
        {
            width: 5em;
            float: left;
            text-align: right;
            margin-right: 1em;
            display: block
        }

        .submit input
        {
            margin-left: 4.5em;
        }
        </style>
    </head>
    <body>
        <form action="#">
            <p><label for="name">Name</label> <input type="text" id="name" /></p>
            <p><label for="e-mail">E-mail</label> <input type="text" id="e-mail" /></p>
            <p class="submit"><input type="submit" value="Submit" /></p>
        </form> 
    </body>
</html>

Looks good to me, save it in a .html and see for yourself.

btfx
This is the most semantically correct.
Mark Trapp
@Mark: Not really. A label and text input is not a paragraph.
DisgruntledGoat
Like I said, just copypasted from the first result... Not anal enough to try to be the most semantically correct, since web design isn't my main occupation (but I do have to dabble a lot).
btfx