I’m trying to align input fields to all start at the same point but the text labels are different lengths, so the input fields naturally start at different distances. How can I align them all to start at the same time?
I’ve tried to align the inputs to the right, align text to the left, etc. and nothing seems to work. Here is my code below:
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}
th,
td {
padding: 8px;
text-align: left;
}
<table>
<tbody>
<!-- Row 1 -->
<tr>
<td>
<label for="row1col1">Example 12345:</label>
<input type="text" id="row1col1" name="row1col1">
</td>
<td>
<label for="row1col2">An example:</label>
<input type="text" id="row1col2" name="row1col2">
</td>
</tr>
<!-- Row 2 -->
<tr>
<td>
<label for="row2col1">very long label here:</label>
<input type="text" id="row2col1" name="row2col1">
</td>
<td>
<label for="row2col2">short label:</label>
<input type="text" id="row2col2" name="row2col2">
</td>
</tr>
<!-- Row 3 -->
<tr>
<td>
<label for="row3col1">Label:</label>
<input type="text" id="row3col1" name="row3col1">
</td>
<td>
<label for="row3col2">label123:</label>
<input type="text" id="row3col2" name="row3col2">
</td>
</tr>
<!-- Row 4 -->
<tr>
<td>
<label for="row4col1">etc:</label>
<input type="text" id="row4col1" name="row4col1">
</td>
<td>
<label for="row4col2">etc:</label>
<input type="text" id="row4col2" name="row4col2">
</td>
</tr>
<!-- Row 5 -->
</tbody>
</table>
2
Answers
You can make use of
flexbox
to achieve your result.td
elements in adiv
with classinput group
.display: flex
to the input group and aligned items to center.You can try something like below:
You could use a TD for each item instead of one per pair of item