我正在为一个网站创建一个注册表单。我希望每个标签及其对应的输入元素出现在同一行。
这是我的代码:
#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;
}
<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>
原文由 luchxo 发布,翻译遵循 CC BY-SA 4.0 许可协议
假设您想浮动元素,您还必须浮动
label
元素。像这样的东西会起作用:
或者,更常见的方法是将
input
/label
元素包装在组中:请注意,
for
属性 应对应于可id
元素的 --- ,而不是其name
。这将允许用户单击label
将焦点放在相应的表单元素上。