My inputs and labels aren't inline - html

I know you can use display:inline but it doesn't seem to be working in this case. Maybe it has something to do with display:block for the inputs and labels.
My inputs and labels are moving down diagonally relative to one another.
See screencast: http://screencast.com/t/pFGzbRvTSy
input, label {
display: block;
margin-top: 5px;
vertical-align: top;
}
input {
display: inline;
float: left;
margin-right: 10px;
}
<label for="start age">START AGE</label>
<input type="number" name="start age"></input>
<label for="retirement age">RETIREMENT AGE</label>
<input type="number" name="retirement age"></input>
<label for="current income">CURRENT INCOME</label>
<input type="number" name="current income"></input>
<label for="inflation">INFLATION</label>
<input type="number" name="inflation"></input>
<label for="monthly disability benefit">MONTHLY DISABILITY BENEFIT</label>
<input type="number" name="monthly disability benefit"></input>

Yep, the explanation for this one is easy too, you have a display: inline property that gets overwritten by float: left since float: left makes your element a block again by setting display: block and applying other positioning.
One of the effects is that a floated element and a block element can exist in one line.
The other fact is that your label is display: block which will cause it to fill the entire screen width.
What happens then is the next element which is a float: left label gets pushed down one line and the label floats next to it taking up the rest of the horizontal space, this continues to create that stair effect that you're seeing.
To solve it and have the inputs and labels next to each other the best thing you could do is wrap each <input> and <label> within a <div> with a class and apply float: left to that div.
e.g.
.float-left {
max-width: 20%; /*added this for illustration*/
float: left;
}
label, input {
display: block;
padding: 4px;
}
<div class="float-left">
<label for="start_age">START AGE</label>
<input type="number" name="start_age" />
</div>
<div class="float-left">
<label for="retirement_age">START AGE</label>
<input type="number" name="retirement_age" />
</div>
<div class="float-left">
<label for="current_income">START AGE</label>
<input type="number" name="current_income" />
</div>
<div class="float-left">
<label for="inflamation">START AGE</label>
<input type="number" name="inflamation" />
</div>
<div class="float-left">
<label for="monthly_disability_benefit">START AGE</label>
<input type="number" name="monthly_disability_benefit" />
</div>
P.S.
I changed the for and name attributes of the fields to no longer have whitespace but using _ (underscore) characters instead - this just makes sure that your labels get understood correctly. It is usually a good idea to avoid spaces wherever you can with those kinds of things.
And as also stated in the comments, I removed the closing </input> tags as the <input /> tag is a self-closing empty tag

Oops sorry, I looked at the screencast and saw what you needed so I added that as well. (See second snippet at the bottom of this answer).
1 - 6 still apply.
In addition:
Add a <br/> after the text of the <label>
There's no closing tag for <inputs/> </input>
If you place your <inputs> inside your <label>, the text will line up in conjunction with vertical-align: baseline (see below)
I believe the for attribute targets id only, for doesn't associate with name.
float: left deleted, floats are archaic and delicate. If used incorrectly, they cause a horrific illogical mess of layouts (as it already has done to yours).
Made the width of inputs to the size of expected content (don't need 15 digits for age).
As a matter of aesthetics, don't use upper caps, I added a touch of class by using font-variant: small-caps ;-)
Relevant CSS
label {
display: inline-block;
margin: 5px;
vertical-align: baseline;
line-height: 1.5;
font-size: 16px;
font-variant: small-caps;
}
input {
display: inline-block;
line-height: 1.5;
padding: 0 3px;
}
I know this demo is just a demo, but if you get in the habit of doing little things, it becomes second nature and won't slow you down.
label {
display: inline-block;
margin: 5px;
vertical-align: baseline;
line-height: 1.5;
font-size: 16px;
font-variant: small-caps;
}
input {
display: inline-block;
line-height: 1.5;
padding: 0 3px;
}
#startAge {
width: 32px;
}
#retirementAge {
width: 32px;
}
#currentIncome {
width: 84px;
}
#inflation {
width: 64px;
}
#monthlyDisabilityBenefit {
width: 84px;
}
<label for="startAge">Start Age
<input type="number" id="startAge"></label>
<label for="retirementAge">Retirement Age
<input type="number" id="retirementAge"></label>
<br/>
<label for="currentIncome">Current Income
<input type="number" id="currentIncome"></label>
<br/>
<label for="inflation">Inflation
<input type="number" id="inflation"></label>
<br/>
<label for="monthlyDisabilityBenefit">Monthly Disability Benefit
<input type="number" id="monthlyDisabilityBenefit"></label>
label {
display: inline-block;
font-variant: small-caps;
}
input {
width: 185px;
}
<label for="startAge">Start Age
<br/>
<input type="number" id="startAge">
</label>
<label for="retirementAge">Retirement Age
<br/>
<input type="number" id="retirementAge">
</label>
<label for="currentIncome">Current Income
<br/>
<input type="number" id="currentIncome">
</label>
<label for="inflation">Inflation
<br/>
<input type="number" id="inflation">
</label>
<label for="monthlyDisabilityBenefit">Monthly Disability Benefit
<br/>
<input type="number" id="monthlyDisabilityBenefit">
</label>

i hope it will help i also added <br> tag with every line to get the the text box and label vertical
input, label {
margin-top: 5px;
}
input {
display: inline;
margin-right: 10px;
}
<label for="start age">START AGE</label><br>
<input type="number" name="start age"></input><br><br>
<label for="retirement age">RETIREMENT AGE</label><br>
<input type="number" name="retirement age"></input><br><br>
<label for="current income">CURRENT INCOME</label><br>
<input type="number" name="current income"></input><br><br>
<label for="inflation">INFLATION</label><br>
<input type="number" name="inflation"></input><br><br>
<label for="monthly disability benefit">MONTHLY DISABILITY BENEFIT</label><br>
<input type="number" name="monthly disability benefit"></input><br><br>

Related

What to use instead of the <br> tag?

I have a simple form like this:
<form method="post" action="/registration">
<label for="alias">Alias:</label>
<input type="text" name="alias" id="alias">
<br>
<label for="email">E-mail:</label>
<input type="text" name="email" id="email">
<br>
<input type="button" value="registger">
</form>
It works fine, but the I have found out that <br> shouldn't be used for this purpose, as it is only intended to be used with text.
If I remove the <br>, then everything will be rendered on a single line, which I do not want.
What is the correct, most clean way to display name-input pairs in a form with CSS, like this:
Alias: [__field__]
E-mail: [__field__]
[SUBMIT BUTTON]
I'd use divs, which will put the labels and inputs into their own block.
<form method="post" action="/registration">
<div>
<label for="alias">Alias:</label>
<input type="text" name="alias" id="alias">
</div>
<div>
<label for="email">E-mail:</label>
<input type="text" name="email" id="email">
</div>
<input type="button" value="registger">
</form>
I typically would put the input inside of the label (so when you click the label, it focuses the input), and then tell the label to be display: block;.
So,
<form method="post" action="/registration">
<label for="alias">
Alias: <input type="text" name="alias" id="alias">
</label>
<label for="email">
E-mail: <input type="text" name="email" id="email">
</label>
<input type="button" value="registger">
</form>
Then do:
label[for], // just selects labels that have the "for" attribute.
input[type="button"] {
display: block;
// And a bottom margin for good measure :)
margin: 0 0 10px; // shorthand for margin-bottom
}
And that should get you what you want.
You could use divs with corresponding CSS:
.myFrm {
width: 250px;
}
input[type=text] {
float: right;
}
.form-group {
margin-bottom: 10px;
}
.form-group::after {
content: "";
clear: both;
display: table;
}
<form method="post" action="/registration">
<div class="myFrm">
<div class="form-group">
<label for="alias">Alias:</label>
<input type="text" name="alias" id="alias">
</div>
<div class="form-group">
<label for="email">E-mail:</label>
<input type="text" name="email" id="email">
</div>
</div>
<input type="button" value="registger">
</form>
I would just use a bit of css to do the trick. Give each of the labels a display:block;
label {
display: block;
}
You can use container divs around the label and input to group them or else make sure "display: block" is added to the label and input elements.
If you need the label to the left of the input then wrap both with a container div and to give you more control on the positioning you could float the label and input to the left or use flexbox.
You ask:
What is the correct, most clean way to display name-input pairs in a
form with CSS
I interpret your question to be related to matters of performance, code efficiency and maintainability. Since just changing the HTML structure does not address responsiveness in different view-ports, adding bits of CSS may have render blocking features but it does nevertheless makes your application ready for mobile responsiveness. This is how I see it:
form {
display: inline-block;
}
label {
margin: 10px;
font-weight: 600;
}
input{
position: absolute;
left: 15%;
}
input[type=button]{
top: 4%;
}
Note that for mobile viewports you may want to adjust the relative measures with media queries. So the question here is not about writing less code but the main requirements of the application.
Get a plunk for this here
What you have there is a list.
https://developer.mozilla.org/en-US/docs/Learn/HTML/Introduction_to_HTML/HTML_text_fundamentals#Lists
http://reisio.com/temp/form1.html

Why I can't change font size for label?

I have little problem and don't know why it doesn't work. I tried to change font size for label tag, but I can't set less than 10px. Why?
.group {
margin-bottom: 20px;
}
.group label {
font-size: 10px;
}
.group2 label {
font-size: 8px;
}
<div class="group">
<label for="input1">First label</label>
<input type="text" name="input1">
</div>
<div class="group group2">
<label for="input2">Second label</label>
<input type="text" name="input2">
</div>
I have tested this one on my side and it works as intended.
If you are loading the styles via a stylesheet it is possible that your browser has cached this, and you may need to clear browser caches.
Some browsers do also supply default values for most HTML elements. (Something to consider)
EDIT: It turned out that cached CSS in Opera was responsible non-updating styling.
Working as required. Remember to re-run your code snippet after making changes.
.group {
margin-bottom: 20px;
}
.group label {
font-size: 10px;
}
.group2 label {
font-size: 4px;
}
<div class="group">
<label for="input1">First label</label>
<input type="text" name="input1">
</div>
<div class="group group2">
<label for="input2">Second label</label>
<input type="text" name="input2">
</div>

Radiobutton Style don't show up as expected

I have a very basic form, but I used a third party css which seems to give me a problem. I looked for it pretty long now in this CSS but cannot find anything what cause this problem,
Mevr.<input type="radio" name="gender" value="Mevr.">
Dhr.<input type="radio" name="gender" value="Dhr.">
Fam.<input type="radio" name="gender" value="Fam.">
this is what I expect
With the third party CSS active I get this:
My question is: What inline style I can use to get my radiobuttons next to eachother like in the first picture?
note:
the css what cause the problem is here:
As you have not given the coding.You can try this:
<style>
.some-class {
float: left;
clear: none;
}
label {
float: left;
clear: none;
display: block;
padding: 2px 1em 0 0;
}
input[type=radio],
input.radio {
float: left;
clear: none;
margin: 2px 0 0 2px;
}
</style>
<div class="some-class">
<input type="radio" class="radio" name="x" value="y" id="y" />
<label for="y">Thing 1</label>
<input type="radio" class="radio" name="x" value="z" id="z" />
<label for="z">Thing 2</label>
</div>
Since you didn't include the CSS in your post it's hard to say what the problem is.
A simple guess would be that the inputs have a display property of block. This would cause the line break and possibly other UI changes (as seen in your screenshots).
Using display: inline-block; would solve this.
body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,textarea,p,blockquote,th {
margin:0;
padding:0;
}
input[type="radio"] {
display: inline-block;
}
Mevr.<input type="radio" name="gender" value="Mevr.">
Dhr.<input type="radio" name="gender" value="Dhr.">
Fam.<input type="radio" name="gender" value="Fam.">
EDIT As per your updated question, the problems seems to be with the width of you input tags.
You should try setting your width property to auto
input {
border: 1px solid #b0b0b0;
padding: 3px 5px 4px;
color: #979797;
width: auto;
}
Mevr.<input type="radio" name="gender" value="Mevr.">
Dhr.<input type="radio" name="gender" value="Dhr.">
Fam.<input type="radio" name="gender" value="Fam.">
Just replace it with the existing one in your CSS.
On a side note. I see that your CSS has multiple declarations throughout its content. Consider going through it and removing unnecessary declarations.
I have used dummy HTML. Try this:
<div style="float: left">
<input id="RadioButtonList1_0" name="RadioButtonList1" value="One" type="radio"><label for="RadioButtonList1_0">One</label>
<input id="RadioButtonList1_1" name="RadioButtonList1" value="Two" type="radio"><label for="RadioButtonList1_1">Two</label>
<input id="RadioButtonList1_2" name="RadioButtonList1" value="Three" type="radio"><label for="RadioButtonList1_2">Three</label>
</div>

HTML/CSS: style form labels to display on the same line and inline with form fields

I have looked on here, but none of the answers have worked for me.
I have 2 form fields in one line, i want to style the form labels to also be on one line and also be inline with the correct form field.
The two methods i have used have either made the form fields inline and also the labels, but one of the labels is not inline, no matter if i try using margins or padding, it just would not move past a certain px/%. (Method 1)
The other makes the labels inline, but once again one of the labels was unable to move past a certain px/% and it messed up the form fields alignmnet. (Method 2)
Method 1
label{
text-transform: uppercase;
font-size: 0.8em;
font-weight: bold;
margin-left: 11px
}
label.right_lab{
display:inline-block;
}
label#lname{
float: right;
margin-left: 60% !important;
}
<label for="fname" class="right_lab">First name</label>
<label for="lname" class="right_lab">Last name</label><br>
<input type="text" id='fname' name="fname" value="" placeholder="e.g Joe">
<input type="text" id='lname' name="lname" value="" placeholder="e.g.Bloggs"><br>
Method 2
label{
text-transform: uppercase;
font-size: 0.8em;
font-weight: bold;
margin-left: 11px
}
label.right_lab{
display:inline-block;
}
label#lname{
float: right;
margin-left: 60% !important;
}
<label for="fname" class="right_lab">First name</label>
<input type="text" id='fname' name="fname" value="" placeholder="e.g Joe">
<label for="lname" class="right_lab">Last name</label><br>
<input type="text" id='lname' name="lname" value="" placeholder="e.g.Bloggs"><br>
Method three:
label {
display: inline-block;
}
label>input {
display: block;
}
<label>
First name
<input type="text" />
</label>
<label>
Last name
<input type="text" />
</label>

Creating form to have fields and text next to each other - what is the semantic way to do it?

I want to create a form so there is text on the left side and the inputs on the right, currently I am doing
<div id="labels">
<ul>
<li>The Label</li>
</ul>
</div>
<div id="inputs">
<ul>
<li><input type="text /></li>
</ul>
</div>
And the CSS
input[type=text] {
height: 14px;
}
#labels {
float: left;
}
#inputs {
float: right;
}
li {
padding-top: 4px;
padding-left: 10px;
}
// Text size is 14px
What happens is that the text and fields are not aligned perfectly (the inputs get progressively lower as I add items). I am thinking this is because not all the inputs can be 14px (I use drop downs, checkboxes, radios, etc.).
What would be the correct way to create this? I know a table would fix the problem but is that semantic?
This sort of question has been asked multiple times here in SO, you can do a simple search and find many solutions.
But here is a simple form to get you started:
HTML
<form>
<div class="line">
<label for="input">Full Name</label>
<div class="input">
<input type="text" size="30" name="input">
</div>
</div>
<div class="line">
<label for="input">Company</label>
<div class="input">
<input type="text" size="30" name="input">
</div>
</div>
<div class="line">
<label for="nselect">Dropdown Menu</label>
<div class="input">
<select name="select">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>
</div>
<div class="line">
<label for="input">Text 1</label>
<div class="input">
<input type="text" size="30" name="input">
</div>
</div>
<div class="line">
<label for="input">Text 2</label>
<div class="input">
<input type="text" size="30" name="input">
</div>
</div>
<div class="line">
<label for="input">Text 3</label>
<div class="input">
<input type="text" size="15" name="input">
</div>
</div>
</form>
CSS
form {
margin:10px 0;
}
label {
color: #404040;
float: left;
font-size: 13px;
line-height: 18px;
padding-top: 6px;
text-align: right;
width: 130px;
}
label, input, select, textarea {
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
font-size: 13px;
font-weight: normal;
line-height: normal;
}
input, textarea, select {
-moz-border-radius: 3px 3px 3px 3px;
border: 1px solid #CCCCCC;
color: #808080;
display: inline-block;
font-size: 13px;
height: 18px;
line-height: 18px;
padding: 4px;
width: 210px;
}
select {
height: 27px;
line-height: 27px;
}
form .input {
margin-left: 150px;
}
form .line {
margin-bottom: 18px;
}
Here is a demo: http://jsfiddle.net/5aduZ/1/
A lot of people will not agree with my use of divs to separate the form elements but through testing i found this format to be the safest and surefire way to go about it as it separates the fields cleanly, and it works just fine under IE. Plus, it is the format used by the big boys (facebook, twitter, google).
It makes sense for the label to be next to the input in the HTML - it's easier to read and more maintainable. Typical HTML for this would be:
<div class="fieldWrapper">
<label for="something">Something</label>
<input type="text" id="something" name="something">
</div>
<div class="fieldWrapper">
<label for="something">Something</label>
<input type="text" id="something" name="something">
</div>
And CSS would be:
label, input {
float:left;
}
input {
font-size:14px;
padding: 2px; // instead of using fixed height
}
label {
width: 100px; // can use JavaScript if it needs to be dynamic
padding-top: 3px; // to make the label vertically inline with the input element
}
.fieldWrapper {
clear:left;
}
If you really can't change your HTML, you could set a CSS height on the <li> tag to fix the alignment problem. But I strongly recommend you to choose one of other proposed solutions, because your HTML is very hard to read in its current state. And you should use the <label> tag.
Write this <input type="text" name="firstname" /> and set the height width and padding
At my company, way back when we first started our first web application back in 2001, we used a table.
<table class="formTable">
<tbody>
<tr>
<td><label>Name:</label></td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td><label>E-mail:/label></td>
<td><input type="text" name="email" /></td>
</tr>
</tbody>
</table>
And while this works, philosophically I don't like the approach, because as far as I am concerned, a table should hold table-ized data.
You could use CSS and DIV's, as well:
<style>
.formLabel, .formInput {
display:inline-block;
}
</style>
<div class="formField">
<div class="formLabel"><label>Name:</label></div>
<div class="formInput"><input type="text" name="name" /></div>
</div>
<div class="formField">
<div class="formLabel"><label>E-Mail:</label></div>
<div class="formInput"><input type="text" name="email" /></div>
</div>
See here: http://jsfiddle.net/9P7pg/
Or, you could avoid the use of div's all together, and just apply the display: inline-block for each label and input (or use classes). But then you will also have to remember to use a breaking space for carriage returns in between the label-field combination.
there is a special list for this actually! it's called definition list (dl) and is comprised of definition terms and definition definitions (dt/dd). i usually put the text in the dt and the input box in the dd. like this:
<form action="bla">
<dl>
<dt>Name*</dt>
<dd><input type="text" name="name" />
<dt>Email</dt>
<dd><input type="text" name="email" />
</dl>
<p><input type="submit" /></p>
</form>