2019 answer:
Some time passed, and now I have changed my approach to building forms. I have made thousands of them until today and am very tired of typing id for each label/input pair, so this was dumped into the toilet. When you enter input directly into the label , everything works the same way, identifiers are not needed. I also took advantage of flexbox that flexbox was very flexible.
HTML:
<label> Short label <input type="text" name="dummy1" /> </label> <label> Somehow longer label <input type="text" name="dummy2" /> </label> <label> Very long label for testing purposes <input type="text" name="dummy3" /> </label>
CSS:
label { display: flex; flex-direction: row; justify-content: flex-end; text-align: right; width: 400px; line-height: 26px; margin-bottom: 10px; } input { height: 20px; flex: 0 0 200px; margin-left: 10px; }
Violin DEMO
Original answer:
Use label instead of span . It is designed to interface with inputs and retains some additional functions (clicking a label focuses the input).
This may be exactly what you want:
HTML:
<label for="dummy1">title for dummy1:</label> <input id="dummy1" name="dummy1" value="dummy1"> <label for="dummy2">longer title for dummy2:</label> <input id="dummy2" name="dummy2" value="dummy2"> <label for="dummy3">even longer title for dummy3:</label> <input id="dummy3" name="dummy3" value="dummy3">
CSS:
label { width:180px; clear:left; text-align:right; padding-right:10px; } input, label { float:left; }
jsfiddle demo here.
Petr Δihula
source share