CSS - Simple form not formatting properly - css

I've got the following form where everything formats properly with the exception of the submit buttons at the bottom of the form: they should be centered.
I've tried a variety of different combinations of nesting divs in combination with setting widths, margins, and text-aligns, but I can't seem to figure out what I am doing wrong.
http://jsfiddle.net/vM5fp/
Here is what I have so far...
HTML
<form id="myForm" action="#" method="post">
<div>
<label for="pickone" id="ruleTypeLabel">Pick One: </label>
<select id="pickone" name="pickone">
<option value="1">1</option>
<option value="2" selected="selected">2</option>
</select>
</div>
<div>
<label for="field1" id="field1label">Field 1: <span class="helpercomment">xx123</span></label>
<input type="text" id="field1" name="field1" />
</div>
<div>
<label for="field2" id="field2label">field 2: </label>
<input type="text" id="field2" name="field2" />
</div>
<div>
<label for="field3" id="field2">field 3: <span class="helpercomment">Separate by commas</span></label>
<textarea id="field 3" name="field 3"></textarea>
</div>
<div>
<label for="field4" id="field4label">field 4: </label>
<input type="text" id="field4" name="field4" />
</div>
<div>
<label for="startDate" id="startDateLabel">Start Date: <span class="helpercomment">MM/DD/YYYY</span></label>
<input type="text" id="startDate" name="startDate" class="date"/>
<a href="#" class="calendaricon">
<img src="Calendar.png" />
</a>
</div>
<div>
<label for="endDate" id="endDateLabel">End Date: <span class="helpercomment">MM/DD/YYYY</span></label>
<input type="text" id="endDate" name="endDate" class="date"/>
<a href="#" class="calendaricon">
<img src="Calendar.png" />
</a>
</div>
<div>
<div class="buttonrow">
<input type="submit" id="submitButton" value="Submit" />
<input type="submit" id="cancelButton" value="Cancel" />
</div>
</div>
</form>
And the css:
form {
width:300px;
margin:10px auto;
}
form div {
clear:both;
width:100%;
}
form div label {
display:block;
text-align:right;
width:140px;
float:left;
}
form div label .helpercomment{
display:block;
font-size:.8em;
font-style:italic;
color:#C1C2D7;
background-color:#FFF;
text-align:right;
}
form div input, form select, form textarea {
float:left;
width:140px;
margin:2px 0 20px 10px;
}
form div input.date {
width:113px;
}
form div a.calendaricon {
}
form div a.calendaricon img {
display:inline;
border:none;
margin-top:2px;
margin-left:4px;
}
form div div.buttonrow {
text-align:center;
width:auto;
margin:auto;
}
.buttonrow #submitButton, .buttonrow #cancelButton {
width:auto;
}

The buttons are floating (because they're inputs too). Either change them to <button> or modify the CSS:
.buttonrow #submitButton, .buttonrow #cancelButton {
float: none;
width:auto;
}
http://jsfiddle.net/UcPD2/

You're far better off using a styled unordered list than DIVs to format forms.
form ul, form li {
margin:0;
padding:0
}
<ul>
<li><label>....</label> <input ... /></li>
...
</ul>

Related

how to get a form side by side to a div

I'm building a menu that scrolls down with the page for example Facebook's menu but i'm having trouble, the form seems to go under the div and i tried making it float left and it still doesn't work, i want to make it so their all lined up in a line.. what am i doing wrong?
here is my code
<div id="topnav">
<div id="nav">
<img src="/img/putlockermedia_logo.png" />
<input type="button" name="button" id="button" value="Shows" />
<input type="button" name="button" id="button" value="movies" />
<form action='/search.php' method='GET'>
<input id='searchbar' type='text' size='65' name='search' placeholder="search for movies & tv shows">
<input id='submit' type='submit' name='submit' value='Search' >
</form>
</div>
</div>
and here is the full css for it :)
<style>
#topnav{
background-color: #999;
height: 45px;
width: 100%;
position: fixed;
left:0px;
top: 0px;
}
#nav{
margin-left: 10px;
}
</style>
Add a wrapper div to the form and float it right
<div id="topnav">
<div id="nav">
<img src="/img/putlockermedia_logo.png" />
<input type="button" name="button" id="button" value="Shows" />
<input type="button" name="button" id="button" value="movies" />
<div id="right">
<form action='/search.php' method='GET'>
<input id='searchbar' type='text' size='65' name='search' placeholder="search for movies & tv shows">
<input id='submit' type='submit' name='submit' value='Search' >
</form>
</div>
</div>
</div>
CSS
<style>
#topnav{
background-color: #999;
height: 45px;
width: 100%;
position: fixed;
left:0px;
top: 0px;
}
#nav{
margin-left: 10px;
}
#right {
float:right
}
</style>
yes you have to use the float:left; width:50%; for form inside the div
check the fiddle link sample

Aligning HTML5 form elements

I am new to HTML5 so all the help I can get is appreciated. I have three fields (First Name, Last Name and Birth Date) and I am trying to align them together. I would like to align the fields together horizontally and vertically.
Here is my simple code so far:
<html>
<!DOCTYPE html>
<html>
<link href="styles.css" rel="stylesheet" type="text/css">
<body>
<form>
<label for="firstname">First Name:</label> <input name="firstname" type="text" size="50" autofocus><br>
<label for="lastname"><br>Last Name:</label> <input type="text" name="lastname" size="50" autofocus><br>
<label for="birthdate"><br>Birth Date:</label> <input type="date" name="bdate" size="50"><br>
<form> </body> </html>
Here is the CSS I have:
input[type=text] {
border: 1px solid #D4E2F1;
}
input[type=date] {
border: 1px solid #D4E2F1;
}
input[type=color] {
border: 1px solid #D4E2F1;
}
I would prefer not use tables as I am not trying to display tabular data. I am looking for a efficient and correct way to do this.
Your help would be greatly appreciated.
Thanks.
AJ
Try this:
HTML:
<form class="user-form">
<div class="field">
<label for="firstname">First Name:</label>
<input name="firstname" type="text" size="50" autofocus />
</div>
<div class="field">
<label for="lastname">Last Name:</label>
<input type="text" name="lastname" size="50" />
</div>
<div class="field">
<label for="birthdate">Birth Date:</label>
<input type="date" name="bdate" size="50" />
</div>
<form>
CSS:
.user-form { padding:20px; }
.user-form .field { padding: 4px; margin:1px; background: #eee; }
.user-form .field label { display:inline-block; width:120px; margin-left:5px; }
.user-form .field input { display:inline-block; }
jsFiddle: http://jsfiddle.net/VuSX4/
Try this
HTML:
<
div style="display:block; position:relative; width:100(or something)px; height:auto; margin:0px auto 0px;"
>
Put your form markup in here
<
/div>
I looked all over for this answer and found the information at W3C Schools and a bit of trial and error
This validates at http://validator.w3.org/

divs placement and display [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I'm trying to create a form with this formatting style:
label a
[input text] SUBMIT
label b
[select]
label c
label from [input text] label to [input texŧ] SUBMIT
This is my html with css code:
<style type="text/css">
form {
width:460px;
}
form label {
font-size:12px;
color:#666;
cursor:auto !important;
}
form input[type=text] {
padding:5px;
height:30px;
line-height:30px;
width:370px;
color:#333;
font-size:14px;
}
form select {
padding:5px;
height:30px;
line-height:26px;
width:370px;
color:#333;
font-size:12px;
border:solid 1px #ccc;
overflow:hidden;
}
form input[type=submit] {
height:20px;
width:20px;
background: url("/mini_glass.png") no-repeat scroll 3px 3px transparent;
border:none;
cursor:pointer;
}
</style>
<div class="margin_top_20">
<form action="">
<div>
<div id="A">
<label>a</label>
<input name="q" value="" type="text">
<input value="" type="submit">
</div>
</div>
<div id="B">
<label>b</label>
<select></select>
</div>
<div id="C">
<label>c</label>
<div id="D">
<label>from</label>
<input name="from" value="" type="text" style="width:50px">
<label>to</label>
<input name="to" value="" type="text" style="width:50px">
<input value="" type="submit">
</div>
</div>
</form>
</div>
I think the problem is that I should set properties like position or float but I don't understand how. Should I use the float:left property for some divs? The way that property works is not very clear to me.
Just add rules specific to the labels in div A and div B
#A label, #B label { display:block}
Demo at http://jsfiddle.net/emTKJ/
I made some changes to your form and CSS and here is the Working Solution as per the format that you want.
The HTML:
<div class="margin_top_20">
<form action="">
<div>
<div id="A">
<label>a</label>
<input name="q" value="" type="text">
<input value="submit" type="button">
</div>
</div>
<div id="B">
<label>b</label>
<select></select>
</div>
<div id="C">
<label>c</label>
<div id="D">
<span>from</span>
<input name="from" value="" type="text" style="width:50px">
<span>to</span>
<input name="to" value="" type="text" style="width:50px">
<input value="submit" type="button">
</div>
</div>
</form>
</div>
The CSS:
form {
width:460px;
}
form label {
font-size:12px;
color:#666;
cursor:auto !important;
display:block;
}
span{
font-size:12px;
color:#666;
cursor:auto !important;
}
form input[type=text] {
padding:5px;
height:30px;
line-height:30px;
width:370px;
color:#333;
font-size:14px;
}
form select {
padding:5px;
height:30px;
line-height:26px;
width:370px;
color:#333;
font-size:12px;
border:solid 1px #ccc;
overflow:hidden;
}
form input[type=submit] {
height:20px;
width:20px;
background: url("/mini_glass.png") no-repeat scroll 3px 3px transparent;
border:none;
cursor:pointer;
}
You just needed to add a display:block; for the form label and a <span> tag for from and to and I changed input type from submit to button and valued as submit. You can change and style it as per your requirement.
Hope this helps.
In my experience, when you want to position a group of element, you should wrapped it with div element and style it. With some element have similar style, add it with the same class. Shouldn't set width of form, just wrap the content inside it with a div and set style for it. I fixed your code here. See full demo in jsfiddle
HTML
<div class="margin_top_20">
<form action="">
<div class="line">
<div id="A">
<div>
<label>Lable A</label>
</div>
<div>
<input name="q" value="" type="text" />
<input value="submit button" type="submit" />
</div>
</div>
</div>
<div class="line" id="B">
<div><label>Label b</label></div>
<div><select></select></div>
</div>
<div class="line" id="C">
<div><label>Label c</label></div>
<div id="D">
<label>from</label>
<input name="from" value="" type="text" style="width:50px" />
<label>to</label>
<input name="to" value="" type="text" style="width:50px" />
<input value="submit button" type="submit" />
</div>
</div>
</form>
</div>
CSS:
form {
}
.line{
padding-top:5px;
}
form label {
font-size:12px;
color:#666;
cursor:auto !important;
}
form input[type=text] {
padding:5px;
width:370px;
color:#333;
font-size:14px;
}
form select {
padding:5px;
width:370px;
color:#333;
font-size:12px;
border:solid 1px #ccc;
overflow:hidden;
}
form input[type=submit] {
border:none;
cursor:pointer;
}

Relative positioning in CSS is completely different in Safari and Firefox?

I am new to CSS and have been trying to create a web form with the radio buttons aligned next to text fields. However, in Firefox the radio boxes look fine, but when I check in Safari the radio boxes are in the middle of the text fields.
The CSS looks like this:
input#answers
{
display: block;
height: 31px;
left: 87px;
position: relative;
top: 0px;
width: 350px;
}
input[type="radio"]
{
display: inline- block;
height: 20px;
left: 249px;
position: relative;
top: -30px;
vertical-align: middle;
}
and the HTML like this:
<form id="quizCreator" name="quizCreator" method="post" action="quiz.maker.php">
<label><span><img src="images/quizcreatorText.ques.png" hspace="79px" vspace="70px" class="titles" /></span> <br />
<input type="text" name="question" id="question" />
</label>
<label><img src="images/quizcreatorText.ans.png" hspace="79px" vspace="70px" class="titles" id="ans" /> <br />
<input type="hidden" name="creator" value="myform" />
<br />
<input type="text" name="answer1" id="answers" />
</label>
<label>
<input type="radio" name="radio" id="radio1" value="radio1" />
</label>
<label>
<input type="text" name="answer2" id="answers" />
</label>
<label>
<input type="radio" name="radio" id="radio3" value="radio2" />
</label>
<label>
<input type="text" name="answer3" id="answers" />
</label>
<label>
<input name="radio" type="radio" class="radio" id="radio3" value="radio3" />
</label>
<label>
<input type="text" name="answer4" id="answers" />
</label>
<label>
<input name="radio" type="radio" id="radio4" value="radio4" />
</label>
<span><input type="image" src="images/quiznameText.next.png" name="nextPage" id="nextPage" class="nxtbutton" /></span>
</form>
I've tried using relative positioning and margins, but while it fixes the problems in Safari, Firefox then looks wrong. Is there a way to make them both work as I want with a single set of code? (And what about MSIE?)
You should not be using relative positioning for this.
The proper structure is something like this (not sure if you want the text before or after):
<div class="answers">
<ul>
<li><label>your text</label><input type="radio" /></li>
...repeat...
</ul>
</div>
CSS:
.answers ul, .answers li {
list-style-type:none;
margin:0;
padding:0;
}
.answers li {
clear:both
}
.answers label {
display:block;
float:left;
width:100px;
}
.answers input {
display:block;
float:left;
}

How to place controls in div tag and design like this

I have a requirement where I need to place the controls like this in design:
label1 textbox1 label5 textbox5
label2 textbox2 label6 textbox6
label3 textbox3 label7 textbox7
label4 textbox4 label8 textbox8
Now I need to place all the controls in a div tag, how can I do it? I am using vs 2008 and asp.net
Here is the css
<style>
#txtbx1div
{
width:200px;
padding:5px;
display:inline;
float:left;
position:relative;
}
#txtbx2div
{
width:200px;
padding:5px;
display:inline;
float:left;
position:relative;
}
</style>
The html part..Remember the values posted in this example are taken as an array txtbox[]..you you can rename it as you wish.
<div id="txtbx1div">
<label>label1</label>
<input type="text" id="textbox[]" />
<label>label2</label>
<input type="text" id="textbox[]" />
<label>label3</label>
<input type="text" id="textbox[]" />
<label>label4</label>
<input type="text" id="textbox[]" />
</div>
<div id="txtbx2div">
<label>label5</label>
<input type="text" id="textbox[]" />
<label>label6</label>
<input type="text" id="textbox[]" />
<label>label7</label>
<input type="text" id="textbox[]" />
<label>label8</label>
<input type="text" id="textbox[]" />
</div>
​
<div>
<div class="left_align">
label1</div>
<div class="left_align">
textbox1</div>
<div class="left_align">
<div class="left_align">
label5</div>
<div class="left_align">
textbox5</div>
</div>
</div>
Css style sheet is:
.left_align
{
float:left;
}

Resources