Flexbox Form

时间:2017-08-24 16:52:02

标签: html css css3 flexbox

我试图在输入字段顶部叠加标签。问题是,我使用flex并且它似乎没有相同的工作方式。

我在Google上寻找解决方案,但似乎我能找到的大部分内容都与通过弹性定位对齐项目有关,但我需要将标签移动后并排排列。 。我已提供代码,以防我难以理解我需要的内容。

我也在这里寻找解决方案,但我还没有发现任何特定于我的问题。目前,我有覆盖字段的标签,但我无法让字段彼此对齐。此外,我相信你们中的一些人会注意到较小屏幕中的标签下方的字段。

所以,为了确保我澄清,我不确定。当标签与输入重叠时,我需要我的输入字段并排对齐。 (白板草图:https://sketchboard.me/dADoYPlJsIfT#/



/* ---- START RESET ---- */
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend, 
input, select, textarea, 
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
	margin: 0;
	padding: 0;
	vertical-align: baseline;
	box-sizing: border-box;
	font-weight: normal;
	font-style: normal;
	border: 0;
	outline: none;
	resize: unset;
	cursor: default; /* 2 */
}

/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
	display: block;
	box-sizing: border-box;
}

html {
	font-size: 62.5%;
	background-color: #eee;
	overflow-x: hidden;
}

html, html a {
	-webkit-font-smoothing: antialiased;
}

body {
	font-size: 1.6rem;
	font-family: OpenSansRegular, sans-seriff;
	color: #333;
	line-height: 1.25;
}

textarea {
	-moz-padding-end: 0px;
	-moz-padding-start: 0px;
}

input, 
textarea, 
select, 
button, 
label { 
	font-family: inherit; 
	font-size: inherit; 
	line-height:inherit;
}

button::-moz-focus-inner,
[type="button"]::-moz-focus-inner,
[type="submit"]::-moz-focus-inner, 
[type="reset"]::-moz-focus-inner, 
[type="menu"]::-moz-focus-inner {
	border: 0;
	padding: 0;
	margin: 0;
}
/* ---- END RESET ---- */

input, 
textarea, 
select, 
button, 
label,
span { 
	box-sizing: border-box;
	order: 0;
	align-self: center;
	border: 0px solid #333;
}

form {
	position: relative;
	display: flex;
	flex-direction: row;
	flex-wrap: wrap;
	justify-content: center;
	align-content: space-between;
	align-items: center;
	width: 80%;
	margin: auto;
	padding: 5rem;
  border: 1px solid #333;
}

input, 
textarea, 
select, 
button, 
label { 
	display: inline-block;
	flex: 0 0 50%;
  border: 1px solid #333;
}

label {
	z-index: 1;
  border: 0px solid #333;
}

input, 
textarea, 
select,
button, 
label { 
	position: relative;
	padding: .8rem;
}

input[type=text], 
input[type=email], 
input[type=url], 
input[type=tel], 
input[type=number], 
input[type=date], 
select, 
textarea {
	vertical-align: top;
	top: 0;
	left: -50%;
}





::placeholder {
	color: rgba(51, 51, 51, 0);
}

::-webkit-input-placeholder { /* WebKit browsers */
	color: rgba(51, 51, 51, 0);
}
:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
	color: rgba(51, 51, 51, 0);
}
::-moz-placeholder { /* Mozilla Firefox 19+ */
	color: rgba(51, 51, 51, 0);
}
:-ms-input-placeholder { /* Internet Explorer 10+ */
	color: rgba(51, 51, 51, 0);
}

textarea::-webkit-input-placeholder { /* WebKit browsers */
	color: rgba(51, 51, 51, 0);
}
textarea:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
	color: rgba(51, 51, 51, 0);
}
textarea::-moz-placeholder { /* Mozilla Firefox 19+ */
	color: rgba(51, 51, 51, 0);
}
textarea:-ms-input-placeholder { /* Internet Explorer 10+ */
	color: rgba(51, 51, 51, 0);
}

<h3>Form Name</h3>

<form class="form-horizontal">
		<!-- Text Input-->
		<label for="firstName">First Name</label> 
		<input type="text" id="firstName" name="firstName" placeholder="" /> 
		
    <!-- Text Input-->
		<label for="lastName">Last Name</label> 
		<input type="text" id="lastName" name="lastName" placeholder="" />
</form>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

他们在两个单独的行上的原因是因为,您正在使用标签的相对定位。相对定位确实会移动标签,但是如果你没有移动它们,它们仍会占用它们所占用的空间。你需要使用绝对定位。

你似乎已经确定了相对于几乎所有事情的位置。您需要将标签绝对定位到字段中。我建议将输入和标签环绕在具有相对位置的div中,然后使用绝对定位来定位标签。

form {
  display: flex
}

.form-field {
  position: relative
}

label {
  position: absolute;
  margin-left: 5px;
  top: 50%;
  transform: translate(0%, -50%)
}
<form>
  <div class="form-field">
    <label>Label1</label>
    <input type="text" />
  </div>
  <div class="form-field">
    <label>Label1</label>
    <input type="text" />
  </div>
</form>

答案 1 :(得分:1)

我刚刚为输入添加了负-50%右边距:(在全屏模式下测试)

/* ---- START RESET ---- */
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend, 
input, select, textarea, 
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
	margin: 0;
	padding: 0;
	vertical-align: baseline;
	box-sizing: border-box;
	font-weight: normal;
	font-style: normal;
	border: 0;
	outline: none;
	resize: unset;
	cursor: default; /* 2 */
}

/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
	display: block;
	box-sizing: border-box;
}

html {
	font-size: 62.5%;
	background-color: #eee;
	overflow-x: hidden;
}

html, html a {
	-webkit-font-smoothing: antialiased;
}

body {
	font-size: 1.6rem;
	font-family: OpenSansRegular, sans-seriff;
	color: #333;
	line-height: 1.25;
}

textarea {
	-moz-padding-end: 0px;
	-moz-padding-start: 0px;
}

input, 
textarea, 
select, 
button, 
label { 
	font-family: inherit; 
	font-size: inherit; 
	line-height:inherit;
}

button::-moz-focus-inner,
[type="button"]::-moz-focus-inner,
[type="submit"]::-moz-focus-inner, 
[type="reset"]::-moz-focus-inner, 
[type="menu"]::-moz-focus-inner {
	border: 0;
	padding: 0;
	margin: 0;
}
/* ---- END RESET ---- */

input, 
textarea, 
select, 
button, 
label,
span { 
	box-sizing: border-box;
	order: 0;
	align-self: center;
	border: 0px solid #333;
}

form {
	position: relative;
	display: flex;
	flex-direction: row;
	flex-wrap: wrap;
	justify-content: center;
	align-content: space-between;
	align-items: center;
	width: 80%;
	margin: auto;
	padding: 5rem;
  border: 1px solid #333;
}

input, 
textarea, 
select, 
button, 
label { 
	display: inline-block;
	flex: 0 0 50%;
  border: 1px solid #333;
}

label {
	z-index: 1;
  border: 0px solid #333;
}

input, 
textarea, 
select,
button, 
label { 
	position: relative;
	padding: .8rem;
}

input[type=text], 
input[type=email], 
input[type=url], 
input[type=tel], 
input[type=number], 
input[type=date], 
select, 
textarea {
	vertical-align: top;
	top: 0;
	left: -50%;
  margin-right: -50%;
}





::placeholder {
	color: rgba(51, 51, 51, 0);
}

::-webkit-input-placeholder { /* WebKit browsers */
	color: rgba(51, 51, 51, 0);
}
:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
	color: rgba(51, 51, 51, 0);
}
::-moz-placeholder { /* Mozilla Firefox 19+ */
	color: rgba(51, 51, 51, 0);
}
:-ms-input-placeholder { /* Internet Explorer 10+ */
	color: rgba(51, 51, 51, 0);
}

textarea::-webkit-input-placeholder { /* WebKit browsers */
	color: rgba(51, 51, 51, 0);
}
textarea:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
	color: rgba(51, 51, 51, 0);
}
textarea::-moz-placeholder { /* Mozilla Firefox 19+ */
	color: rgba(51, 51, 51, 0);
}
textarea:-ms-input-placeholder { /* Internet Explorer 10+ */
	color: rgba(51, 51, 51, 0);
}
<h3>Form Name</h3>

<form class="form-horizontal">
		<!-- Text Input-->
		<label for="firstName">First Name</label> 
		<input type="text" id="firstName" name="firstName" placeholder="" /> 
		
    <!-- Text Input-->
		<label for="lastName">Last Name</label> 
		<input type="text" id="lastName" name="lastName" placeholder="" />
</form>