【发布时间】:2019-02-09 00:00:12
【问题描述】:
目前我正在使用网格和弹性框之间的混合来尝试实现表单的布局。
我希望两个 input 元素水平排列而不设置高度。如果我要在文本输入上申请填充,则范围应继续垂直居中,并与相邻的输入相邻。
此外,我还希望 label 元素也保持水平对齐。
请提出不需要更改标记或使用 Javascript 的解决方案。
我知道我可以在范围上应用一个固定的高度或填充来对齐它,但我正在努力使它尽可能流畅。
HTML:
<form class="grid">
<div class="group">
<label for="input-1">Label 1</label>
<input type="text" id="input-1" placeholder="placeholder" />
</div>
<div class="group">
<label for="input-2">Label 2</label>
<div class=range-wrapper>
<input type="range" id="input-2" placeholder="placeholder" />
<output>0</output>
</div>
</div>
</form>
SCSS:
html {
box-sizing: border-box;
}
*, *::before, *::after {
box-sizing: inherit;
}
body {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
font-size: 1.2rem;
}
.grid {
width: 80%;
max-width: 960px;
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-gap: 40px 10px;
padding: 50px;
background: #ccc;
}
.group {
display: flex;
flex-direction: column;
margin-top: auto;
label {
margin-bottom: 10px;
}
}
input {
font-size: inherit;
&[type="text"] {
padding: 14px;
}
&[type="range"] {
width: 100%;
}
}
.range-wrapper {
display: flex;
align-items: center;
output {
padding: 10px;
}
您也可以查看codepen here。
非常感谢!
** 编辑 **
应该支持多行标签,如果标签包含多行,元素仍然应该水平对齐。
多行标签示例:Codepen
【问题讨论】:
-
没有允许对齐不共享父元素的元素的 CSS 属性或布局方法。
-
你可以做的是强制组 div 中的最后一个项目一直到行的底部......像这样 - codepen.io/Paulie-D/pen/GXmVWv
-
这与将
margin-bottom: auto添加到label元素中的效果相同。另外,如果我想将更多input组放入网格中,它将无法正常工作。此外,这实际上并没有解决相邻input和range之间的对齐问题。 -
正如我所说....有 NO CSS 方法。
标签: html css sass flexbox css-grid