views:

489

answers:

4

I have a table based layout, with one main two-column table, for left and right sides of a form. Each column contains an inner, two-column table, with a label and and input column, and each field on its own row.

Could I use CSS to set the width of both the input columns?

E.g.

<table id="frameTable">
  <tr>
    <td id="leftFieldList">
      <table class="formColumn">
        <tr>
          <td>Surname</td>
          <td><asp:TextBox.....></td>
        </tr>
        <tr>
          <td>Address</td>
          <td><asp:TextBox.....></td>
        </tr>
        ....
        ....
      <table>
    </td>
</table>

In the table with class "formColumn", I want to set the the width of the second <td>, with the server controls. I would prefer to do this without assigning a class to every second <td> in my field lists.

+2  A: 

Usually, you could do:

table.formColumn tr td:last-child { width: 200px; }

but IE7 does not recognize last-child, so you might have to go with

table.formColumn tr td+td { width: 200px; }

IE6 does not recognize the + selector, according to quirksmode.org. If you're planning on supporting IE6, I don't have a solution for you, with your current markup.

That aside, some argue that you should not use tables to design the layout of your website. That second table (formColumn) could've been replaced with something like this:

<fieldset><label ...>Surname</label><YourTextInput /></fieldset>
Hugo Peixoto
Good answer. Especially like the fieldset suggestion; i struggled for a way to suggest that without sounding rude and eventually gave up, but i think you nailed it. Deleting mine, upvoting this.
Shog9
Why would you sound rude? I know you and your web mastery, and I know my ignorance. I used tables because I was copying how our legacy layout at work is done, and that layout is what is rude.
ProfK
+1  A: 

If you want to set them to the same width it's easy:

table.formColumn td {
  width: 150px;
}

If you want them to be different widths, it's a bit more problematic but not hugely so. Easiest option is just to assign a class to the td elements in the first row:

<table id="frameTable">
  <tr>
    <td id="leftFieldList">
      <table class="formColumn">
        <tr>
          <td class="left">Surname</td>
          <td class="right"><asp:TextBox.....></td>
        </tr>
        <tr>
          <td>Address</td>
          <td><asp:TextBox.....></td>
        </tr>
        ....
        ....
      <table>
    </td>
</table>

then:

table.formColumn td.left {
  width: 150px;
}


table.formColumn td.right {
  width: 300px;
}

Since the rest of the column will take those widths.

You could also use the + selector, but it's not supported in IE6:

table.formColumn td {
  width: 150px;
}


table.formColumn td + td {
  width: 300px;
}
cletus
+5  A: 

The best solution around: use col tags!

<style> .col2 { width: 200px; } </style>
...

<table class="formColumn">
  <colgroup>
    <col></col>
    <col class="col2"></col>
  </colgroup>
  <tbody>
    <tr>...
  </tbody>
</table>
David Hanak
just to add to this: here is the col specification http://www.w3.org/TR/html4/struct/tables.html#h-11.2.4 cols are incredibly useful for exactly the purpose the poster needs
Darko Z
oh and another thing to add, cols actually support css classes. So this is really a css solution
Darko Z
Yep, the col element is the best solution. You can style columns with CSS: use <col class="theclass"></col> with .theclass { width: thewidth }
DisgruntledGoat
Heh, nice. But as Darko notes, you don't actually have to hard-code the width in the markup.
Shog9
Shog9: thanks for the modification.
David Hanak
I'm asked in my question to try and "do this without assigning a class to every second <td> in my field lists.". This is a good solution, but I have a fair number of cols that I would need to retro-fit class attributes to,
ProfK
+1: I forgot you could put classes on a col.
Joel Potter
+3  A: 

Another IE6-unfriendly solution is

.formColumn td:last-child { width: 200px; }

If you're not stuck on CSS, you could use col groups

<table class="formColumn">
   <colgroup>
      <col />
      <col width="200px" />
   </colgroup>
   <tr>
      <td>Surname</td>
      <td><asp:textbox..../></td>
   </tr>
</table>
Joel Potter
Not stuck on CSS - I just didn't think HTML could do it.
ProfK