This question already has answers here:
Center form submit buttons HTML / CSS
(11 answers)
Closed 5 years ago.
Very odd and I spent an hour today trying to figure out what I'm doing wrong. Have a email signup form. Four input fields and a submit button. In my design, the submit button should be centered under the four fields. However, instead the button is flush left aligned no matter whether I use or don't use float:left; or clear:both; or margin:0 auto; In other words, the usual suspects.
Here's the site. The form is on the bottom: http://ellismarsalis2017.jasonmarsalis.com/
Here's the code:
#footerForm {
position: relative;
float: none;
width: 728px;
height: auto;
margin: 0 auto;
padding: 18px auto 0;
}
footer form input {
float: left;
color: #2a358f;
width: 44%;
background: #edc53e;
border-radius: 8px;
margin: 0 2% 14px;
font-size: 18px;
padding: 0 .5%;
border: none;
}
footer form input.signUp {
font-family: "clarendon-urw", serif;
float: none!important;
clear: both;
background: #2a358f;
color: #edc53e;
margin: 0 auto;
font-size: 18px;
padding: 8px 24px;
border: none;
text-align: center;
}
footer p {
padding: 28px 0;
}
<div id="footerForm">
<form name="" method="post" action="http://www.yoursite.com/box.php">
<input name="name" type="text" id="name" value="Your Name">
<input name="field1" type="text" id="field1" value="Your City">
<input name="email" type="text" id="email" value="Your Email Address">
<input name="field2" type="text" id="field2" value="Your State">
<input name="p" type="hidden" id="p" value="7">
<input type="hidden" name="nlbox[1]" value="1">
<input type="submit" name="Submit" class="signUp" value="Sign me up for the Email List!">
</form>
</div>
You should put it on DIV section and make it's style text-align:center like that:
<div style="text-align:center;">
<input type="submit" name="Submit" class="signUp" value="Sign me up for the Email List!">
</div>
or with a class and css code :
HTML
<div class="submitsection">
<input type="submit" name="Submit" class="signUp" value="Sign me up for the Email List!">
</div>
CSS
.submitsection {
text-align:center;
}
I'm trying to create two css container classes that can be used to:
Vertically align form elements using .group.
Horizontally align form elements using .group.group--inline.
Each for element will use the class .group__item to make sure there's 16px vertical and horizontal distance between the form elements. For example:
.group__item { margin-top: 16px; }
I however want to sure that the entire height and width of the .group can be used for the form elements and that there is no unwanted whitespace. Not having any margin around our components makes it easier to properly layout them.
To negate the margin on the .group__item's I'm adding it as negative margin to the .group and .group--inline. For example:
.group { margin-top: -16px; }
I'm wondering if there are any negative side effects to giving the .group container a negative margin?
function toggleGroupBorder() {
var groups = document.querySelectorAll('.group');
for (var i = 0, j = groups.length; i < j; i++) {
groups[i].classList.toggle('group--show-border');
}
}
.container {
margin: 32px;
padding: 32px;
border: 1px solid #99f;
}
.group {
margin-top: -16px;
}
.group.group--show-border {
border: 1px solid #f99;
}
.group .group__item {
display: block;
margin-top: 16px;
}
.group.group--inline {
margin-left: -16px;
}
.group.group--inline .group__item {
margin-left: 16px;
display: inline-block;
}
* {
box-sizing: border-box;
}
body {
font-family: Helvetica, Arial;
margin: 0;
}
input {
height: 32px;
padding: 0 8px;
}
button {
height: 32px;
padding: 0 24px;
border: none;
}
<button onclick="toggleGroupBorder();">Toggle Group Border</button>
<h2>Vertical field alignment using <code>.group</code></h2>
<div class="container">
<div class="group">
<input class="group__item" type="text" />
<input class="group__item" type="text" />
<button class="group__item" >Default</button>
</div>
</div>
<h2>Horizontal field alignment using <code>.group.group-inline</code></h2>
<div class="container">
<div class="group group--inline">
<input class="group__item" type="text" />
<input class="group__item" type="text" />
<button class="group__item" >Default</button>
<input class="group__item" type="text" />
<input class="group__item" type="text" />
<button class="group__item" >Default</button>
<input class="group__item" type="text" />
<input class="group__item" type="text" />
<input class="group__item" type="text" />
<button class="group__item" >Default</button>
</div>
</div>
Or see this CodePen
I want a row of blocks from left to right, followed by a block underneath.
Here is a picture of what I would like to see rendered in the browser.
I need to do all positioning by CSS, not by tables. Here is my HTML and my CSS...
<!DOCTYPE html>
<html>
<head><link rel="stylesheet" href="demo.css" /><head>
<body>
<form action="">
<fieldset>
<legend>Field set A</legend>
<label for="password">Password
<input id="password" name="password" type="text" value="my password" />
</label>
</fieldset>
<fieldset class="radio">
<legend>Chaining mode</legend>
<label for="chain-cfb">
<input id="chain-cfb" name="chain" type="radio" />CFB
</label>
<label for="chain-cbc">
<input id="chain-cbc" name="chain" type="radio" />CBC
</label>
</fieldset>
</form>
<hr />
<p style="padding-top: 1em;">Some text underneath</p>
</body>
</html>
... and here is the content of demo.css...
fieldset
{
float: left;
display: block;
width: 17em;
margin: 0 1em 1em 0;
padding: 0 1em 1em 1em;
}
fieldset.radio input
{
clear: both;
float: left;
width: auto;
}
input
{
display: block;
width: 15em;
}
label
{
display: block;
margin-bottom: 1em;
font-weight: bold;
}
label.first
{
padding-top: 1em;
}
The way I read it, should be getting the desired result with this code. But I am not. Here is what renders instead ....
What changes do I need to make to my html/css in order to get the stated desired result?
A way without clearing is:
form { overflow: hidden; }
I usually create a class called floatbox and use this on every container which contains floating elements
.floatbox { overflow: hidden; }
the matching html then is
<form class="floatbox" action="">
<fieldset><p>I'm floating</p></fieldset>
<fieldset><p>me too</p></fieldset>
</form>
you need to make the <hr /> element clear the floats. hr { clear: left; }
Add:
hr {
clear: left;
}
to your style sheet to clear your floats.
You could use the ole' dummy clearing element trick:
<form action="">
<fieldset>
<legend>Field set A</legend>
<label for="password">Password
<input id="password" name="password" type="text" value="my password" />
</label>
</fieldset>
<fieldset class="radio">
<legend>Chaining mode</legend>
<label for="chain-cfb">
<input id="chain-cfb" name="chain" type="radio" />CFB
</label>
<label for="chain-cbc">
<input id="chain-cbc" name="chain" type="radio" />CBC
</label>
</fieldset>
<div style="clear:both"> </div>
</form>
This ensures your form actually occupies as much space as the elements inside it.
The problem with simply clearing the hr is that the form has zero width and height, which could be problematic if you're applying styling to the form as well.
I have the following form:
<form action="post.php" method="POST">
<fieldset>
<div class="ratingClass">
<input type="radio" class="radio" name="rate" value="1" id="1"/>
<label for="1">1</label>
<input type="radio" class="radio" name="rate" value="2" id="2"/>
<label for="2">2</label>
<input type="radio" class="radio" name="rate" value="3" id="3"/>
<label for="3">3</label>
<input type="radio" class="radio" name="rate" value="4" id="4"/>
<label for="4">4</label>
<input type="radio" class="radio" name="rate" value="5" id="5"/>
<label for="5">5</label>
</div>
</fieldset>
<input type="submit" value="Rate">
</form>
Styled by the following CSS:
fieldset {
overflow:hidden;
}
.ratingClass {
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;
}
It's all inside of another div that has text-align: center; styling.
I realize that behavior is because of the floats, but if I remove them then the radio buttons no longer display inline.
How can I have them inline and centered?
You don't need to float everything nor make the labels block elements. Replacing your CSS with this causes everything to be centered:
fieldset {
overflow: hidden;
}
label {
padding: 2px 1em 0 0;
}
input[type=radio], input.radio {
margin: 2px 0 0 2px;
}
The <div class="ratingClass"> is also superfluous and can be removed.
Try making the .ratingClass container either:
.ratingClass {
float:left;
clear:none;
width: 100%;
text-align: center;
}
or
.ratingClass {
float:none;
text-align: center;
}
If you can cope with a fixed width for the ratingClass div you could do it as follows…
.ratingClass {
width:300px; /* insert desired width here */
margin:auto;
}
How to position a complex form with multiple fields in line across the screen?
Why are people so hell-bent on avoiding tables?
Tables are not deprecated and should be used when displaying content which logically belongs in a table.
If your form is logically grouped such that a table would be intuitive, please use a table.
Always be thinking: "What's the cleanest, simplest, most maintainable way to achieve this result."
If you want a fluid form with a variable number columns, then disregard this.
I prefer the slightly-more-semantic way, using a definition list:
<dl class="form">
<dt><label for="input1">One:</label></dt>
<dd><input type="text" name="input1" id="input1"></dd>
<dt><label for="input2">Two:</label></dt>
<dd><input type="text" name="input2" id="input2"></dd>
</dl>
Then your CSS:
dl.form {
width:100%;
float:left;
clear:both;
}
dl.form dt {
width:50%;
float:left;
clear:left;
text-align:right;
}
dl.form dd {
width:50%;
float:left;
clear:right;
text-align:left;
}
This should produce a form centered in the page, with the labels in the left column and the inputs in the right
There are many different ways to do this. It's all a matter of preference. What I typically do is have a wrapper div that contains all of the rows, and then a div block per row that contains the label, input, and validator. You can use the line-height CSS property to help you with vertical alignment. Example:
<div class="formWrapper">
<form>
<div class="formItem">
<label for="firstName">First Name:</label>
<input name="firstName" id="firstName" class="required" type="text" />
<span class="validator" style="display: none;">*</>
</div>
... <!-- Rinse repeat -->
</form>
</div>
<style type="text/css">
.formWrapper { width: 400px }
.formWrapper .formItem { line-height: 35px; height: 35px; }
.formWrapper label { width: 50px; }
.formWrapper input { width: 100px; border: 1px solid #000; }
.formWrapper .validator { padding-left: 10px; color: #FF0000; }
</style>
Hope that helps.
After looking at many many different solutions, I found the examples on this page (particularly the one from 'Fatal'?) some of the most helpful. But the extensive and tags did bother me a bit. So here is a little bit of a modification that some may like. Also, you find some sort of 'wrapper' or 'fieldset' style very necessary to keep the float from affecting other HTML. Refer to examples above.
<style>
.formcol{
float: left;
padding: 2px;
}
.formcol label {
font-weight: bold;
display:block;}
</style>
<div class="formcol">
<label for="org">organization</label>
<input type="text" id="org" size="24" name="org" />
</div>
<div class="formcol">
<label for="fax">fax</label>
<input type="text" id="fax" name="fax" size="2" />
</div>
<div class="formcol">
<label for="3">three</label>
<input type="text" id="3" name="3" />
<label for="4">four</label>
<input type="text" id="4" name="4" />
<label for="5">five</label>
<input type="text" id="5" name="5" />
</div>
<div class="formcol">
<label for="6">six</label>
<input type="text" id="6" name="6" />
</div>
That would be done using CSS by setting the "display" property to "inline" (since form elements are, by default, block level elements).
Do a search for "layouts without tables". Many sites describe formatting with CSS. Here is a simple intro: http://www.htmlgoodies.com/beyond/css/article.php/3642151
I suggest you blueprint CSS framework. Have a quick look at the demo page.
This is what I usually use when I need to design pretty complex forms.
HTML:
<fieldset> <legend>Consent group</legend> <form> <fieldset class="nolegend"> <p><label><span>Title</span> <input type="text" name="title" size="40" value="" /></label></p> <p><label><span>Short name</span> <input type="text" name="sname" size="20" value="" /></label></p> <p><label><br /><input type="checkbox" name="approval"> This consent group requires approval</label></p> </fieldset> <fieldset class="nolegend"> <p><label><span>Data use limitations</span> <textarea name="dul" cols="64" rows="4"></textarea></label></p> </fieldset> <input type="submit" value="Submit" /> </form></fieldset>
CSS:
body, input, textarea, select { font: 1em Arial, Helvetica, sans-serif;}input, textarea, select { font-size: .8em }fieldset,fieldset legend { background-color: #EEE;}fieldset { border: none; margin: 0; padding: 0 0 .5em .01em; top: 1.25em; position: relative; margin-bottom: 2em;}fieldset fieldset { margin: 0 0 1em 0;}fieldset legend { padding: .25em .5em 0 .5em; border-bottom: none; font-weight: bold; margin-top: -1.25em; position: relative; *left: -.5em; color: #666;}fieldset form,fieldset .fieldset { margin: 0; padding: 1em .5em 0 .5em; overflow: hidden;}fieldset.nolegend { position: static; margin-bottom: 1em; background-color: transparent; padding: 0; overflow: hidden;}fieldset.nolegend p,fieldset.nolegend div { float: left; margin: 0 1em 0 0;}fieldset.nolegend p:last-child,fieldset.nolegend div:last-child { margin-right: 0;}fieldset.nolegend label>span { display: block;}fieldset.nolegend label span { _display: block;}
I omitted couple lines of CSS with Safari hacks. You can check out live version of this code.
Pace KyleFarris but I just had to give Ben S a vote for having the guts to mention tables. Just look at the variety of CSS solutions on this page and around the internet for a ridiculously simple problem. CSS may one day become a good solution, but for the time being replicating the simple row and column grid that the table tag provides is extremely complex. I have spent countless fruitless hours with this prejudice against tables for things like a form. Why do we do this to ourselves?
input fields, by default, are inline. Therefore, you can simply use line them up without Another option if you want them lined up correctly is as follows:
<div id="col1" style="float: left;>
<input type="text" name="field1" />
<br />
<input type="text" name="field3" />
</div>
<div id="col2" style="float: left;>
<input type="text" name="field2" />
<br />
<input type="text" name="field4" />
</div>
I prefer to use fieldset to group all elements and p for each form field.
<html>
<head>
<style type="text/css">
fieldset {
width: 500px;
background-color: lightblue;
}
fieldset legend {
font-weight: bold;
}
fieldset p {
clear:both;
padding: 5px;
}
fieldset label {
text-align: left;
width: 100px;
float: left;
font-weight: bold;
}
fieldset .Validator {
color: red !important;
font-weight: bold;
}
</style>
<head>
<body>
<form>
<fieldset>
<legend>Data</legend>
<p>
<label for="firstName">First Name:</label>
<input name="firstName" id="firstName" class="required" type="text" />
<span class="Validator" style="display: none;">*</span>
</p>
<p>
<label for="lastName">Last Name:</label>
<input name="lastName" id="lastName" class="required" type="text" />
<span class="Validator">*</span>
</p>
</fieldset>
</form>
</body>
</html>