我正在为网站创建注册表单。我希望每个标签及其相应的输入元素出现在同一行。
这是我的代码:
HTML
<div id="form">
<form action="" method="post" name="registration" class="register">
<fieldset>
<label for="Student"> Name: </label>
<input name="Student" />
<label for="Matric_no"> Matric number: </label>
<input name="Matric_no" />
<label for="Email"> Email: </label>
<input name="Email" />
<label for="Username"> Username: </label>
<input name="Username" />
<label for="Password"> Password: </label>
<input name="Password" type="password" />
<input name="regbutton" type="button" class="button" value="Register" />
</fieldset>
</form>
</div>
CSS
#form {
background-color: #FFF;
height: 600px;
width: 600px;
margin-right: auto;
margin-left: auto;
margin-top: 0px;
border-top-left-radius: 10px;
border-top-right-radius: 10px;
padding: 0px;
}
label {
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 18px;
color: #333;
height: 20px;
width: 200px;
margin-top: 10px;
margin-left: 10px;
text-align: right;
clear: both;
}
input {
height: 20px;
width: 300px;
border: 1px solid #000;
margin-top: 10px;
float: left;
}
答案 0 :(得分:58)
假设您想要浮动元素,您还必须浮动label
元素。
这样的事情会起作用:
label {
/* Other styling... */
text-align: right;
clear: both;
float:left;
margin-right:15px;
}
#form {
background-color: #FFF;
height: 600px;
width: 600px;
margin-right: auto;
margin-left: auto;
margin-top: 0px;
border-top-left-radius: 10px;
border-top-right-radius: 10px;
padding: 0px;
text-align:center;
}
label {
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 18px;
color: #333;
height: 20px;
width: 200px;
margin-top: 10px;
margin-left: 10px;
text-align: right;
clear: both;
float:left;
margin-right:15px;
}
input {
height: 20px;
width: 300px;
border: 1px solid #000;
margin-top: 10px;
float: left;
}
input[type=button] {
float:none;
}
<div id="form">
<form action="" method="post" name="registration" class="register">
<fieldset>
<label for="Student">Name:</label>
<input name="Student" id="Student" />
<label for="Matric_no">Matric number:</label>
<input name="Matric_no" id="Matric_no" />
<label for="Email">Email:</label>
<input name="Email" id="Email" />
<label for="Username">Username:</label>
<input name="Username" id="Username" />
<label for="Password">Password:</label>
<input name="Password" id="Password" type="password" />
<input name="regbutton" type="button" class="button" value="Register" />
</fieldset>
</form>
</div>
或者,更常见的方法是将input
/ label
元素包装在组中:
<div class="form-group">
<label for="Student">Name:</label>
<input name="Student" id="Student" />
</div>
#form {
background-color: #FFF;
height: 600px;
width: 600px;
margin-right: auto;
margin-left: auto;
margin-top: 0px;
border-top-left-radius: 10px;
border-top-right-radius: 10px;
padding: 0px;
text-align:center;
}
label {
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 18px;
color: #333;
height: 20px;
width: 200px;
margin-top: 10px;
margin-left: 10px;
text-align: right;
margin-right:15px;
float:left;
}
input {
height: 20px;
width: 300px;
border: 1px solid #000;
margin-top: 10px;
}
<div id="form">
<form action="" method="post" name="registration" class="register">
<fieldset>
<div class="form-group">
<label for="Student">Name:</label>
<input name="Student" id="Student" />
</div>
<div class="form-group">
<label for="Matric_no">Matric number:</label>
<input name="Matric_no" id="Matric_no" />
</div>
<div class="form-group">
<label for="Email">Email:</label>
<input name="Email" id="Email" />
</div>
<div class="form-group">
<label for="Username">Username:</label>
<input name="Username" id="Username" />
</div>
<div class="form-group">
<label for="Password">Password:</label>
<input name="Password" id="Password" type="password" />
</div>
<input name="regbutton" type="button" class="button" value="Register" />
</fieldset>
</form>
</div>
请注意,for
attribute应与可标记元素的id
对应,而不是name
。这将允许用户单击label
以将焦点放在相应的表单元素上。
答案 1 :(得分:14)
AAA ## HTML 我建议你将它们包装在一个div中,因为你可能会在某些情况下将它们浮出水面。
<div class="input-w">
<label for="your-input">Your label</label>
<input type="text" id="your-input" />
</div>
然后在该div中,您可以制作每个inline-block
,以便您可以使用vertical-align
将它们居中 - 或设置基线等(您的标签和输入可能会在未来改变尺寸。
.input-w label, .input-w input {
float: none; /* if you had floats before? otherwise inline-block will behave differently */
display: inline-block;
vertical-align: middle;
}
这就是我最近做事的方式。
<label class='input-w' for='this-input-name'>
<span class='label'>Your label</span>
<input class='input' type='text' id='this-input-name' placeholder='hello'>
</label>
<label class='input-w' for='this-other-input-name'>
<span class='label'>Your label</span>
<input class='input' type='text' id='this-other-input-name' placeholder='again'>
</label>
html { // https://www.paulirish.com/2012/box-sizing-border-box-ftw/
box-sizing: border-box;
*, *:before, *:after {
box-sizing: inherit;
}
} // if you don't already reset your box-model, read about it
.input-w {
display: block;
width: 100%; // should be contained by a form or something
margin-bottom: 1rem;
@media (min-width: 500px) {
display: flex;
flex-direction: row;
align-items: center;
}
.label, .input {
display: block;
width: 100%;
border: 1px solid rgba(0,0,0,.1);
@media (min-width: 500px) {
width: auto;
display: flex;
}
}
.label {
font-size: 13px;
@media (min-width: 500px) {
/* margin-right: 1rem; */
min-width: 100px; // maybe to match many?
}
}
.input {
padding: .5rem;
font-size: 16px;
@media (min-width: 500px) {
flex-grow: 1;
max-width: 450px; // arbitrary
}
}
}
答案 2 :(得分:11)
我发现"display:flex"
样式是将这些元素放在同一行中的好方法。无论div中有什么元素。特别是如果输入类是表单控件,其他解决方案(如bootstrap,inline-block)将无法正常工作。
示例:
<div style="display:flex; flex-direction: row; justify-content: center; align-items: center">
<label for="Student">Name:</label>
<input name="Student" />
</div>
有关display:flex:
的更多细节flex-direction:行,列
对齐内容:flex-end,center,space-between,space-around
align-items:stretch,flex-start,flex-end,center
答案 3 :(得分:4)
你错过的是浮动:左;这是一个刚刚在HTML中完成的例子
<div id="form">
<form action="" method="post" name="registration" class="register">
<fieldset>
<label for="Student" style="float: left">Name:</label>
<input name="Student" />
<label for="Matric_no" style="float: left">Matric number:</label>
<input name="Matric_no" />
<label for="Email" style="float: left">Email:</label>
<input name="Email" />
<label for="Username" style="float: left">Username:</label>
<input name="Username" />
<label for="Password" style="float: left">Password:</label>
<input name="Password" type="password" />
<input name="regbutton" type="button" class="button" value="Register" />
</fieldset>
</form>
更有效的方法是在标签中添加一个类并设置float:left;到CSS中的类
答案 4 :(得分:3)
除了使用浮动之外,正如其他人所建议的那样,您还可以依赖Bootstrap之类的框架,您可以使用“水平形式”类将标签和输入放在同一行上。
如果您不熟悉Bootstrap,则需要包括:
这是非常直接的,如上所述,你不必乱用浮动或大量的CSS进行格式化。
<div id="form">
<div class="row">
<form action="" method="post" name="registration" class="register form-horizontal">
<fieldset>
<legend>Address Form</legend>
<div class="form-group">
<label for="Student" class="col-sm-2 control-label">Name:</label>
<div class="col-sm-6">
<input name="Student" class="form-control">
</div>
</div>
<div class="form-group">
<label for="Matric_no" class="col-sm-2 control-label">Matric number: </label>
<div class="col-sm-6">
<input name="Matric_no" class="form-control">
</div>
</div>
<div class="form-group">
<label for="Email" class="col-sm-2 control-label">Email: </label>
<div class="col-sm-6">
<input name="Email" class="form-control">
</div>
</div>
<div class="form-group">
<label for="Username" class="col-sm-2 control-label">Username: </label>
<div class="col-sm-6">
<input name="Username" class="form-control">
</div>
</div>
<div class="form-group">
<label for="Password" class="col-sm-2 control-label">Password: </label>
<div class="col-sm-6">
<input name="Password" type="password" class="form-control">
</div>
</div>
<div>
<button class="btn btn-info" name="regbutton" value="Register">Submit</button>
</div>
</fieldset>
</form>
</div>
</div>
</div>
答案 5 :(得分:2)
这个东西很好用,它把带有标签的单选按钮或复选框放在同一行中,没有任何CSS。
<label><input type="radio" value="new" name="filter">NEW</label>
<label><input type="radio" value="wow" name="filter">WOW</label>
答案 6 :(得分:1)
将标签和输入包裹在bootstraps div中
<div class ="row">
<div class="col-md-4">Name:</div>
<div class="col-md-8"><input type="text"></div>
</div>
答案 7 :(得分:1)
对于Bootstrap 4,可以使用class="form-group" style="display: flex"
<div class="form-group" style="display: flex">
<label>Topjava comment:</label>
<input class="form-control" type="checkbox"/>
</div>
答案 8 :(得分:0)
我已经通过几种不同的方式完成了这项工作,但我发现的唯一方法是将标签和相应的文本/输入数据保持在同一条线上,并始终完美地包裹在父线宽度上display:内联表。
CSS
.container {
display: inline-table;
padding-right: 14px;
margin-top:5px;
margin-bottom:5px;
}
.fieldName {
display: table-cell;
vertical-align: middle;
padding-right: 4px;
}
.data {
display: table-cell;
}
HTML
<div class='container'>
<div class='fieldName'>
<label>Student</label>
</div>
<div class='data'>
<input name="Student" />
</div>
</div>
<div class='container'>
<div class='fieldName'>
<label>Email</label>
</div>
<div class='data'>
<input name="Email" />
</div>
</div>
答案 9 :(得分:0)
#form {
background-color: #FFF;
height: 600px;
width: 600px;
margin-right: auto;
margin-left: auto;
margin-top: 0px;
border-top-left-radius: 10px;
border-top-right-radius: 10px;
padding: 0px;
text-align:center;
}
label {
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 18px;
color: #333;
height: 20px;
width: 200px;
margin-top: 10px;
margin-left: 10px;
text-align: right;
margin-right:15px;
float:left;
}
input {
height: 20px;
width: 300px;
border: 1px solid #000;
margin-top: 10px;
}
&#13;
<div id="form">
<form action="" method="post" name="registration" class="register">
<fieldset>
<div class="form-group">
<label for="Student">Name:</label>
<input name="Student" />
</div>
<div class="form-group">
<label for="Matric_no">Matric number:</label>
<input name="Matric_no" />
</div>
<div class="form-group">
<label for="Email">Email:</label>
<input name="Email" />
</div>
<div class="form-group">
<label for="Username">Username:</label>
<input name="Username" />
</div>
<div class="form-group">
<label for="Password">Password:</label>
<input name="Password" type="password" />
</div>
<input name="regbutton" type="button" class="button" value="Register" />
</fieldset>
</form>
</div>
&#13;
答案 10 :(得分:0)
我将Angular 6与Bootstrap 4结合使用,发现这种方式有效:
<div class="form-group row">
<div class="col-md-2">
<label for="currentPassword">Current Password:</label>
</div>
<div class="col-md-6">
<input type="password" id="currentPassword">
</div>
</div>
答案 11 :(得分:-3)
另一个选择是在表单中放置一个表。 (见下文)我知道有些人对表格不满意,但我认为它们在响应式表单布局方面效果很好。
<FORM METHOD="POST" ACTION="http://www.cs.tut.fi/cgi-bin/run/~jkorpela/echo.cgi">
<TABLE BORDER="1">
<TR>
<TD>Your name</TD>
<TD>
<INPUT TYPE="TEXT" NAME="name" SIZE="20">
</TD>
</TR>
<TR>
<TD>Your E-mail address</TD>
<TD><INPUT TYPE="TEXT" NAME="email" SIZE="25"></TD>
</TR>
</TABLE>
<P><INPUT TYPE="SUBMIT" VALUE="Submit" NAME="B1"></P>
</FORM>