我正在尝试使用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>
我对html结构做了一些更改,并添加了一些额外的样式以适应您的用例。我的解决方案也支持响应。
链接到工作示例:JSbin
这是代码:
* {
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 {
width: 100%;
justify-content: center;
display: flex;
flex-direction: column;
margin: 0 auto;
}
form .field {
margin: 15px 0;
/* flex-direction: column; */
display: flex;
position: relative;
}
form .combined{
display: flex;
flex-direction: row;
}
.form-input {
flex: 1;
}
form .combined .field{
flex:1;
justify-content: space-between;
/* border: 1px solid red; */
}
form .combined .field .form-input{
margin-right: 20px;
}
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%;
}
@media screen and (max-width: 520px) {
form .combined{
display: flex;
flex-direction: column;
}
form .combined .field{
flex:1;
justify-content: space-between;
margin: 25px 0;
}
form .combined .field label{
position: absolute;
top: -11px;
}
form .combined .field .form-input{
margin-right: 0;
}
}
<aside class="form-container">
<h3>Personal</h3>
<form>
<div class="combined">
<div class="field">
<label>First Name</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="field">
<label>Last Name</label>
<input type="text" name="name" class="form-input" />
</div>
</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>
将left: 50%
添加到姓氏label
- 这是有道理的,因为绝对定位的标签共享给予他们的全宽50-50到期flex: 1
。见下面的演示:
* {
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);
}
form .field label:nth-of-type(2) {
left: 50%; /* ADDED */
}
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>