I am creating a registration form for a website. I want each label and its corresponding input element to appear on the same line.
Here's my code:
#form {_x000D_
background-color: #FFF;_x000D_
height: 600px;_x000D_
width: 600px;_x000D_
margin-right: auto;_x000D_
margin-left: auto;_x000D_
margin-top: 0px;_x000D_
border-top-left-radius: 10px;_x000D_
border-top-right-radius: 10px;_x000D_
padding: 0px;_x000D_
}_x000D_
_x000D_
label {_x000D_
font-family: Georgia, "Times New Roman", Times, serif;_x000D_
font-size: 18px;_x000D_
color: #333;_x000D_
height: 20px;_x000D_
width: 200px;_x000D_
margin-top: 10px;_x000D_
margin-left: 10px;_x000D_
text-align: right;_x000D_
clear: both;_x000D_
}_x000D_
_x000D_
input {_x000D_
height: 20px;_x000D_
width: 300px;_x000D_
border: 1px solid #000;_x000D_
margin-top: 10px;_x000D_
float: left;_x000D_
}
_x000D_
<div id="form">_x000D_
_x000D_
<form action="" method="post" name="registration" class="register">_x000D_
_x000D_
<fieldset>_x000D_
_x000D_
<label for="Student"> Name: </label>_x000D_
<input name="Student" />_x000D_
<label for="Matric_no"> Matric number: </label>_x000D_
<input name="Matric_no" />_x000D_
<label for="Email"> Email: </label>_x000D_
<input name="Email" />_x000D_
<label for="Username"> Username: </label>_x000D_
<input name="Username" />_x000D_
<label for="Password"> Password: </label>_x000D_
<input name="Password" type="password" />_x000D_
_x000D_
<input name="regbutton" type="button" class="button" value="Register" />_x000D_
</fieldset>_x000D_
_x000D_
</form>_x000D_
</div>
_x000D_
#form {_x000D_
background-color: #FFF;_x000D_
height: 600px;_x000D_
width: 600px;_x000D_
margin-right: auto;_x000D_
margin-left: auto;_x000D_
margin-top: 0px;_x000D_
border-top-left-radius: 10px;_x000D_
border-top-right-radius: 10px;_x000D_
padding: 0px;_x000D_
text-align:center;_x000D_
}_x000D_
label {_x000D_
font-family: Georgia, "Times New Roman", Times, serif;_x000D_
font-size: 18px;_x000D_
color: #333;_x000D_
height: 20px;_x000D_
width: 200px;_x000D_
margin-top: 10px;_x000D_
margin-left: 10px;_x000D_
text-align: right;_x000D_
margin-right:15px;_x000D_
float:left;_x000D_
}_x000D_
input {_x000D_
height: 20px;_x000D_
width: 300px;_x000D_
border: 1px solid #000;_x000D_
margin-top: 10px;_x000D_
}
_x000D_
<div id="form">_x000D_
<form action="" method="post" name="registration" class="register">_x000D_
<fieldset>_x000D_
<div class="form-group">_x000D_
<label for="Student">Name:</label>_x000D_
<input name="Student" />_x000D_
</div>_x000D_
<div class="form-group">_x000D_
<label for="Matric_no">Matric number:</label>_x000D_
<input name="Matric_no" />_x000D_
</div>_x000D_
<div class="form-group">_x000D_
<label for="Email">Email:</label>_x000D_
<input name="Email" />_x000D_
</div>_x000D_
<div class="form-group">_x000D_
<label for="Username">Username:</label>_x000D_
<input name="Username" />_x000D_
</div>_x000D_
<div class="form-group">_x000D_
<label for="Password">Password:</label>_x000D_
<input name="Password" type="password" />_x000D_
</div>_x000D_
<input name="regbutton" type="button" class="button" value="Register" />_x000D_
</fieldset>_x000D_
</form>_x000D_
</div>
_x000D_
Aside from using floats, as others have suggested, you can also rely on a framework such as Bootstrap where you can use the "horizontal-form" class to have the label and input on the same line.
If you're unfamiliar with Bootstrap, you would need to include:
It's very straight forward and you wouldn't have to mess with floats or a ton of CSS for formatting, as you listed above.
<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>
I've done this several different ways but the only way I've found that keeps the labels and corresponding text/input data on the same line and always wraps perfectly to the width of the parent is to use display:inline table.
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>
What you were missing was the float: left; here is an example just done in the 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>
The more efficient way to do this is to add a class to the labels and set the float: left; to the class in CSS
I found "display:flex"
style is a good way to make these elements in same line. No matter what kind of element in the div. Especially if the input class is form-control,other solutions like bootstrap, inline-block will not work well.
Example:
<div style="display:flex; flex-direction: row; justify-content: center; align-items: center">
<label for="Student">Name:</label>
<input name="Student" />
</div>
More detail about display:flex:
flex-direction: row, column
justify-content: flex-end, center, space-between, space-around
align-items: stretch, flex-start, flex-end, center
This thing works well.It put radio button or checkbox with label in same line without any css.
<label><input type="radio" value="new" name="filter">NEW</label>
<label><input type="radio" value="wow" name="filter">WOW</label>
I am using Angular 6 with Bootstrap 4 and find this way works:
<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>
aaa##HTML I would suggest you wrap them in a div, since you will likely end up floating them in certain contexts.
<div class="input-w">
<label for="your-input">Your label</label>
<input type="text" id="your-input" />
</div>
Then within that div, you can make each piece inline-block
so that you can use vertical-align
to center them - or set baseline etc. (your labels and input might change sizes in the future...
.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;
}
This is how I do things these days.
<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
}
}
}
For Bootstrap 4 it could be done with class="form-group" style="display: flex"
<div class="form-group" style="display: flex">
<label>Topjava comment:</label>
<input class="form-control" type="checkbox"/>
</div>
Another option is to place a table inside the form. (see below) I know tables are frowned upon by some people but I think they work nicely when it comes to responsive form layouts.
<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>
Wrap the label and the input within a bootstraps div
<div class ="row">
<div class="col-md-4">Name:</div>
<div class="col-md-8"><input type="text"></div>
</div>
Source: Stackoverflow.com