我正在尝试使用flex创建一个响应表单,其中名字和姓氏在大型设备中位于同一列,在小型设备中位于单独的列中。我设计了表单,它按预期工作(虽然不是响应部分),直到我调整了输入框中的标签。我用了
position: absolute
对标签进行调整,打破了名和姓字段的设计。他们现在合并了。
这是演示
http://jsbin.com/pecijupicu/3/edit?html,css
同时,这里是源代码
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
html,
body {
background: #f2f3f78a;
-webkit-font-smoothing: antialiased;
}
aside.form-container {
display: flex;
flex-direction: column;
flex: 1;
padding: 20px;
background: #fff;
box-shadow: 0 0 10px 0 #f0efef;
margin: 0 40px;
border-radius: 6px;
}
.form-container h3 {
color: rgb(67, 77, 90);
}
form {
min-width: 400px;
width: 600px;
justify-content: center;
display: flex;
flex-direction: column;
margin: 0 auto;
}
form .field {
margin: 15px 0;
/* flex-direction: column; */
display: flex;
position: relative;
}
.form-input {
flex: 1;
}
label {
position: absolute;
top: 4px;
}
form .field label {
margin: 10px 0;
color: rgb(67, 77, 90);
}
button.next {
padding: 10px 0;
width: 100px;
background: rgb(0, 213, 229);
border: none;
display: flex;
justify-content: center;
}
button.next:after {
content: "Next";
color: #fff;
}
input[type="text"],
textarea {
padding: 8px;
border: transparent;
border-bottom: solid 1px #999;
}
input[type="text"]:focus,
textarea:focus {
outline: none;
border-bottom: solid 1px rgb(0, 213, 229);
}
textarea {
width: 100%;
}
<aside class="form-container">
<h3>Personal</h3>
<form>
<div class="field">
<label>First Name</label>
<input type="text" name="name" class="form-input" />
<label>Last Name</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="field">
<label>Email</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="field">
<label>Country</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="field">
<label>City</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="field">
<label>Description</label>
<textarea></textarea>
</div>
<!-- show button to the bottom right corner -->
<button class="next"></button>
</form>
</aside>