在内容框剩余宽度中输入标签

input with labels in content box remaining width

我在自定义内容框的样式方面遇到了一些问题。内容框包含几个 labelinput 字段。内容框的 width 为 `500px。

一些 labels 比其他的长,但是输入应该总是和 windows 一样大(- 有点白 space)。

这是我现在的 HTML:

<div class="content">
    <div class="row">
        <label for="test1">This is a test #1</label>
        <input type="text" name="test1">
    </div>
    <div class="row">
        <label for="test2">This is a test #2</label>
        <input type="text" name="test2">
    </div>
    <div class="row">
        <label for="test3">But this is a test with a much longer label</label>
        <input type="text" name="test3">
    </div>
</div>

这是 CSS:

.content {
    border: 1px solid #000;
    width: 500px;
    height: 500px;
}

.row {
    margin-top: 5px;
}

input {
    width: 385px;
}

如此处的 fiddle 所示:https://jsfiddle.net/4ga3533o/ 您可以看到前 2 个 'rows' 工作正常,但是到第三个时,input 在下方label 因为宽度比内容框长,我怎样才能让它工作,使标签可以有不同的长度,但宽度只占宽度的剩余 space?

display: flex 将是您问题的最佳解决方案。试试这个代码。

<div class="content">
  <div class="form-wrap">
    <label for="test1">This is a test #1</label>
    <input type="text" name="test1">
  </div>
  <div class="form-wrap">
    <label for="test2">This is a test #2</label>
    <input type="text" name="test2">
  </div>
  <div class="form-wrap">
    <label for="test3">But this is a test with a much longer label</label>
    <input type="text" name="test3">
  </div>
</div>

.content {
  border: 1px solid #000;
  width: 500px;
  height: 500px;
}

.row {
  margin-top: 5px;
}

input {
  width: 385px;
}

.form-wrap {
  margin-bottom: 10px;
  display: flex;
  align-items: flex-start;
}

.form-wrap label {
  width: 40%;
}

你可以flex

.content {
  border: 1px solid #000;
  width: 500px;
  height: 500px;
}
.row {
  margin-top: 5px;
  display: flex;
}
input {
  flex: 1;
}
label {
  margin-right: 10px;
}
<div class="content">
  <div class="row">
    <label for="test1">This is a test #1</label>
    <input type="text" name="test1">
  </div>
  <div class="row">
    <label for="test2">This is a test #2</label>
    <input type="text" name="test2">
  </div>
  <div class="row">
    <label for="test3">But this is a test with a much longer label</label>
    <input type="text" name="test3">
  </div>
</div>