how to align button radio in css - html

I am trying to align the button radios on my page and it seems that one of the buttons (Gender) is highly indented. The radio for the M (Gender) is at the center and the other button is at the far right. I've been changing the values in css but it doesn't align. How do I align them? Anyone help? My css is combined and I got most of help here from SO.
form {
width: 80%;
margin: 0 auto;
}
label,
input {
display: inline-block;
}
label {
width: 30%;
}
label + input {
width: 30%;
margin: 0 10% 0 1%;
}
.label-align input[type="checkbox"] {
cursor: pointer;
margin-top: 6px;
}
.label-align span {
margin: 0 0 10px 10px;
display: block;
}
.left {
float: left;
}
<div id="register" class="container">
<form action="" method="post">
<label>*First Name:</label>
<input type="text">
<br>
<label>*Last Name:</label>
<input type="text">
<br>
<label>*Birth Date:</label>
<input type="text">
<br>
<label>*Gender:</label>
<input type="radio" name="gender" value="male" checked>M
<input type="radio" name="gender" value="female" checked>F
<br>
<label>*Email:</label>
<input type="text">
<br>
<label>*Password:</label>
<input type="text">
<br>
<label>*Re-enter Password:</label>
<input type="text">
<br>
<button class="button" type="submit" value="Submit" onclick="" style="vertical-align:middle"><span>Submit</span>
</button>
</form>
</div>

This is happening because of this the following style.
label + input {
width: 30%;
margin: 0 10% 0 1%;
}
All inputs following a label will gain a width of 30%.
The reason only the first radio-button is affected by this is because it's the one that's adjacent to the gender label.
You could change this rule so it only affects input boxes for example.
label + input[type='text'] {
width: 30%;
margin: 0 10% 0 1%;
}
Snippet example
form {
width: 80%;
margin: 0 auto;
}
label,
input {
display: inline-block;
}
label {
width: 30%;
}
label + input[type='text'] {
width: 30%;
margin: 0 10% 0 1%;
}
.label-align input[type="checkbox"] {
cursor: pointer;
margin-top: 6px;
}
.label-align span {
margin: 0 0 10px 10px;
display: block;
}
.left {
float: left;
}
<div id="register" class="container">
<form action="" method="post">
<label>*First Name:</label>
<input type="text">
<br>
<label>*Last Name:</label>
<input type="text">
<br>
<label>*Birth Date:</label>
<input type="text">
<br>
<label>*Gender:</label>
<input type="radio" name="gender" value="male" checked>M
<input type="radio" name="gender" value="female" checked>F
<br>
<label>*Email:</label>
<input type="text">
<br>
<label>*Password:</label>
<input type="text">
<br>
<label>*Re-enter Password:</label>
<input type="text">
<br>
<button class="button" type="submit" value="Submit" onclick="" style="vertical-align:middle"><span>Submit</span>
</button>
</form>
</div>

label + input {
width: 30%;
margin: 0 10% 0 1%;
}
Remove width :30%;

Please add following css,
input[type="radio"] {
margin: 4px 4px 4px 4px;
width: 2%;
}

In your code add one class like this
<input type="radio" name="gender" class="gender" value="male" checked>M
<input type="radio" name="gender" value="female" checked>F<br>
in your css style property add this class gender property like this.
.gender{
width: 0px !important;
margin: 0px 0px 0px 4px !important;
}

Related

HTML/CSS: How to make the certain input fields on the same line in the form?

I'm working on a hotel reservation webpage, and having trouble aligning the input/select fields. For example, my current code shows the first name and the last name in two different lines, but I want to have them all together. This is my form looks like with my code:
first name
last name
address 1
address 2
city
state
zip
And below is how I wanted it to be:
first name last name <<----
address 1
address 2
city state <<----
zip
From my research I was able to do similarly by using display: inline-block, so I tried using it in my code as below, but it does not change anything. What am I doing wrong here?
#mainContainer {
width: 1139px;
display: flex;
justify-content: center;
padding: 0;
margin: auto;
text-align: center;
}
#formContainer {
max-width: 1000px;
width: 100%;
height: 100%;
margin-top: 110px;
background-color: white;
}
#contact {
padding-top: 25px;
}
#customerInformationForm {
width:50%;
float:left;
margin-bottom: 50px
}
#contact input {
width: 70%;
border: 1px solid #ccc;
background: #FFF;
margin: 0 0 5px;
padding: 10px;
}
#contact select {
width: 70%;
border: 1px solid #ccc;
background: #FFF;
margin: 0 0 5px;
padding: 10px;
}
#contact input [class="customerFullName"] {
display: inline-block;
width: 50%;
}
<div id="mainContainer">
<div id="formContainer">
<form id="contact" action="" method="post">
<div id="customerInformationForm">
<input class="customerFullName" placeholder="First name" type="text">
<input class="customerFullName" placeholder="Last name" type="text">
<input placeholder="Address 1" type="text">
<input placeholder="Address 2" type="text">
<input placeholder="City" type="text">
<select id="state" name="state">
<option value="State" selected>State</option>
<option value="Alabama">AL</option>
<option value="Alaska">AK</option>
<option value="Arizona">AZ</option>
</select>
<input placeholder="ZIP" type="text">
</div>
</form>
</div>
</div>
You made a little mess about all those "width" declarations. You made your div #customerInformationForm "width" for a half of a parent (50% width). Then you inserted in that div your first, last name etc. inputs, and set up their width for 70% of the parent, which actually made no possible, to insert two inputs side by side (70% + 70% equals more than 100%, so it displays in new line). Reconsider using all these width declarations, below you have a little start how you may handle it.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
#mainContainer {
width: 1139px;
display: flex;
justify-content: center;
padding: 0;
margin: auto;
text-align: center;
}
#formContainer {
max-width: 1000px;
width: 100%;
height: 100%;
margin-top: 110px;
background-color: white;
}
#contact {
padding-top: 25px;
}
#customerInformationForm {
/* width:50%;*/
float:left;
margin-bottom: 50px;
background-color: red;
}
#contact input {
width: 35%;
border: 1px solid #ccc;
background: #FFF;
margin: 0 0 5px;
padding: 10px;
}
#contact input:nth-child(3),
#contact input:nth-child(4) {
width: 70%;
}
#contact select {
width: 35%;
border: 1px solid #ccc;
background: #FFF;
margin: 0 0 5px;
padding: 10px;
}
#contact #customerInformationForm input .customerFullName {
display: inline-block;
width: 70%;
}
</style>
</head>
<body>
<div id="mainContainer">
<div id="formContainer">
<form id="contact" action="" method="post">
<div id="customerInformationForm">
<input class="customerFullName" placeholder="First name" type="text">
<input class="customerFullName" placeholder="Last name" type="text">
<input placeholder="Address 1" type="text">
<input placeholder="Address 2" type="text">
<input placeholder="City" type="text">
<select id="state" name="state">
<option value="State" selected>State</option>
<option value="Alabama">AL</option>
<option value="Alaska">AK</option>
<option value="Arizona">AZ</option>
</select>
<input placeholder="ZIP" type="text">
</div>
</form>
</div>
</div>
</body>
</html>
Use semantic tags like fieldset to you advantage here to group elements. I also champion the proper use of label, not hijacking the place holder attribute for that purpose.
The example below could use a bit of style tidying but it will give you the idea.
It uses flexbox to achieve inlining the field where required.
#customerInformationForm {
padding-top:2em;
}
fieldset {
border: none;
position:relative;
}
#customerInformationForm fieldset {
padding-left:0;
}
#customerInformationForm {
background-color:#DDD;
}
#customerInformationForm > fieldset {
background-color:#EEE;
padding: 1.5em 1em;
margin-bottom: 0.5em;
border-radius:5px;
}
input, select {
width:100%;
}
legend {
font-weight: bold;
padding-left: 0;
position:absolute;
top:0;
}
label {
display: block;
}
.flex {
display:flex;
align-items:stretch;
}
.flex > .form_group {
flex:1;
}
.form_group {
margin-right:10px;
}
<div id="mainContainer">
<div id="formContainer">
<form id="contact" action="" method="post">
<fieldset id="customerInformationForm">
<legend>Customer Information</legend>
<fieldset class="customer_name flex">
<legend>Customer Name</legend>
<div class="form_group">
<label for="firstName">First Name</label>
<input class="customerFullName" id="firstName" placeholder="Eg: John" type="text">
</div>
<div class="form_group">
<label for="lastName">Last Name</label>
<input class="customerFullName" placeholder="Eg: Smith" id="lastName" type="text">
</div>
</fieldset>
<fieldset class="address">
<legend>Address</legend>
<div class="form_group">
<label for="address1">Address 1</label>
<input type="text" id="address1">
</div>
<div class="form_group">
<label for="address1">Address 2</label>
<input type="text">
</div>
<fieldset class="city_state flex">
<div class="form_group">
<label for="City">City</label>
<input type="text" id="City">
</div>
<div class="form_group">
<label for="state">State</label>
<select id="state" name="state">
<option value="" selected></option>
<option value="Alabama">AL</option>
<option value="Alaska">AK</option>
<option value="Arizona">AZ</option>
</select>
</div>
</fieldset>
<div class="form_group">
<label for="zip">Zip</label>
<input id="zip" type="text">
</div>
</fieldset>
</fieldset>
</form>
</div>
</div>

How to add vertical lines in text box to separate elements

I have the following CSS and HTML code for a simple form, however, I don't can't get vertical lines that will separate my text from my radio buttons, currently, I am just using the '|' in the body to separate it, but it doesn't look nice. How can I get the vertical lines to connect all the way from the top of the text box to the bottom?
<!DOCTYPE html>
<html>
<head>
<style>
.box1 {
width: 300px;
height: 20px;
padding: 5px;
border: 1px solid black;
margin: 0;
float: left
}
input[type=radio] {
display: inline;
}
</style>
</head>
<body>
<div class="box1">
<form method="GET" action="." target="new">
Up Down |
<input type="radio" name="option" id="r1" value="1" />
<label for="r1">Up</label>
<input type="radio" name="option" id="r2" value="2" />
<label for="r2">Down</label>
|
<input type="submit" />
</form>
</div>
</body>
</html>
I personally wouldn't use table elements for layout in this case (unless I am rendering a bunch of data in a table, then it applies) because its not semantic. Instead, flexbox can handle this easily.
display: flex;
.box1 form {
width: 300px;
border: 1px solid black;
margin: 0;
display: flex;
}
.col {
display: inline-block;
border-right: 1px solid black;
padding: 5px;
}
.col:last-child {
border-right: none;
}
.col.input-control {
padding-right: 20px;
}
<div class="box1">
<form method="GET" action="." target="new">
<label class="col">Up Down</label>
<span class="col input-control">
<input type="radio" name="option" id="r1" value="1" />
<label for="r1">Up</label>
<input type="radio" name="option" id="r2" value="2" />
<label for="r2">Down</label>
</span>
<span class="col">
<input type="submit" />
</span>
</form>
</div>
You need to create a table and inside put the html inputs, labels.
Also you need to add CSS style to the table for a better visualization.
You can wrap your radiobuttons into a div element and add borders to left and right.
https://jsfiddle.net/krbp8tx7/
.box1 {
height: 20px;
padding: 5px;
border: 1px solid black;
margin: 0;
float: left
}
input[type=radio] {
display: inline;
}
.separateradios {
display: inline-block;
border-left:1px solid #000;
border-right:1px solid #000;
padding: 0 0.2em;
}
<body>
<div class="box1">
<form method="GET" action="." target="new">
Up Down
<div class="separateradios">
<input type="radio" name="option" id="r1" value="1" />
<label for="r1">Up</label>
<input type="radio" name="option" id="r2" value="2" />
<label for="r2">Down</label>
</div>
<input type="submit" />
</form>
</div>
</body>

How to side by side align two input bars in html and css?

I would like to side by side align two input bars in html and css. This is to make up a form. I would like something like this.side by side bars
So given that you dont have anything yet I'm just gonna put out an answer:
label { width: 200px; float: left; margin: 0 20px 0 0; }
span { display: block; margin: 0 0 3px; font-size: 1.2em; font-weight: bold; }
input { width: 200px; border: 1px solid #000; padding: 5px; }
<form>
<label for="email">
<span>Email</span>
<input type="email" id="email" placeholder="Email" />
</label>
<label for="password">
<span>password</span>
<input type="password" id="password" placeholder="Password"/>
</label>
</form>
Hopes this helps

creating a form in html

im trying to create a form in html css. this is the form i am trying to replicate [1]: http://i.stack.imgur.com/mITNz.png
but i cant seem to get it looking like the required format as in the picture above.
this is my html
*{
margin: 0;
padding: 0;
}
label {
display: block;
float: left;
width: 6em;
}
input, textarea {
width: 14em;
display: block;
}
input[type="radio"] {
width: 1em;
margin: .1em .2em;
}
<!DOCTYPE html >
<html>
<head>
<link rel="stylesheet" type="text/css" href="pracExam2.css">
</head>
<body>
<form action="#" method="get" >
<label>Given name</label><input type="text" name="givenName" /><br/>
<label>Family name</label><input type="text" name="familyName" /><br/>
<label>eMail</label><input type="email" name="eMail" /><br/>
<label>Address</label><textarea rows="4" name="address"></textarea><br/>
<label>Service</label>
<fieldset>
<label for="service1">Basic</label><input type="radio" name="service" id="service1" value="basic" />
<label for="service2">Professional</label><input type="radio" name="service" id="service1" value="professional" />
<label for="service3">Premium</label><input type="radio" name="service" id="service1" value="premium" />
</fieldset>
</form>
<input type="submit" value = "submit form" /><br/>
</body>
</html>
if anybody could help me get this right that would be great
You were almost there. Just add a float: left; to your radio inputs:
LIVE DEMO
input[type="radio"] {
width: 1em;
margin: .1em .2em;
float:left;
}
There are two 'extra' points to note:
All ID's should be unique
Your submit button may be more 'commonly accepted' as being nested within the form, not after it.
With those amendments changed, you should have something similar to this
*{
margin: 0;
padding: 0;
}
label {
display: block;
float: left;
width: 6em;
}
input, textarea {
width: 14em;
display: block;
}
input[type="radio"] {
width: 1em;
margin: .1em .2em;
}
<!DOCTYPE html >
<html>
<head>
<link rel="stylesheet" type="text/css" href="pracExam2.css">
</head>
<body>
<form action="#" method="get" >
<label>Given name</label><input type="text" name="givenName" /><br/>
<label>Family name</label><input type="text" name="familyName" /><br/>
<label>eMail</label><input type="email" name="eMail" /><br/>
<label>Address</label><textarea rows="4" name="address"></textarea><br/>
<label>Service</label>
<fieldset>
<label for="service1">Basic</label><input type="radio" name="service" id="service1" value="basic" />
<label for="service2">Professional</label><input type="radio" name="service" id="service1" value="professional" />
<label for="service3">Premium</label><input type="radio" name="service" id="service1" value="premium" />
</fieldset>
</form>
<input type="submit" value = "submit form" /><br/>
</body>
</html>
Why don't you try http://getbootsrtap.com and i thing this example will help you
http://getbootstrap.com/css/#forms-horizontal
I've changed the width of the label inside the fieldset and added float:left label and input elements.
* {
margin: 0;
padding: 0;
}
label {
display: block;
float: left;
width: 6em;
}
input,
textarea {
width: 14em;
display: block;
}
input[type="radio"] {
width: 1em;
margin: .1em .2em;
}
form {
border: solid grey;
}
fieldset label,
fieldset input {
float: left;
}
fieldset label {
width: auto;
}
<form action="#" method="get">
<label>Given name</label>
<input type="text" name="givenName" />
<br/>
<label>Family name</label>
<input type="text" name="familyName" />
<br/>
<label>eMail</label>
<input type="email" name="eMail" />
<br/>
<label>Address</label>
<textarea rows="4" name="address"></textarea>
<br/>
<label>Service</label>
<fieldset>
<label for="service1">Basic</label>
<input type="radio" name="service" id="service1" value="basic" />
<label for="service2">Professional</label>
<input type="radio" name="service" id="service1" value="professional" />
<label for="service3">Premium</label>
<input type="radio" name="service" id="service1" value="premium" />
</fieldset>
</form>
<input type="submit" value="submit form" />
<br/>
You need to have block elements
html
<form action="#" method="get" >
<label>Given name</label><input type="text" name="givenName" /><br/>
<label>Family name</label><input type="text" name="familyName" /><br/>
<label>eMail</label><input type="email" name="eMail" /><br/>
<label>Address</label><textarea rows="4" name="address"></textarea><br/>
<label>Service</label>
<fieldset style="display:inline-block;">
<div class="fieldset_blocks"><label for="service1">Basic<input type="radio" name="service" id="service1" value="basic" /></label></div>
<div class="fieldset_blocks"><label for="service2">Professional<input type="radio" name="service" id="service1" value="professional" /></label></div>
<div class="fieldset_blocks"><label for="service3">Premium<input type="radio" name="service" id="service1" value="premium" /></label></div>
</fieldset>
</form>
<input type="submit" value = "submit form" /><br/>
css
*{
margin: 0;
padding: 0;
}
label {
display: block;
float: left;
padding:5px 10px;
}
input, textarea {
width: 14em;
display: block;
}
input[type="radio"] {
width: 1em;
margin: .1em .2em;
}
form {
border: solid grey;
display:inline-block;
}
.fieldset_blocks{
display:inline-block;
}
.fieldset_blocks label,.fieldset_blocks input{
display:inline-block;
}
Check this fiddle
http://jsfiddle.net/8kj177et/1/
Try this CSS.
It has the proper aligments as per the reference image
*{
margin: 0px;
padding: 0px;
}
label {
display: block;
float: left;
width: 6em;
text-align:right;
margin-right:10px;
}
input[type="text"], input[type="email"], textarea {
width: 80%;
display: block;
}
input[type="radio"] {
width: 1em;
margin: .2em .1em;
float:left;
}
form {
border: solid grey;
padding:10px;
}
Check this DEMO
Try this Out I have add some css and html codes.
*{
margin: 0;
padding: 0;
}
label {
display: block;
float: left;
width: 6em;
}
input, textarea
{
width: 14em;
display: block;
}
input[type="radio"]
{
width: 1em;
margin: .1em .2em;
float: left;
}
form
{
border: solid grey;
width: 350px;
padding-top: 10px;
padding-left: 10px;
}
HTML code
<html>
<head>
<link rel="stylesheet" type="text/css" href="pracExam2.css">
</head>
<body>
<form action="#" method="get" >
<label>Given name</label><input type="text" name="givenName" /><br/>
<label>Family name</label><input type="text" name="familyName" /><br/>
<label>eMail</label><input type="email" name="eMail" /><br/>
<label>Address</label><textarea rows="4" name="address"></textarea><br/>
<label>Service</label>
<fieldset style="width: 232px;">
<label style="width:35px" for="service1">Basic</label><input type="radio" name="service" id="service1" value="basic" />
<label style="width:80px" for="service2">Professional</label><input type="radio" name="service" id="service1" value="professional" />
<label style="width:60px" for="service3">Premium</label><input type="radio" name="service" id="service1" value="premium" />
</fieldset>
<div style="text-align: center;padding-top: 10px;">
<input type="submit" style="display: inline-block;" value = "submit form" /></div>
<br/>
</form>
</body>
</html>

How to Align Web Form Text and Inputs Separately?

I'm looking for the most efficient way to code a fairly simple html form layout I've done a mockup of.
So far I've thought of a number of ways to code this but they all seem rather cumbersome when implemented.
Basically what I'm trying to put into action is plain text aligned to the right and form imputes aligned to the left with a line in the center of both. Below is an image that should give an example of what I'm trying to achieve.
Here's one approach, though I think you could have helped yourself a great deal more, by showing previous attempts and explaining problems you had. However:
form {
width: 80%;
max-width: 40em;
margin: 0 auto;
color: #3f3a27;
background: #f4f0e5 url(http://davidrhysthomas.co.uk/linked/test.png) 35% 0 repeat-y;
padding: 0.5em;
}
label, input[type=text], select {
display: inline-block;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
-o-box-sizing: border-box;
-box-sizing: border-box;
margin-bottom: 0.6em;
}
label {
width: 30%;
text-align: right;
margin: 0 10% 0 0;
}
label:after {
content: ': ';
}
input[type=text] {
width: 40%;
}
select {
width: 20%;
}
fieldset {
margin: 0 0 1em 0;
}
With the HTML:
<form action="#" method="post">
<fieldset>
<label for="fullName">Full name</label>
<input type="text" id="fullName" />
<label for="companyName">Company</label>
<input type="text" id="companyName" />
</fieldset>
<fieldset>
<label for="select">Select</label>
<select id="select" name="select">
<option>Option one</option>
<option>Option two</option>
</select>
<label for="t1">Text input 1</label>
<input id="t1" type="text" />
<label for="t2">Text input 1</label>
<input id="t2" type="text" />
<label for="t3">Text input 1</label>
<input id="t3" type="text" />
</fieldset>
</form>
JS Fiddle demo.
Here is a basic form with minimal style
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>Save
</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;
}
Test
http://jsfiddle.net/andresilich/qxMVd/
I recommend using the display values of table, table-row, and table-cell to keep the markup as semantically neat as possible: See this jsFiddle
CSS & HTML http://jsfiddle.net/27cdz/3/
I have it like you wanted it: http://jsfiddle.net/XURye/
With the same colors and every position is correct too!
This minimal two selectors CSS from this Drupal post worked really well:
.form-item {
padding: 10px 0 10px 200px;
position: relative;
}
.form-item label {
left: 0;
position: absolute;
}