The accepted answer (setting the explicit width in pixels) makes changes difficult and is interrupted when your users use a different font size. Using CSS tables, on the other hand, works great:
form { display: table; } p { display: table-row; } label { display: table-cell; } input { display: table-cell; }
<form> <p> <label for="a">Short label:</label> <input id="a" type="text"> </p> <p> <label for="b">Very very very long label:</label> <input id="b" type="text"> </p> </form>
Here's JSFiddle: http://jsfiddle.net/DaS39/1/
And if you need right-aligned labels, just add text-align: right to the labels: http://jsfiddle.net/DaS39/
EDIT: Another short note: CSS tables also allow you to play with columns: for example, if you want input fields to take up as much space as possible, you can add the following to your form:
<div style="display: table-column;"></div> <div style="display: table-column; width:100%;"></div>
you might want to add white-space: nowrap to labels in this case.
Clément May 19 '14 at 15:00 2014-05-19 15:00
source share