我在数字输入组件中使用
flex
来在输入本身旁边呈现自定义“+”和“-”按钮。按钮使用 aspect-ratio: 1/1;
来确保它们始终是方形的,但我似乎无法让按钮的容器遵守这一点。相反,按钮溢出了容器,几乎就像容器在没有 aspect-ratio
属性的情况下测量其大小一样。当我删除该属性时,容器会以适当的大小呈现。
一些示例代码:
.input-interior {
box-shadow: 0 1px 0 blue;
box-sizing: border-box;
display: flex;
flex-grow: 1;
position: relative;
width: 600px;
}
.input-container {
flex-grow: 1;
min-width: 0;
}
input {
appearance: textfield;
background-color: transparent;
border: none;
box-shadow: none;
box-sizing: border-box;
color: rgb(15 15 15);
height: 80px;
margin: 0;
outline: none;
padding-left: 8px;
padding-right: 8px;
padding-top: 5px;
padding: 0;
width: 100%;
}
.input-actions {
box-shadow: 0 0 0 1px green;
box-sizing: border-box;
column-gap: 8px;
display: flex;
flex-shrink: 0;
padding: 8px;
}
button {
border: none;
margin: 0;
padding: 0;
aspect-ratio: 1 / 1;
min-height: 44px;
align-items: center;
display: flex;
justify-content: center;
min-width: auto;
width: auto;
}
<div class="input-interior">
<div class="input-container">
<input min="0" step="1" type="number" value="0" />
</div>
<div class="input-actions">
<button type="button">-</button>
<button type="button">+</button>
</div>
</div>
如何让
.input-actions
容器适应其子级的动态大小?
简短回答:
min-height
应等于整个可用高度。
button {
min-height: 64px;
}
.input-interior {
box-shadow: 0 1px 0 blue;
box-sizing: border-box;
display: flex;
flex-grow: 1;
position: relative;
width: 600px;
}
.input-container {
flex-grow: 1;
min-width: 0;
}
input {
appearance: textfield;
background-color: transparent;
border: none;
box-shadow: none;
box-sizing: border-box;
color: rgb(15 15 15);
height: 80px;
margin: 0;
outline: none;
padding-left: 8px;
padding-right: 8px;
padding-top: 5px;
padding: 0;
width: 100%;
}
.input-actions {
box-shadow: 0 0 0 1px green;
box-sizing: border-box;
column-gap: 8px;
display: flex;
flex-shrink: 0;
padding: 8px;
}
button {
border: none;
margin: 0;
padding: 0;
aspect-ratio: 1 / 1;
min-height: 64px;
align-items: center;
display: flex;
justify-content: center;
min-width: auto;
width: auto;
}
<div class="input-interior">
<div class="input-container">
<input min="0" step="1" type="number" value="0" />
</div>
<div class="input-actions">
<button type="button">-</button>
<button type="button">+</button>
</div>
</div>
详情
当只有
min-height
时,.input-actions
容器也将其用作按钮的宽度(自 aspect-ratio: 1/1
起),并以此为基础构建自己的宽度:
112px = (2 * button min-height 44px) + (2 * container padding 8px) + (column-gap 8px)
设置
min-height: 100%
似乎很合理,但在这种情况下,容器将变得甚至小于 40px(取决于字体规格),因为它需要绝对单位,而不是相对单位。
CSS 变量和
calc()
可用于使高度可在一处设置:
.input-interior {
--input-height: 80px;
box-shadow: 0 1px 0 blue;
box-sizing: border-box;
display: flex;
flex-grow: 1;
position: relative;
width: 600px;
}
.input-container {
flex-grow: 1;
min-width: 0;
}
input {
appearance: textfield;
background-color: transparent;
border: none;
box-shadow: none;
box-sizing: border-box;
color: rgb(15 15 15);
height: var(--input-height);
margin: 0;
outline: none;
padding-left: 8px;
padding-right: 8px;
padding-top: 5px;
padding: 0;
width: 100%;
}
.input-actions {
box-shadow: 0 0 0 1px green;
box-sizing: border-box;
column-gap: 8px;
display: flex;
flex-shrink: 0;
padding: 8px;
}
button {
border: none;
margin: 0;
padding: 0;
aspect-ratio: 1 / 1;
min-height: calc(var(--input-height) - 8px * 2);
align-items: center;
display: flex;
justify-content: center;
min-width: auto;
width: auto;
}
<div class="input-interior">
<div class="input-container">
<input min="0" step="1" type="number" value="0" />
</div>
<div class="input-actions">
<button type="button">-</button>
<button type="button">+</button>
</div>
</div>