首页 > 解决方案 > 如何为输入类型文本和输入类型日期获得相同的宽度

问题描述

我有两个<input>元素 - 一个是 type text,另一个是 type date。当它们自己在一条线上时,它们都长到相同的宽度。但是,如果我将它们都移动到divwith 中display:flex,突然之间<input type="date" />会变得比另一个小。

我如何确保两个元素的宽度在它们可以自由增长(即没有硬编码的宽度)时得到相同的计算?

* {
  box-sizing: border-box;
  -webkit-box-sizing : border-box;
  -moz-box-sizing: border-box;
}
body{
  display:flex;
  flex-grow:1;
  flex-direction: column;
  height: 95vh;
}

.form{
  display:flex;
  flex-direction: column;
  width: 600px;
}
.input-row{
  display:flex;
}
.input-group{
  display:flex;
  flex-direction: column;
  flex-grow: 1;
  margin-bottom: 10px;
}
input {
  display: inline-block;
}
input, select {
  padding: .4em;
}

input[type="date"]{
  padding: .3em;
}

.comment{
  font-size: .8em;
  margin: 15px 0px;
  color: blue;
}
<div class="form">
<span class="comment">Notice how both text and date expand to the same width when they're alone on the line.</span>
  <div class="input-group">
    <label>Text</label>
    <input type = "text" />
  </div>
  
  <div class="input-group">
    <label>Date</label>
    <input type="date" />
  </div>
  
  <span class="comment">Notice how Date is smaller than Text when combined on a single line</span>
  <div class="input-row">
    <div class="input-group">
      <label>Text</label>
      <input type = "text" />
    </div>

    <div class="input-group">
      <label>Date</label>
      <input type="date" />
    </div>
  </div>


  <div class="input-row">
    <div class="input-group">
      <label>Text</label>
      <input type = "text" />
    </div>

    <div class="input-group">
      <label>Text2</label>
      <input type="text" />
    </div>
  </div>
</div>

标签: javascripthtmlcss

解决方案


这是您的要求 | 为了获得更好的性能,请使用 Bootstrap 框架和 w3schools.com

* {
  box-sizing: border-box;
  -webkit-box-sizing : border-box;
  -moz-box-sizing: border-box;
}
body{
  display:flex;
  flex-grow:1;
  flex-direction: column;
  height: 95vh;
}

.form{
  display:flex;
  flex-direction: column;
  width: 600px;
}
.input-row{
  display:flex;
}
.input-group{
  display:flex;
  flex-direction: column;
  flex-grow: 1;
  margin-bottom: 10px;
}
input {
  display: inline-block;
}
input, select {
  padding: .3em;
  width:50vw;
  height: 8vh;
  
}

input[type="date"]{
  padding: .3em;
  width:50vw;
  height: 8vh;
  
}

.comment{
  font-size: .8em;
  margin: 15px 0px;
  color: blue;
}
<div class="form">
<span class="comment">Notice how both text and date expand to the same width when they're alone on the line.</span>
  <div class="input-group">
    <label>Text</label>
    <input type = "text" />
  </div>
  
  <div class="input-group">
    <label>Date</label>
    <input type="date" />
  </div>
  
  <span class="comment">Notice how Date is smaller than Text when combined on a single line</span>
  <div class="input-row">
    <div class="input-group">
      <label>Text</label>
      <input type = "text" />
    </div>

    <div class="input-group">
      <label>Date</label>
      <input type="date" />
    </div>
  </div>


  <div class="input-row">
    <div class="input-group">
      <label>Text</label>
      <input type = "text" />
    </div>

    <div class="input-group">
      <label>Text2</label>
      <input type="text" />
    </div>
  </div>
</div>

我要求您不要在 CSS 声明中多次使用单个类名。像“输入


推荐阅读