Search code examples
htmlcsshtml-tableinput-field

Two input fields in one row, align one left and the other right


I want to have a form which consists of different input fields. On some rows there should be two input fields. The second one should be aligned right.

jsFiddle

HTML:

<table>
    <tr>
        <td>
            <input type="text" name="first-name" class="form-first-name" />&nbsp;
            <input type="text" name="last-name" class="form-last-name" />
        </td>
    </tr>
    <tr>
        <td class="empty-row">&nbsp;</td>
    </tr>
    <tr>
        <td>
            <input type="text" name="address-1" class="form-address-1" />
        </td>
    </tr>
</table>

CSS:

.form-address-1 {
    width: 100%;
}

.form-first-name, .form-last-name {
    width: 46%;
}

.form-last-name {
    float: right;
}

My problem is that the input fields have different sizes and so the size of the row somehow changes. I tried to set the width of tr without success. How can I align the second input field right?


Solution

  • Its a box-layout issue.

    check with the inspector, and you'll see that your address input is overflowing its row. (thats because 100% + default margin/padding = more than 100%)

    Working Fiddle

    Add this to your CSS

    *
    {
        -moz-box-sizing: border-box;
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
    }