javascript - 如何为输入类型文本和输入类型日期获得相同的宽度
问题描述
我有两个<input>
元素 - 一个是 type text
,另一个是 type date
。当它们自己在一条线上时,它们都长到相同的宽度。但是,如果我将它们都移动到div
with 中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>
解决方案
这是您的要求 | 为了获得更好的性能,请使用 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 声明中多次使用单个类名。像“输入”
推荐阅读
- java - 使用 C# 中的 jni4net 连接 JVM 时 webapp 出错
- javascript - 大型firebase函数请求似乎导致连接错误
- jdbc - 如何通过将身份验证代码存储在数据库中而不是 ConCurrentHashMap 在 Spring 中配置 OAuth 2.0
- c - For循环创建顺序名称txt文件
- react-native - React Native WebView 不会在 android 上水平滚动
- scala - 如何将文本文件字符串分配给字典值到一个变量中,以及如何通过在 spark scala 中传递键来提取值?
- ruby - Docker Postgres 容器未针对 Rails 应用程序启动
- python - 为 Django ModelForm 中的特定字段添加 Formset
- tensorflow - Keras 没有检测到 GPU,但 tensorflow 是
- amazon-ec2 - aws 自动缩放导致 Laravel 中的自动注销