Positioning elements in a form using CSS - html

I'm a beginner to CSS, and i want to know the best technique to position elements in a form using only CSS. I just can't get my two buttons to align probably beside each other...
Here's my HTML code :
<form action="creat_fam.php" method ="post" >
<div id="formWrapper">
<label for="fam_name">Family Name : </label>
<input type="text" placeholder="Family Name" name="fam_name" required>
<br/>
<label for="people_count">People count : </label>
<input type="number" name="people_count" min="1" value="1" required>
<br/>
<label for="location">Location : </label>
<input type="text" name="location" placeholder="location" required>
<br/>
<input type="reset" value="Reset">
<input type="submit" name="submit" value="Done">
</div>
</form>
And my CSS code :
#formWrapper {
width : 400px;
padding : 15px;
-webkit-box-sizing : border-box;
}
#formWrapper label {
float:left;
margin-bottom : 15px;
line-height: 25px;
}
#formWrapper input[type="text"] , #formWrapper input[type="number"] {
float :right;
margin-bottom : 10px;
padding : 2px;
width : 250px;
height: 25px;
border:1px solid gray;
}
#formWrapper input[name="submit"] , #formWrapper input[type="reset"] {
margin : 0;
width : 90px;
height: 40px;
border : none;
border-radius : 4px;
background : -webkit-gradient(linear , 0 0, 0 100% , from(#7f7f7f) ,to(#535353));
color : lightgray;
font-weight: bold;
cursor : pointer;
}
And all i get is this :
Thanks in advance.

Although inputs are inline elements, your second button is going to the next line because of the location input. The location input takes up a little bit of invisible space (the margin-bottom) next to the first button, causing the second button to wrap.

Adding float:left; into #formWrapper input[name="submit"] , #formWrapper input[type="reset"] {
} will align the boxes horizontally, the i would put the buttons in their own div and play around with margins so they align right.

I realise this is an older question, but it is prominent in search results, so hopefully this will help others.
Using developer tools, one can see that the container will appear much smaller than the total height of the collective fields. To avoid the need for adjusting margins to 'make it fit', wrap the buttons in their own div (e.g. #buttonsDiv) as suggested by Jack, but apply the CSS of clear:both to the div instead.
Any formatting performed on the buttons will then apply independently from the content above, allowing the buttons to be positioned next to each other effortlessly.

Related

How to add space between label and input and make input go to right edge of container in same line?

I'm completely new at html/CSS and I can't seem to be able to find how to both add spacing between labels and input while making the input extend to the right edge of it's enclosing container and have both the label and input be on the same line.
so far I have:
label {
text-align: right;
clear: both;
float:left;
margin-right:25px;
}
input {
width: 100%;
}
<div class="container">
<form action="home.html">
<label for="label">Label:</label>
<input type="text" id="rcorners1" name="field_name">
<input type="submit" value="submit" class="btn btn-primary btn-block">
</form>
</div>
This makes the label appear above the input which makes sense because I set the width to 100%. What is a good solution? I do not want to set the width to a specific pixel size, but rather have the width always extend from the label to the right edge of the container no matter the size of the label itself.
I understand similar questions may have been asked before but I can't find a solution so thank you very much for your help.
You may try:
label {
text-align: right;
float:left;
margin-right:25px;
width:100px;
}
input {
width: calc(100% - 150px);
float:left;
}
with your HTML
<label for="label">Label:</label>
<input type="text" id="rcorners1" name="field_name">
See it on jsfiddle
Note: you can set the with of the label as you like

Huge amount of space in between radio button and selection text

I am front-end stupid and can't ever figure this stuff out. For some reason I'm seeing an abnormal amont of space in between my radio button and the text.
look on bottom of page
I am using bootstrap and I feel as if it is doing this. How can I get that space to go away? There's no margin or anything on it current which is why I am a little confused.
<div class="radio">
<h4>By Price</h4>
<label><input type="radio" name="optradio"> Low </label>
<br />
<label><input type="radio" name="optradio"> High </label>
You're labels are floating left which means it's being pulled to the far left of your container. I would recommend wrapping your content in columns. So something like this would work...
<div class="radio col-sm-3">
<h4>By Price</h4>
<label><input type="radio" name="optradio"> Low </label>
<br />
<label><input type="radio" name="optradio"> High </label>
</div>
If your content is then not centred, I would add this to your CSS...
.radio {
margin: 0 auto;
}
That should centre your content.
.radio{
width: 100px;
margin-left: auto;
margin-right: auto;
}
Also use a different div class.
<div class="radio col-sm-3">
I believe it's because the Div with class "radio" has a default width of 100%. When I change the css for .radio to width: 100px (or a percentage of your choice) then the radio button is much closer to the text. You will also have to center the div with the margin-left and margin-right as follows
.radio{
width: 100px;
margin-left: auto;
margin-right: auto;
}
As stated, the radio buttons are floated left in CSS.
This is a CSS rule meaning they're being taken out of the flow of the document.
The result is that they're positioned relative to the parent div, instead of according to their relationship to other block-level elements (the labels). That's pushing them all the way to the left of the form element.
In your external CSS file (not "products.css" but the one w/the gigantic hashed name) find this line:
.radio input[type="radio"], .radio-inline input[type="radio"], .checkbox input[type="checkbox"], .checkbox-inline input[type="checkbox"] {
float: left;
margin-left: -20px;
}
The offending code is float: left;
Remove the float and the inputs should rest directly next to the text.
Play with the margin settings to position it as you like.
I would recommend using:
<input type="radio" name="optradio"> Low <br />
<input type="radio" name="optradio"> High
I'm pretty sure it will work.
I had the same problem, Later i found out it was because i created a class for input like,
.input
{
width: 200px;
border: 1px solid #000;
padding: 5px;
}
Later i edited the input with some other name like,
input.a1
{
width: 200px;
border: 1px solid #000;
padding: 5px;
}
and the problem solved for me;

How To Properly Space A Contact Form In A Table?

I'm trying to properly add spacing between the s in my table contact form here: http://jsfiddle.net/k6XSp/1/.
I would like to have the fields on the right to line up with the width of the message box on the right side, thus meaning I need space added in between the s in each of the two s.
It is looking good, but I can't figure out how to space the right sides of the fields. Adding a margin-right seems to do the trick, but not very well, as it is very glitchy.
In addition, the form moves around when you click on the filed because the border shows up.
The CSS for the text fields looks like this:
#contact-area input, #contact-area textarea {
padding: 5px;
width: 451px;
font-family: Helvetica, sans-serif;
font-size: 1.4em;
margin: 0px 0px 10px 0px;
border: none;
background-color: #dedede;
height: 40px;
}
Take a look at this fiddle http://jsfiddle.net/k6XSp/6/. As suggested, try not to use tables to position elements. Use css instead.
<div class="control-group">
<label for="FirstName" class="text">First Name:</label>
<div class="control">
<input type="text" name="FirstName" id="FirstName" />
</div>
</div>
.control-group {width: auto; float: left; margin: 0 10px 0 0}
.control {display: block; clear: both;}
Forms use fieldset, legend and label tags, not tables, unless you need to code HTML from the 90's. The idea is to be semantically correct e.g. your HTML should reflect it's meaning/purpose.
<form action="contactengine.php" method="POST">
<fieldset>
<legend>personal data</legend>
<label for="firstname">First name</label>
<input id="firstname" placeholder="First name" />
</fieldset>
<input type="submit" value="submit my form" />
</form>
Spacing these fields is part of your markup => CSS.
See fiddle for a responsive/liquid (desktop, tablet, mobile) approach.
Note:
I see you use the 62.5% font base approach. We've tested with this on a big project and noticed IE has a floating point issue ending up with a different font-base than 10px. In my opinion just put it to 10px and be done with it => rem values can now be used in modern browsers.

Word-wrap of Label text in text-area

I want to re-size the label of a text-area in the format shown in the picture below. I'm trying to do a wrap using a paragraph tag, but it is not working.
My code:
<label for="qual">This is the format i want the text-area to be displayed:</label>
<textarea id="qual" rows="5" cols="50" style="resize:none" placeholder="Description and Qualification"></textarea><br><br>
Desired Output:
style="max-width: 140px; word-wrap: break-word"
place these styles in your label tag and adjust the max-width or min-width to your needs.
This doesn't work in Internet Explorer
Try white-space: normal;
For more details visit
http://www.w3schools.com/cssref/pr_text_white-space.asp
Here is a JSFiddle solution.
The HTML:
<label for="qual" class="abc">This is the format i want the text-area to be displayed:</label>
<textarea id="qual" rows="5" cols="50" style="resize:none" placeholder="Description and Qualification"></textarea>
The CSS:
.abc{float:left; width:125px; text-align:left; margin-right:30px;}
If you don't want to create a class, you can apply the styles on label in the CSS.
Something like this:
label {
float: left;
width:120px;
padding:10px 30px;
font-weight:bold;
}
Demo
In the block of your HTML add:
<style>
label { padding:5px; font-weight:bold; float:left; width:150px; }
</style>
The style settings above will also replace the spacing:
<label for="qual">This is the format i want the text-area to be displayed:</label>
<textarea id="qual" rows="5" cols="50" style="resize:none" placeholder="Description and Qualification"></textarea>
You want to style the label and textarea elements in a self-contained, bullet-proof manner.
I suggest the following HTML:
<div class="wrap">
<label for="qual">This is the format...to be displayed:</label>
<textarea id="qual" rows="5" cols="50" style="resize:none"
placeholder="Description and Qualification"></textarea>
</div>
with the following CSS:
.wrap {
outline: 1px dotted blue; /* Just for demonstration... */
overflow: auto;
}
.wrap label {
outline: 1px dashed blue;
float:left;
width: 10em;
margin-right: 1.00em;
}
Define a parent container div.wrap to hold the two child elements and specify overflow: auto to generate a new block formatting context.
You want to do this to make sure that the floated label field does not interfere with any other adjacent elements that may be in the page or form. This may be important if you are building a responsive design.
The <label> field is floated left and you must specify a suitable width. You can also apply margins, background color or images, padding as needed to create the design that you need.
Fiddle: http://jsfiddle.net/audetwebdesign/2sTez/

Vertical Align text in a Label

I have been asked to vertically align the text in the labels for the fields in a form but I don't understand why they are not moving. I have tried putting in-line styles using vertical-align:top; and other attributes like bottom and middle but it doesn't work.
Any ideas?
<dd>
<label class="<?=$email_confirm_class;?>"
style="text-align:right; padding-right:3px">Confirm Email</label>
<input class="text" type="text"
style="border:none;" name="email_confirm"
id="email_confirm" size="18" value="<?=$_POST['email_confirm'];?>"
tabindex="4" />
*
</dd>
You can use flexbox in 2018+:
.label-class {
display: flex;
align-items: center;
}
Browser support: https://caniuse.com/#search=flexbox
Vertical alignment only works with inline or inline-block elements, and it's only relative to other inline[-block] elements. Because you float the label, it becomes a block element.
The simplest solution in your case is to set the label to display: inline-block and add vertical-align: middle to the labels and the inputs. (You might find that the height of the text is such that vertical align won't make any difference anyway.)
Have you tried line-height? It won't solve your problems if there are multiple row labels, but it can be a quick solution.
The vertical-align style is used in table cells, so that won't do anything for you here.
To align the labels to the input boxes, you can use line-height:
line-height: 25px;
I had a similar problem and solved it wrapping the label into a div and setting the following styles:
<div style="display: table; vertical-align: middle">
<label style="display: table-cell;" ... > ... </label>
</div>
This is what I usually do to "vertical align" text inside labels:
label {
display: block;
float: left;
padding-top: 2px; /*This needs to be modified to fit */
}
It won't scale very nicely, but it works.
I came across this trying to add labels o some vertical radio boxes. I had to do this:
<%: Html.RadioButton("RadioGroup1", "Yes") %><label style="display:inline-block;padding-top:2px;">Yes</label><br />
<%: Html.RadioButton("RadioGroup1", "No") %><label style="display:inline-block;padding-top:3px;">No</label><br />
<%: Html.RadioButton("RadioGroup1", "Maybe") %><label style="display:inline-block;padding-top:4px;">Maybe</label><br />
This gets them to display properly, where the label is centered on the radio button, though I had to increment the top padding with each line, as you can see. The code isn't pretty, but the result is.
label {
padding: 10px 0;
position: relative;
}
Add some padding-top and padding-bottom instead of height.
Use css on your label.
For example:
label {line-height:1em; margin:2px 5px 3px 5px; padding:2px 5px 3px 5px;}
Notice that the line-height will adjust the height of the line itself, whereas margin will dictate how far out other elements will be outside the lable and padding will dictate any inner space from the outside edge of the label. The margin and padding work like this (clockwise: Top Right Bottom Left), so 2px 5px 3px 5px is:
2px Top
5px Right
3px Bottom
5px Left
To do this you should alter the vertical-align property of the input.
<dd><label class="<?=$email_confirm_class;?>" style="text-align:right; padding-right:3px">Confirm Email</label><input class="text" type="text" style="vertical-align: middle; border:none;" name="email_confirm" id="email_confirm" size="18" value="<?=$_POST['email_confirm'];?>" tabindex="4" /> *</dd>
Here is a more complete version. It has been tested in IE 8 and it works.
see the difference by removing the vertical-align: middle from the input:
<html><head></head><body><dl><dt>test</dt><dd><label class="test" style="text-align:right; padding-right:3px">Confirm Email</label><input class="text" type="text" style="vertical-align: middle; font-size: 22px" name="email_confirm" id="email_confirm" size="28" value="test" tabindex="4" /> *</dd></dl></body></html>
Adding disply:flex property to the label will get the job done!
None of these worked for me. I am using ASP.Net MVC with Bootstrap.
I used the following successfully:
.label-middle {
padding-top:6px;
}
<label id="lblX" class="label-middle" ></label>
This vertically aligned the label with the textbox next to it.
If your label is in table, padding may cause it to expand. To avoid this you may use margin:
div label {
display: block;
text-align: left;
margin-bottom: -0.2%;
}
You don't have to add any padding or edit the line-height!
Instead, just make sure that when you have an HTML like this :
<label><input type="checkbox" name=""><span>Checkbox Text</span></label>
Just make sure that the input height and width are the same, and the the text has the same font size.
Then, it will look perfectly fine and looks centered.
You have this:
<label class="styling_target">Label Text</label>
<input />
Do this instead:
<label>
<span class="styling_target">Label Text</span>
<input />
</label>
Styling a label doesn't really work, but you can have arbitrary HTML inside it, and you can style that.
Force relative positions to provide top/bottom adjustments
.whatever {
position: relative;
}
.whatever .input {
position: relative;
}
.whatever span {
position: relative;
top: -2px; /* adjust this up or down */
}
<label class="whatever">
<input type="checkbox"><span>my thing</span>
</label>
Just set the vertical-align property of the label to top.
label {
vertical-align: top;
}
<label for="desc">Description</label>
<textarea name="desc" id="desc" cols="30" rows="10"></textarea>
Lacking in elegance, pure html, no CSS solution:
<table>
<tr>
<td>
<label></label>
</td>
</tr>
<tr>
<td>
<input></input>
</td>
</tr>
</table>