使用Flexbox时垂直对齐输入

时间:2018-04-09 08:32:57

标签: html css css3 flexbox

如何让输入框与中间垂直对齐?我尝试添加vertical-align:" middle"到几个地方,但我没有取得任何成功。我觉得flexbox是问题的一部分吗?



.container {
  /* Flex Properties */
  display: flex;
  flex-wrap: wrap;
  flex-direction: column;
  align-items: center;
}

.item {
  background-color: orange;
}

label {
  width: 100px;
  display: inline-block;
}

<div class="container">
  <div class="item">
    <label>Gee my iefwaf fwats: </label>
    <input>
  </div>
</div>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:4)

提供.item div display: flexalign-items: center,因为它是input 父 >元素:

.container {
  /* Flex Properties */
  display: flex;
  flex-wrap: wrap;
  flex-direction: column;
  align-items: center;
}

.item {
  display: flex; /* added */
  align-items: center; /* added */
  background-color: orange;
}

label {
  width: 100px;
  /*display: inline-block; not necessary*/
}
<div class="container">
  <div class="item">
    <label>Gee my iefwaf fwats: </label>
    <input type="text">
  </div>
</div>

答案 1 :(得分:1)

使用您的html尝试此css代码。

.container {
  display: flex;
  flex-wrap: wrap;
  flex-direction: column;
  align-items: center;
}

.item {
  background-color: orange;
  display: flex;
  align-items: center;
}

label {
  width: 100px;
  display: inline-block;
}
input {
  display: inline-block;
}

答案 2 :(得分:0)

请查看添加的CSS ::

.container {
  /* Flex Properties */
  display: flex;
  flex-wrap: wrap;
  flex-direction: column;
  align-items: center;
}

.item {
  background-color: orange;
}

    label {
      width: 100px;
      display: inline-block;
      vertical-align: middle;
    }

input {
      width: 100px;
      display: inline-block;
      vertical-align: middle;
    }
<div class="container">
  <div class="item">
    <label>Gee my iefwaf fwats: </label>
    <input>
  </div>
</div>