views:

349

answers:

2
A: 

Wrap the fields with a div, set the div to have the desired width, and set the input and the label to fill the div.

Either that or set the width for both the label and the input to the same pixels. Div method is a bit more flexible.

Note though, the "Name:" prompt is the label. The error should be a span or something, it isn't a "label", at least not in the way HTML thinks about it.

Parrots
can you give an example of both the methods?
fuz3d
+1  A: 

This works, without adding any extra divs (though admittedly there is an extra span (.errorMsg) to contain the error-message:

Using XHTML 1.0 strict doctype.

    <style>

     #container
      {width: 50%;
      margin: 0 auto;
      }

     label {display: inline-block;
      width: 48%;
      text-align: right;
      }

     input {display: inline-block;
      width: 50%;
      }
     .errorMsg
      {display: block;
      width: 51%;
      margin: 0 0 0.5em 49%;
      color: #f00;
      background-color: #ffa
      }

    </style>

...

    <div id="container">

     <form>

      <label for="input1">Label 1</label>
       <input type="text" id="input1" name="input1" />
       <span class="errorMsg">Error message</span>

      <label for="input1">Label 2</label>
       <input type="text" id="input2" name="input2" />
       <span class="errorMsg">Error message</span>

     </form>

    </div>

It's worth noting that the width of the .errorMsg is 51%, not 50%, to accomodate the borders of the input (being added to the 50% width defined in the CSS. That may just be FF3.x on Ubuntu 8.04, though. I've not tested exhaustively. Or much at all, I'm afraid. YM, as always, MV.

Demonstration at: http://www.davidrhysthomas.co.uk/so/errorLabels.html.

David Thomas
thanks. but the problem is i'm using jquery validation plug-in, so the error label/span is defined in the package. i can only make the modifications in label.error
fuz3d
You may find it worthwhile swapping out the 'table' tag for a 'JQuery' tag. This might help bring in people able to help you more appropriately. Sorry I can't be of more use to you myself =/
David Thomas