我有一个模仿材质设计风格的表单html / css布局:
<div class="form-group">
<input
type="text"
id="feedName"
name="name"
value={nameValue}
onChange={this.handleInputChange}
/>
<label class="control-label" htmlFor="feedName">Name</label>
<i class="bar"></i>
</div>
,CSS是:
.form-group {
position:relative;
}
.form-group input,
.form-group select {
width: 100%;
}
.form-group input {
display: block;
background: none;
padding: 0.125rem 0.125rem 0.0625rem;
font-size: 1rem;
border-width: 0;
border-color: transparent;
transition: all 0.28s ease;
box-shadow: none;
height: 1.9rem;
}
.form-group .control-label {
position: absolute;
top: 0.25rem;
pointer-events: none;
padding-left: var(--gutter);
font-weight: normal;
transition: all 0.28s ease;
}
.form-group .bar {
position: relative;
border-bottom: 0.0625rem solid #999;
display: block;
}
.form-group .bar::before {
content: '';
height: 0.125rem;
width: 0;
left: 50%;
bottom: -0.0625rem;
position: absolute;
background: var(--secondaryGreen);
transition: left 0.28s ease, width 0.28s ease;
z-index: 2;
}
基本上,我在form-group
上设置相对定位,然后在control-label
上设置绝对定位,将标签放在输入字段的正上方。
我试图将其作为CSS网格中的grid-item
居中。对于文本项目,我可以使用:
.main > div:not(.form-group) {
/* text-only item vertical centering */
display: flex;
align-items: center;
}
但上面的弹性工具箱搞砸了form-group
。我也试过了:
.form-group {
position:relative;
margin: auto 0;
}
但这也不起作用。
完整的代码是:
:root {
--secondaryGreen: rgba(114, 191, 68, 1);
--gutter: 15px;
}
.main {
margin-top: 50px;
border: 1px solid rgba(0, 0, 0, 0.13);
border-radius: 4px;
background-color: #fff;
box-shadow: 0 -1px 2px 0 rgba(0, 0, 0, .05), 0 2px 3px 0 rgba(0, 0, 0, 0.10);
display: grid;
grid-template-columns: 100%;
grid-template-rows: 60px 50px 80px 80px 80px;
}
.main > div {
padding: 0 var(--gutter);
}
.main > div:not(.form-group) {
/* text-only item vertical centering */
display: flex;
align-items: center;
}
.title {
font-weight: 600;
color: var(--primaryBlue);
font-size: 16px;
border-bottom: 1px solid rgba(0, 0, 0, 0.13);
}
.form-group {
position:relative;
margin: auto 0;
/*margin-bottom: 2.25rem;*/
/*margin-top: auto;
margin-bottom: auto;*/
/*vertical-align: baseline;*/
}
.form-group input,
.form-group select {
width: 100%;
}
.form-group input {
display: block;
background: none;
padding: 0.125rem 0.125rem 0.0625rem;
font-size: 1rem;
border-width: 0;
border-color: transparent;
transition: all 0.28s ease;
box-shadow: none;
height: 1.9rem;
}
.form-group .control-label {
position: absolute;
top: 0; /* 0.25rem; */
pointer-events: none;
padding-left: var(--gutter);
/*z-index: 1;*/
/*font-size: 1rem;*/
font-weight: normal;
transition: all 0.28s ease;
}
.form-group .bar {
position: relative;
border-bottom: 0.0625rem solid #999;
display: block;
}
.form-group .bar::before {
content: '';
height: 0.125rem;
width: 0;
left: 50%;
bottom: -0.0625rem;
position: absolute;
background: var(--secondaryGreen);
transition: left 0.28s ease, width 0.28s ease;
z-index: 2;
}
.form-group select,
.form-group input:focus,
.form-group input:valid,
.form-group input.form-file,
.form-group input.has-value,
.form-group textarea:focus,
.form-group textarea:valid,
.form-group textarea.form-file,
.form-group textarea.has-value {
color: #333;
}
.form-group select ~ .control-label,
.form-group input:focus ~ .control-label,
.form-group input:valid ~ .control-label,
.form-group input.form-file ~ .control-label,
.form-group input.has-value ~ .control-label,
.form-group textarea:focus ~ .control-label,
.form-group textarea:valid ~ .control-label,
.form-group textarea.form-file ~ .control-label,
.form-group textarea.has-value ~ .control-label {
font-size: 0.8rem;
color: gray;
top: -1rem;
left: 0;
}
.form-group select ~ .control-label {
top: -1.5rem;
}
.form-group select:focus,
.form-group input:focus,
.form-group textarea:focus {
outline: none;
}
.form-group select:focus ~ .control-label,
.form-group input:focus ~ .control-label,
.form-group textarea:focus ~ .control-label {
color: var(--secondaryGreen);
}
.form-group select:focus ~ .bar::before,
.form-group input:focus ~ .bar::before,
.form-group textarea:focus ~ .bar::before {
width: 100%;
left: 0;
}
&#13;
<div class="main">
<div class="title">Title</div>
<div>Details</div>
<div class="form-group">
<input
type="text"
id="feedName"
name="name"
value=""
onChange={this.handleInputChange}
/>
<label class="control-label" htmlFor="feedName">Name</label>
<i class="bar"></i>
</div>
<div class="form-group">
<select
id="feedKind"
name="feedKind"
onChange={this.handleInputChange}
>
<option value="option 1">
Option 1
</option>
<option value="option 2">
Option 2
</option>
</select>
<label class="control-label" htmlFor="feedKind">Type</label>
<i class="bar"></i>
</div>
&#13;
输入字段的位置似乎也受到其下方选择状态的影响。我很困惑grid-item
如何影响其他grid-item
的位置。
答案 0 :(得分:1)
对您的代码进行以下调整:
(无需绝对定位。)
.main > div {
display: flex;
flex-direction: column;
justify-content: center; /* vertical centering */
}
.form-group .control-label {
order: -1; /* position label first in the stack */
}
:root {
--secondaryGreen: rgba(114, 191, 68, 1);
--gutter: 15px;
}
.main {
margin-top: 50px;
border: 1px solid rgba(0, 0, 0, 0.13);
border-radius: 4px;
background-color: #fff;
box-shadow: 0 -1px 2px 0 rgba(0, 0, 0, .05), 0 2px 3px 0 rgba(0, 0, 0, 0.10);
display: grid;
grid-template-columns: 100%;
grid-template-rows: 60px 50px 80px 80px 80px;
}
.main > div {
padding: 0 var(--gutter);
display: flex;
flex-direction: column;
justify-content: center;
/* text-align: center; <-- horizontal centering, if necessary */
border: 1px dashed red !important; /* demo only */
}
.main > div + div {
margin-top: 10px; /* demo only */
}
.title {
font-weight: 600;
color: var(--primaryBlue);
font-size: 16px;
border-bottom: 1px solid rgba(0, 0, 0, 0.13);
}
.form-group input {
background: none;
padding: 0.125rem 0.125rem 0.0625rem;
font-size: 1rem;
border-width: 0;
border-color: transparent;
transition: all 0.28s ease;
box-shadow: none;
height: 1.9rem;
}
.form-group .control-label {
order: -1;
pointer-events: none;
padding-left: var(--gutter);
font-weight: normal;
transition: all 0.28s ease;
}
.form-group .bar {
position: relative;
border-bottom: 0.0625rem solid #999;
display: block;
}
.form-group .bar::before {
content: '';
height: 0.125rem;
width: 0;
left: 50%;
bottom: -0.0625rem;
position: absolute;
background: var(--secondaryGreen);
transition: left 0.28s ease, width 0.28s ease;
z-index: 2;
}
.form-group select,
.form-group input:focus,
.form-group input:valid,
.form-group input.form-file,
.form-group input.has-value,
.form-group textarea:focus,
.form-group textarea:valid,
.form-group textarea.form-file,
.form-group textarea.has-value {
color: #333;
}
.form-group select ~ .control-label,
.form-group input:focus ~ .control-label,
.form-group input:valid ~ .control-label,
.form-group input.form-file ~ .control-label,
.form-group input.has-value ~ .control-label,
.form-group textarea:focus ~ .control-label,
.form-group textarea:valid ~ .control-label,
.form-group textarea.form-file ~ .control-label,
.form-group textarea.has-value ~ .control-label {
font-size: 0.8rem;
color: gray;
top: -1rem;
left: 0;
}
.form-group select ~ .control-label {
top: -1.5rem;
}
.form-group select:focus,
.form-group input:focus,
.form-group textarea:focus {
outline: none;
}
.form-group select:focus ~ .control-label,
.form-group input:focus ~ .control-label,
.form-group textarea:focus ~ .control-label {
color: var(--secondaryGreen);
}
.form-group select:focus ~ .bar::before,
.form-group input:focus ~ .bar::before,
.form-group textarea:focus ~ .bar::before {
width: 100%;
left: 0;
}
<div class="main">
<div class="title">Title</div>
<div>Details</div>
<div class="form-group">
<input type="text" id="feedName" name="name" value="" onChange={this.handleInputChange} />
<label class="control-label" htmlFor="feedName">Name</label>
<i class="bar"></i>
</div>
<div class="form-group">
<select id="feedKind" name="feedKind" onChange={this.handleInputChange}>
<option value="option 1">
Option 1
</option>
<option value="option 2">
Option 2
</option>
</select>
<label class="control-label" htmlFor="feedKind">Type</label>
<i class="bar"></i>
</div>