Put the one box down exactly to the other - html

In html I have this form:
<form action="index.php" method="GET">
Name <input type="text" name="FirstName" size="10" /><br />
<br />
Surname: <input type="text" name="LastName" size="10" /><br />
<br />
</form>
It is a for with the first and last name. Is it possible to make the one box to be exactly down to the other. Because the first box is closer to name because it has less letters than the surname and respectively it takes the space?

Without using CSS simplest way to do this, is using Table of HTML.
Consider this Snippet:
<form action="index.php" method="GET">
<table>
<tr>
<td>Name </td>
<td>
<input type="text" name="FirstName" size="10" />
<br />
</td>
</tr>
<tr>
<td>Surname:</td>
<td>
<input type="text" name="LastName" size="10" />
</td>
</tr>
</table>
</form>

here's one way you could do it.
input{position:absolute;left:80px;}
<form action="index.php" method="GET">
<label>Name</label> <input type="text" name="FirstName" size="10" />
<br><br>
<label>Surname:</label> <input type="text" name="LastName" size="10" />
</form>

HTML:
<form action="index.php" method="GET">
<label>Name:</label><input type="text" name="FirstName" size="10" /><br />
<br />
<label>Surname:</label> <input type="text" name="LastName" size="10" /><br />
<br />
</form>
CSS:
label{
display: inline-block;
float: left;
clear: left;
width: 150px;
text-align: right;
}
input {
display: inline-block;
float: left;
}
Fiddle:http://jsfiddle.net/4ncwzLpx/

First, you should improve your HTML. <br /> are ugly and you should associate the text with the inputs using labels:
<form action="index.php" method="GET">
<label>
Name: <input type="text" name="FirstName" size="10" />
</label>
<label>
Surname: <input type="text" name="LastName" size="10" />
</label>
</form>
Now, you can try displaying the form as a table:
form {
display: table;
}
label{
display: table-row;
}
form {
display: table;
}
label{
display: table-row;
}
<form action="index.php" method="GET">
<label>
Name: <input type="text" name="FirstName" size="10" />
</label>
<label>
Surname: <input type="text" name="LastName" size="10" />
</label>
</form>
And now, to align the inputs,
You can align the content to the right:
label {
text-align: right;
}
form {
display: table;
}
label{
display: table-row;
text-align: right;
}
<form action="index.php" method="GET">
<label>
Name: <input type="text" name="FirstName" size="10" />
</label>
<label>
Surname: <input type="text" name="LastName" size="10" />
</label>
</form>
Or, if you want the text aligned to the left, you can float the inputs to the right:
input {
float: right;
}
form {
display: table;
}
label{
display: table-row;
}
input {
float: right;
}
<form action="index.php" method="GET">
<label>
Name: <input type="text" name="FirstName" size="10" />
</label>
<label>
Surname: <input type="text" name="LastName" size="10" />
</label>
</form>

Related

CSS way to align form's input elements, based on the widest label

Suppose I have a form:
<form action="#">
<p>
<label for="name">Name:</label>
<input type="text" placeholder="John Smith" />
</p>
<p>
<label for="name">What do you think about him?:</label>
<input type="text" placeholder="Your opinion" />
</p>
<p>
<label for="name">Another Text Input:</label>
<input type="text" placeholder="John Smith" />
</p>
</form>
With the following CSS:
form {
width: 400px;
}
p {
display: flex;
}
label {
margin-right: 10px;
}
input {
flex-grow: 1;
}
It looks the following way:
I need to align the right side of inputs, based on the widest label. That's what I want:
I don't know label's width beforehand, so bootstrap-like solutions with columns are not good here. Is there pure HTML/CSS way to achieve it, without using JS?
PS!!: And I don't want to use table
Remove the p and use CSS grid:
form {
width: 400px;
display:grid;
grid-template-columns:auto 1fr;
grid-gap:5px;
margin:10px;
}
input {
width:100%;
}
<form action="#">
<label for="name">Name:</label>
<input type="text" placeholder="John Smith" />
<label for="name">What do you think about him?:</label>
<input type="text" placeholder="Your opinion" />
<label for="name">Another Text Input:</label>
<input type="text" placeholder="John Smith" />
</form>
<form action="#" style="width:600px;">
<label for="name">Name:</label>
<input type="text" placeholder="John Smith" />
<label for="name">What do you think about him?:</label>
<input type="text" placeholder="Your opinion" />
<label for="name">Another Text Input:</label>
<input type="text" placeholder="John Smith" />
</form>
Or use display:contents with the p
form {
width: 400px;
display:grid;
grid-template-columns:auto 1fr;
grid-gap:5px;
margin:10px;
}
input {
width:100%;
}
p {
display:contents;
}
<form action="#">
<p>
<label for="name">Name:</label>
<input type="text" placeholder="John Smith" />
</p>
<p>
<label for="name">What do you think about him?:</label>
<input type="text" placeholder="Your opinion" />
</p>
<p>
<label for="name">Another Text Input:</label>
<input type="text" placeholder="John Smith" />
</p>
</form>
<form action="#" style="width:600px;">
<p>
<label for="name">Name:</label>
<input type="text" placeholder="John Smith" />
</p>
<p>
<label for="name">What do you think about him?:</label>
<input type="text" placeholder="Your opinion" />
</p>
<p>
<label for="name">Another Text Input:</label>
<input type="text" placeholder="John Smith" />
</p>
</form>
You should put the flex-grow on the label :) :
form {
width: 400px;
}
p {
display: flex;
}
label {
flex: 1;
margin-right: 10px;
white-space: nowrap;
}
<form action="#">
<p>
<label for="name">Name:</label>
<input type="text" placeholder="John Smith" />
</p>
<p>
<label for="name">What do you think about him?:</label>
<input type="text" placeholder="Your opinion" />
</p>
<p>
<label for="name">Another Text Input:</label>
<input type="text" placeholder="John Smith" />
</p>
</form>
You could simply organise it into a table. This will give you the formatting that you want.
Edit: Since you have specified that you don't want to use a table (and it would be better to avoid having to reorganise your html), how about styling your elements as if they were a table? I've changed my answer to keep your original html intact.
form {
display:table;
}
p {
display:table-row;
}
label, input {
display: table-cell;
}
<form action="#">
<p>
<label for="name">Name:</label>
<input type="text" placeholder="John Smith" />
</p>
<p>
<label for="name">What do you think about him?:</label>
<input type="text" placeholder="Your opinion" />
</p>
<p>
<label for="name">Another Text Input:</label>
<input type="text" placeholder="John Smith" />
</p>
</form>
The best way to do so is, creating a table with two columns.
<table>
<tr>
<td>label</td>
<td>input</td>
</tr>
<tr>
<td>label</td>
<td>input</td>
</tr>
</table>
Here is the working example:
form {
width: 400px;
}
.form-group {
display: flex;
align-items: center;
margin-bottom: 10px;
}
.form-group label {
margin-right: 10px;
flex: 1;
}
.form-group input {
min-width: 180px;
}
<form action="#">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" placeholder="John Smith" />
</div>
<div class="form-group">
<label for="name">What do you think about him?:</label>
<input type="text" placeholder="Your opinion" />
</div>
<div class="form-group">
<label for="name">Another Text Input:</label>
<input type="text" placeholder="John Smith" />
</div>
</form>

CSS-center text over a field

I'm horrible with CSS and was wondering how I could center text over input fields.
This is what I have:
.fieldset{
border: 1px solid rgb(255,232,57);
width: 400px;
margin:0px auto;
text-align:center;
}
what I get:
html:
<div class="fieldset" id="Display">
<form id="addNew" action="/RxCard/AddAccount" enctype="multipart/form-data" method="post" novalidate="novalidate"><input name="__RequestVerificationToken" type="hidden" value="gz4TQWfHkdBy6ClvES3plFN_RK4J8F3neJdgvSzTf3_eJX_pnvPvbN71UR8jrBlysSPWi3jHmx05s7svwr82TF1hmGwSDb5EgsODmmE6H6k1"> <fieldset>
<div class="form">
<label id="lblAccountName">Account Name</label>
<span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="Pharmacy.AccountName"></span>
<input name="Pharmacy.AccountName" id="Pharmacy_AccountName" type="text" value="" data-val-required="The account name is required." data-val="true">
<label id="lblAddress" style="margin: 5px;">Address</label>
<span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="Pharmacy.Address"></span>
<input name="Pharmacy.Address" id="Pharmacy_Address" type="text" value="" data-val-required="The address is required." data-val="true">
<label id="lblCity" style="margin: 5px;">City</label>
<span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="Pharmacy.City"></span>
<input name="Pharmacy.City" id="Pharmacy_City" type="text" value="" data-val-required="The city is required." data-val="true">
<label id="lblState" style="margin: 5px;">State</label>
<span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="Pharmacy.State"></span>
<input name="Pharmacy.State" id="Pharmacy_State" type="text" value="" data-val-required="The state is required." data-val="true">
<label id="lblZip" style="margin: 5px;">Zip</label>
<span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="Pharmacy.ZipCode"></span>
<input name="Pharmacy.ZipCode" id="Pharmacy_ZipCode" type="text" value="" data-val-required="The zip code is required." data-val="true" data-val-regex-pattern="^[-,0-9]+$" data-val-regex="Zip code can only contain numeric values.">
<label id="lblPhoneNumber" style="margin: 5px;">Phone Number (optional)</label>
<span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="Pharmacy.PhoneNumber"></span>
<input name="txtArea" class="valid" id="txtArea" aria-invalid="false" aria-describedby="txtArea-error" style="width: 5em; float: left;" onkeyup="tabout(this,'txtPrefix');" type="text" maxlength="3" value="">
<input name="txtPrefix" class="valid" id="txtPrefix" aria-invalid="false" aria-describedby="txtPrefix-error" style="width: 5em; float: left;" onkeyup="tabout(this,'txtSuffix');" type="text" maxlength="3" value="">
<input name="txtSuffix" class="valid" id="txtSuffix" aria-invalid="false" aria-describedby="txtSuffix-error" style="width: 5em; float: left;" type="text" maxlength="4" value="">
</div>
</fieldset>
<input type="submit" value="Save">
<input type="submit" value="Cancel">
</form> </div>
update:
I think I might have solved it by replacing "block" with a new class called "form" with properties "width:200px;" and "margin:0 auto;"? but I'd still like to get an opinion from the experts.
I'm a huge advocate of flexbox, but this is just me :)
form {
display: flex;
flex-direction: column;
align-items: center;
}
input {
width: 200px; /* SET THIS TO WHATEVER WIDTH */
}
input[type="submit"] {
margin-top: 25px;
}
/* BASIC STYLING */
<form action="#" method="post">
<label for="name">Text Input:</label>
<input type="text" name="name" id="name" value="" tabindex="1" />
<label for="name">Text Input:</label>
<input type="text" name="name" id="name" value="" tabindex="1" />
<label for="name">Text Input:</label>
<input type="text" name="name" id="name" value="" tabindex="1" />
<input type="submit" value="Submit" />
</form>
try this
input {
width: 100%;
}
UPD_
You wanted so much? It's simple:
label {
display: block;
}
input[type=text] {
margin:0px auto;
/* OR */
/* width: 100%; */
}
see here - https://jsfiddle.net/7znty0vb/2/

Aligning form with multilined label? (HTML/CSS)

I am wanting to make a label on a form that is much longer than the other labels in the form appear on multiple lines. I then want to align the inputs to the labels on the colons of the labels.
Here is a picture of the current set up:
Basically, I need Releasse Date to appear as
Release Date
(YYYY-MM-DD): [input box]
HTML Code:
<form action="http://localhost/songadded.php" method="post" id="songform">
<h4>Add a New Song</h4>
<div>
<label for="name">Name:</label>
<input type="text" name="name" size="30" value=""/>
</div>
<div>
<label for="artist">Artist:</label>
<input type="text" name="artist" size="30" value=""/>
</div>
<div>
<label for="album">Album:</label>
<input type="text" name="album" size="30" value=""/>
</div>
<div>
<label for="genre">Genre:</label>
<input type="text" name="genre" size="30" value=""/>
</div>
<div>
<label for="release_date" id="rdlabel">Release Date (YYYY-MM-DD):</label>
<input type="text" name="release_date" size="30" value="" id="rdinput"/>
</div>
<div>
<label for="bpm">BPM:</label>
<input type="text" name="bpm" maxlength="3" value=""/>
</div>
<div id="songsubmit">
<input type="submit" name="submit" value="Add Song"/>
</div>
</form>
CSS Code:
#songform {
margin: 20px;
}
label {
float: left;
width: 250px;
margin-top: 20px;
clear: right;
}
input{
margin-top: 20px;
}
#songsubmit {
margin-left: 80px;
}
Use display:inline-block and vertical-align:bottom. No floats or absolute positioning needed.
#songform {
margin: 20px;
}
#songform > div {
position: relative;
margin-top: 20px;
}
label {
display:inline-block;
width: 250px;
vertical-align:bottom;
}
#songsubmit {
margin-left: 80px;
}
<form action="http://localhost/songadded.php" method="post" id="songform">
<h4>Add a New Song</h4>
<div>
<label for="name">Name:</label>
<input type="text" name="name" size="30" value=""/>
</div>
<div>
<label for="artist">Artist:</label>
<input type="text" name="artist" size="30" value=""/>
</div>
<div>
<label for="album">Album:</label>
<input type="text" name="album" size="30" value=""/>
</div>
<div>
<label for="genre">Genre:</label>
<input type="text" name="genre" size="30" value=""/>
</div>
<div>
<label for="release_date" id="rdlabel">Release Date<br>(YYYY-MM-DD):</label>
<input type="text" name="release_date" size="30" value="" id="rdinput"/>
</div>
<div>
<label for="bpm">BPM:</label>
<input type="text" name="bpm" maxlength="3" value=""/>
</div>
<div id="songsubmit">
<input type="submit" name="submit" value="Add Song"/>
</div>
</form>
I've made a fiddle with code for your case: http://jsfiddle.net/862xc2j1/
You can solve it with adding clear:left to each div wrapper for each form field block.
<div class="form-item">
<label for="name">Name:</label>
<input type="text" name="name" size="30" value=""/>
</div>
.form-item {
clear: left;
}
To get this alignment next to the colon, you can use absolute positioning. Here is a working example - I made a few more changes to get it to work:
#songform {
margin: 20px;
}
#songform > div {
position: relative;
margin-top: 20px;
}
#songform > div:after {
/* Clearfix */
content:"";
display:table;
clear:both;
}
#songform > div > input {
position: absolute;
bottom: 0;
}
label {
float: left;
width: 250px;
clear: right;
}
#songsubmit {
margin-left: 80px;
}
<form action="http://localhost/songadded.php" method="post" id="songform">
<h4>Add a New Song</h4>
<div>
<label for="name">Name:</label>
<input type="text" name="name" size="30" value=""/>
</div>
<div>
<label for="artist">Artist:</label>
<input type="text" name="artist" size="30" value=""/>
</div>
<div>
<label for="album">Album:</label>
<input type="text" name="album" size="30" value=""/>
</div>
<div>
<label for="genre">Genre:</label>
<input type="text" name="genre" size="30" value=""/>
</div>
<div>
<label for="release_date" id="rdlabel">Release Date<br>(YYYY-MM-DD):</label>
<input type="text" name="release_date" size="30" value="" id="rdinput"/>
</div>
<div>
<label for="bpm">BPM:</label>
<input type="text" name="bpm" maxlength="3" value=""/>
</div>
<div id="songsubmit">
<input type="submit" name="submit" value="Add Song"/>
</div>
</form>

Trying to align input field with labels

I want to align the input fields shown below, with keep using <label> in a correct way.
The code is as below:
<form action="add.php" method="POST" enctype="multipart/form-data">
<label for="refNo">Field1 name (long)</label>
<input id="refNo" type="text" name="refNo" value="" /><br>
<label for="name">Field2 name</label>
<input id="name" type="text" name="name" value="" /><br>
<input type="submit" value=":: Add ::" name="addBtn" />
</form>
I am thinking to separate <label> tags in a <div> and the input fields in another and then with some floating manipulation I make the intended alignment, is this a correct way?
How about this
form > label {
min-width: 185px;
display: inline-block;
}
<form action="add.php" method="POST" enctype="multipart/form-data">
<label for="refNo">Field1 name (long)</label>
<input id="refNo" type="text" name="refNo" value="" /><br>
<label for="name">Field2 name</label>
<input id="name" type="text" name="name" value="" /><br>
<input type="submit" value=":: Add ::" name="addBtn" />
</form>
<style>
form{
width: 300px;
float: left;
}
form > label{
width: 50%;
float: left;
margin-bottom: 10px;
}
form > input{
float: right;
width: 50%;
margin-bottom: 10px;
}
</style>
<form action="add.php" method="POST" enctype="multipart/form-data">
<label for="refNo">Field1 name (long)</label>
<input id="refNo" type="text" name="refNo" value="" /><br>
<label for="name">Field2 name</label>
<input id="name" type="text" name="name" value="" /><br>
<input type="submit" value=":: Add ::" name="addBtn" />
</form>
For simple UI use this (Using Table):
<form action="add.php" method="POST" enctype="multipart/form-data">
<table>
<tr>
<td>
<label for="refNo">Field1 name (long)</label>
</td>
<td>
<input id="refNo" type="text" name="refNo" value="" />
</td>
</tr>
<tr>
<td>
<label for="name">Field2 name</label>
</td>
<td>
<input id="name" type="text" name="name" value="" /><br>
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value=":: Add ::" name="addBtn" />
</td>
</tr>
</table>
</form>
For More good looking and advanced UI try using Bootstrap.

Text isn't aligning with an object correctly in html

So I'm trying to make a form with a message box, but it won't align correctly; it keeps pushing the text to the bottom of the area it takes up..
I've accepted putting in a break between the text field and the message field for a long time, but there's a break, so it isn't aligned there either...
JSFiddle: http://jsfiddle.net/2U2PJ/
<form action="sending.php" method="post" name="Contact Form" id="form1" onsubmit="MM_validateForm('Name','','R','E-mail','','R','Device','','R','Message','','R')return document.MM_returnValue" encytype="text/plain">
<p>
<label><font size="3">Name: </font>
<input name="Name" type="text" id="Name" size="25" /><font size="2"> <font color="red"><span id="req_3" class="req">*</span></font></font></label>
</p>
<p>
<label><font size="3">Email:</font>
<input name="E-mail" type="text" id="E-mail" size="25" /> <font size="2"><font color="red"><span id="req_3" class="req">*</span></font></font></label>
</p>
<p>
<label><font size="3">Review:</font>
<textarea name="Message" cols="35" rows="7" id="Message" placeholder="Y U SO BROKE MR. THINGY D;"></textarea>
</p>
</label>
<label>
<input name="Submit" type="submit" id="Submit" value="Submit" />
</label>
</form>
As per my comment. Use CSS. This should get you on the correct path:
http://jsfiddle.net/2U2PJ/3/
css:
.contact-form .field label { width: 100px; display:inline-block; vertical-align: top; }
.contact-form .req { color: red; }
.contact-form .field .textbox { width: 180px; }
html:
<div class="contact-form">
<form action="sending.php" method="post" name="Contact Form" id="form1" onsubmit="MM_validateForm('Name','','R','Email','','R','Device','','R','Message','','R')return document.MM_returnValue" encytype="text/plain">
<div class="field">
<label for="Name">Name:</label>
<input name="Name" type="text" id="Name" class="textbox" />
<span id="req_3" class="req">*</span>
</div>
<div class="field">
<label for="Email">Email:</label>
<input name="Email" type="text" id="Email" class="textbox" />
<span id="req_3" class="req">*</span>
</div>
<div class="field">
<label for="Message">Review:</label>
<textarea name="Message" cols="35" rows="7" id="Message" placeholder="Y U SO BROKE MR. THINGY D;"></textarea>
</div>
<div>
<input name="Submit" type="submit" id="Submit" value="Submit" />
</div>
</form>
</div>
Full Code to simplify your issue!
http://jsfiddle.net/SinisterSystems/jLXsV/2/
HTML:
<form action="sending.php" method="post" name="Contact Form" id="form1" onsubmit="MM_validateForm('Name','','R','E-mail','','R','Device','','R','Message','','R')return document.MM_returnValue" encytype="text/plain">
<p>
<label>Name:</label>
<input type="text" />
</p>
<p>
<label>Email:</label>
<input type="text" />
</p>
<p>
<label>Review:</label>
<textarea rows="7" cols="35"></textarea>
</p>
<p>
<input name="Submit" type="submit" id="Submit" value="Submit" />
</p>
CSS:
input, textarea {
width:200px;
margin-left:15px;
}
label {
float:left;
min-width:100px;
}
With your IDs
Fiddle: http://jsfiddle.net/SinisterSystems/jLXsV/3/