html - 如何使输入的反应方式与弹性框中的父 div 相同?
问题描述
我正在尝试使用 flexbox 正确调整一些 div 的大小,以便稍后我要添加的输入和其他项目具有正确的大小,但无论我对 div 做什么,输入大小都不会t 改变。
我可以对输入做些什么来使它的大小也根据父 div 改变?
下面我发布了我目前拥有的代码,以及输入明显很小的相同代码。第二个显示具有正确大小的绿色区域和小输入,而在顶部的一个上,输入将父级拉伸并且都具有相同的大小......
我需要将第二个标记为绿色的尺寸保持不变,并且我希望输入与该尺寸相匹配......
.flex-o-1 {flex: 1 1 auto;}
.flex-o-2 {flex: 2 2 auto;}
.flex-o-3 {flex: 3 3 auto;}
.flex-o-4 {flex: 4 4 auto;}
.flex-o-5 {flex: 5 5 auto;}
.GenInfo {display: flex;}
.GenInfo>div {
margin-left: 0.2em;
margin-right: 0.2em;
background-color: green;
height: 2em;
}
.GenInfo>div>input {max-width: 100%;} /* Also tried without this, nothing changes */
.SmallInput {max-width: 1em !important;} /* !important just for testing */
<div class="GenInfo">
<div class="flex-o-5">
<input type="text" />
</div>
<div class="flex-o-1">
<input type="text" />
</div>
<div class="flex-o-3">
<input type="text" />
</div>
<div class="flex-o-2">
<input type="text" />
</div>
<div class="flex-o-2">
<input type="text" />
</div>
<div class="flex-o-4">
<input type="text" />
</div>
</div>
<br>
<div class="GenInfo">
<div class="flex-o-5">
<input type="text" class="SmallInput" />
</div>
<div class="flex-o-1">
<input type="text" class="SmallInput" />
</div>
<div class="flex-o-3">
<input type="text" class="SmallInput" />
</div>
<div class="flex-o-2">
<input type="text" class="SmallInput" />
</div>
<div class="flex-o-2">
<input type="text" class="SmallInput" />
</div>
<div class="flex-o-4">
<input type="text" class="SmallInput" />
</div>
</div>
解决方案
应用一个小宽度然后使用min-width:100%
.flex-o-1 {flex: 1 1 auto;}
.flex-o-2 {flex: 2 2 auto;}
.flex-o-3 {flex: 3 3 auto;}
.flex-o-4 {flex: 4 4 auto;}
.flex-o-5 {flex: 5 5 auto;}
.GenInfo {display: flex; margin-bottom:5px}
.GenInfo>div {
margin-left: 0.2em;
margin-right: 0.2em;
background-color: green;
height: 2em;
}
.GenInfo>div>input {
min-width: 100%;
width: 1px;
box-sizing: border-box;
}
<div class="GenInfo">
<div class="flex-o-5">
<input type="text" >
</div>
<div class="flex-o-1">
<input type="text" >
</div>
<div class="flex-o-3">
<input type="text" >
</div>
<div class="flex-o-2">
<input type="text" >
</div>
<div class="flex-o-2">
<input type="text" >
</div>
<div class="flex-o-4">
<input type="text" >
</div>
</div>
<div class="GenInfo">
<div class="flex-o-5">
</div>
<div class="flex-o-1">
</div>
<div class="flex-o-3">
</div>
<div class="flex-o-2">
</div>
<div class="flex-o-2">
</div>
<div class="flex-o-4">
</div>
</div>
推荐阅读
- c# - 将超过 1000 行写入 Google 表格
- 3d - 如何修改 LibGDX 3D 中的雾以使其“更厚””
- python - 使用 aio-pika 时,有没有办法禁用证书验证
- python - 注册带有附加参数的函数?
- azure - 使用 CLI 命令创建 Azure 私有 DNS 区域 A Recorset:未找到父资源
- python - 熊猫在他的列中获取单元格的索引?
- list - 尝试在 Prolog 中编写代码,我输入一个包含字符串和数字的列表,我想获得它们之间的最大数字
- python - 如何在列表中找到匹配条件的第一个元素?
- rabbitmq - 为什么 RabbitMQ 客户端在将实际消息体发送到 RabbitMQ 代理之前发送一个方法帧和一个内容帧?
- .net-core - UTF8.Encoding.UTF8.GetBytes 在 Windows 和 Linux 上返回不同的结果