Vertically align inputs with each other

2019-08-13 20:02发布

I have two inputs, but the pre-displayed labels have different length, causing the inputs not to start from the same position (vertically speaking). How to achieve that?

Some of my attempts are:

input {
    margin-left: 50px;
}

input {
    vertical-align:middle;
}

Please notice that display: block; will make the label and input lose their initial corresponding positions!

My JSFiddle.

1条回答
Anthone
2楼-- · 2019-08-13 20:48

You can make use of CSS table layout. The parent element acts as a table row and the child elements as table cells.

.input-container {
  display: table-row;
}
.input-container * {
  display: table-cell;
  margin-left: 5px;
}
<fieldset>
  <legend>Team1</legend>
  <div class="input-container">
    <label for="player1">Player1</label>
    <input type="number" name="player1">
  </div>
  <div class="input-container">
    <label for="player2">Player2345</label>
    <input type="number" name="player2">
  </div>
</fieldset>

查看更多
登录 后发表回答