views:

151

answers:

2

Hi.

I'm using rightToLeft language for my UI, so I need to have label of textfield on right side. I tried using div with float:right for label and div with float:left for textfield. but it still doesn't align correctly in my form.

This is code sample:

<s:form action="processRegister" cssClass="form">
    <div style="float:right;text-align:right">
        <s:text name="label12.msg"/>
    </div>
    <div style="float:left">
        <s:textfield name="username" cssClass="textfield" />
    </div>

    <div style="float:right;text-align:right">
        <s:text name="label13.msg"/>
    </div>
    <div style="float:left">
        <s:password id="password1" name="password" cssClass="textfield"/>
    </div>
</s:form>

and when I used table and tried to put textfield on one column and label on the other column, they didn't align on one line.

A: 

In what way is it not aligning properly? can show us some screenshot and example of codings? U wanna check your magin and padding of each field that is not aligning properly? Make the margin and padding to 0 first.

Actually another way is to use a table, if it applies to you. Inside individual cell, just make sure the padding/ margin is 0. Then using the cell's alignment property to adjust the align problem. From what I see, using table for form to align is definitely cross browser

C_Rance
Thanks but when I use table and try to put textfield in left column and it's label in right column, textfield gets a whole line and doesn't align in one line with it's label. I used simple theme for this problem but it doesn't let Validation to work so I had to remove it.
Zim
actually your text field is a text box or a textarea, from your code, it seems like a textbox equivalent. I don't quite understand your term of "textfield gets a whole line and doesn't align in one line with it's label".Maybe you can try this? Control the width of the cell and table.If this still doesn't help, then I'm sorry that unable to help<table width="300"><tr> <td width="150" align="right"> <label> </td> <td width="150" align="left"> <textfield> </td></tr></table>
C_Rance
A: 

This jsfiddle does what you want. The key is in clearing your floats so that all the input/label rows end up on their own distinct lines. It's also a good idea when using floats to specify widths on your elements whenever possible. This will prevent accidental float drop.

Also, for usability, I've added a <label> element so screen readers will know which <input>'s your labels apply to.

HTML

<form>
    <div>
        <label for="username">Username</label>    
        <input type="text" id="username" name="username" size="20"/>
    </div>
    <div>
        <label for="password">Password</label>   
        <input type="password" id="password" name="password" size="20"/>
    </div>
</form>

CSS

div {
    clear: both;
    width: 300px;
}

div label {
    float: right;
    text-align: right;
}

div input {
    float: left;
}
Pat
Thanks. I need to check this out. But actually the problem is that I have to user <s:textfield> tag of struts2 for getting input from user and it renders a <tr> with 2 <td>, one for label and another one for input. I guess I need to define a new template for that tag. Thanks for your response anyway.
Zim
Could you switch to the `simple` theme for those textfields (`<s:textfield theme="simple" />`)? That way it wouldn't render the tr and td tags. Unfortunately if you have to keep using the table row/table cell, there's not much you can do to swap the position of the label and input field.
Pat