How do I add a pixel of space between my inputs? - html

I have these fields in a search form …
<form id="search-form" action="/events/search" accept-charset="UTF-8" method="get"><input name="utf8" type="hidden" value="✓">
<input type="text" name="first_name" id="first_name" placeholder="First Name" class="searchField">
<input type="text" name="last_name" id="last_name" placeholder="Last Name" class="searchField">
<input type="text" name="event" id="event" placeholder="Event" class="searchField">
<input alt="Search" type="image" src="http://www.racertracks.com/assets/magnifying-glass-0220f37269f90a370c3bb60229240f2ef2a4e15b335cd42e64563ba65e4f22e4.png" class="search_button">
</form>
then I have this style for the form …
#search-form {
display: flex;
flex: 1 0 auto;
}
Problem is, the elements appear right next to each other — https://jsfiddle.net/fmy1syfw/ . I would like at least a pixel of space between them to not make everything seem so cramped. I have tried adding
margin: 1px;
to the above style, but to no avail. How do I add a pixel of space between my elements without breaking the behavior of the form at different screen widths?

Just add margin as required to the inputs and remove it in your media queries (again, as required).
Since you have used fixed widths of 50% in you media query, these will have to be adjusted using calc to account for the extra margin.
body {
background-color: grey;
}
#logo {
margin: 0 auto;
padding: 10px;
}
#searchForm {
padding: 20px;
}
#search-form {
background-color: orange;
display: flex;
flex: 1 0 auto;
}
#last_name,
#event {
margin-left: 1px;
}
#first_name,
#last_name {
width: 20%;
}
#event {
flex-grow: 1;
}
/* Do not specify width to allow it to grow freely */
.search_button {
width: 40px;
height: 40px;
}
/* Predefine image dimensions to ensure proper aspect ratio */
#loginArea {
border-radius: 25px;
font-size: 20px;
padding: 20px;
background-color: #ffffff;
color: #000000;
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translateX(-50%) translateY(-50%);
transform: translateX(-50%) translateY(-50%);
width: 100%;
max-width: 580px;
}
#media (max-width: 620px) {
#search-form {
flex-wrap: wrap;
}
#first_name {
width: 50%;
margin: 0;
}
#last_name {
width: calc(50% - 1px);
margin-left: 1px;
}
#event {
width: calc(100% - 40px);
margin: 0;
}
}
.searchField {
line-height: 40px;
font-size: 22px;
margin: 0;
padding: 5px;
border: 1px solid #ccc;
box-sizing: border-box;
-webkit-appearance: textfield;
background-color: white;
-webkit-rtl-ordering: logical;
-webkit-user-select: text;
letter-spacing: normal;
word-spacing: normal;
text-transform: none;
text-indent: 0px;
text-shadow: none;
text-align: start;
}
<div id="loginArea">
<div id="searchForm">
Search For Results
<br />
<div>
<form id="search-form" action="/events/search" accept-charset="UTF-8" method="get">
<input name="utf8" type="hidden" value="✓">
<input type="text" name="first_name" id="first_name" placeholder="First Name" class="searchField">
<input type="text" name="last_name" id="last_name" placeholder="Last Name" class="searchField">
<input type="text" name="event" id="event" placeholder="Event" class="searchField">
<input alt="Search" type="image" src="http://www.racertracks.com/assets/magnifying-glass-0220f37269f90a370c3bb60229240f2ef2a4e15b335cd42e64563ba65e4f22e4.png" class="search_button">
</form>
</div>
</div>
</div>
JSFiddle Demo

Related

Header and footer being pushed left at 375 & 320 mobile view, but fine in all else?

So my footer and header (header is ONLY an image btw) are being pushed left once i go to 375 or 320 mobile view. But on all else it's totally fine, 425px view included. I have tried removing the form margin/padding and fieldset margin/padding, that made no difference. I have tried using background cover/fill for the header image, that did nothing either.
Everything else on the page is right where it should be, only the header and footer are being pushed left at those views.
This is my code
body {
font-family: 'Nunito', sans-serif;
width: 100%;
height: 100%;
margin: 0px;
padding: 0px;
overflow-x: hidden;
}
* {
box-sizing: border-box;
}
/* Header css */
header {
display: flex;
text-align: center;
}
#header_img {
width: 100%;
max-height: 150px;
}
/* Form css */
form {
display: flex;
flex-wrap: wrap;
flex-direction: column;
align-items: center;
padding: 0;
}
/* Fieldset and Legend*/
fieldset {
display: flex;
flex-wrap: wrap;
flex-direction: column;
align-items: center;
padding: 1em;
margin: 1em 0;
border: solid 1px #ccc;
border-radius: 6px;
min-width: 200px;
}
legend {
padding: 0 .25em;
font-size: 1.25em;
color: rgb(34, 34, 34);
}
/* Labels */
label {
display: block;
margin-top: .5em;
}
label:first-of-type {
margin-top: 0;
}
/* Inputs and textarea */
input {
width: 15em;
padding: .5em;
border: solid 1px #999;
font-size: 12px;
}
#first_name, #last_name {
font-weight: bold;
font-size: 14px;
}
#email {
font-style: italic;
}
textarea {
min-height: 8em;
min-width: 100%;
padding: .5em;
border: solid 1px #999;
}
input[type="checkbox"] {
width: 1em;
}
input[type="submit"] {
padding: .5em 1em;
margin-top: 1em;
border-radius: 6px;
background-color: #333;
color: #fff;
font-size: .8em;
width: 7em;
}
input, textarea {
border-radius: 2px;
}
#terms_and_conditions_div {
display: flex;
flex-direction: row;
align-items: baseline;
}
/* Footer css*/
footer {
display: flex;
justify-content: center;
position: absolute;
width: 100%;
text-align: center;
font: 18px bold;
}
/* Large screen rules */
#media screen and (min-width: 430px) {
legend {
font-size: 1.75em;
}
fieldset {
padding: 1em 2em;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css2?family=Nunito:wght#400&display=swap" rel="stylesheet">
<link rel="stylesheet" href="./main.css">
<title>Event</title>
</head>
<body>
<header>
<img id="header_img" src="./Code_challenge_img.png" alt="Event location image">
</header>
<form id="contact_form" name="contact_form" method="POST">
<fieldset>
<legend>Contact Details</legend>
<label for="first_name">First name:</label>
<input type="text" id="first_name" name="first_name" placeholder="First name" maxlength="30">
<label for="last_name">Last name:</label>
<input type="text" id="last_name" name="last_name" placeholder="Last name" maxlength="30">
<label for="phone">Phone:</label>
<input type="tel" id="phone" name="phone" placeholder="Phone number" maxlength="30">
<label for="email">Email:</label>
<input type="email" name="email" id="email" placeholder="Email" maxlength="50">
<label for="promo_code">Promo Code:</label>
<input type="text" name="promo_code" id="promo_code" placeholder="Promo code" maxlength="7">
<label for="how_did_you_hear_menu">How did you hear:</label>
<select name="how_did_you_hear_menu" id="how_did_you_hear_menu">
<option value="Social Media">Social Media</option>
<option value="From a friend">From a friend</option>
<option value="Email">Email</option>
<option value="Other">Other</option>
</select>
<label for="how_did_you_hear_other">Please specify:</label>
<textarea name="how_did_you_hear_other" id="how_did_you_hear_other" rows="4" cols="50" placeholder="Please specify" maxlength="255"></textarea>
<div id="terms_and_conditons_div">
<input type="checkbox" name="terms_and_conditions" id="terms_and_conditions">
I agree to the terms and conditions of this event.
</div>
<input type="submit" id="form_submit_btn" value="Submit">
</fieldset>
</form>
<footer>
<p id="footer_text">For assistance please call 555-5555 or email test#test.com.</p>
</footer>
</body>
</html>
Thanks for any help everyone!
It is not the header or footer. The problem here is the textarea, it is more then 100% width. Change in the html and css the textarea code as shown below to have a correct header/form/footer.
body {
font-family: 'Nunito', sans-serif;
width: 100%;
height: 100%;
margin: 0px;
padding: 0px;
overflow-x: hidden;
}
* {
box-sizing: border-box;
}
/* Header css */
header {
display: flex;
text-align: center;
}
#header_img {
width: 100%;
max-height: 150px;
}
/* Form css */
form {
display: flex;
flex-wrap: wrap;
flex-direction: column;
align-items: center;
padding: 0;
}
/* Fieldset and Legend*/
fieldset {
display: flex;
flex-wrap: wrap;
flex-direction: column;
align-items: center;
padding: 1em;
margin: 1em 0;
border: solid 1px #ccc;
border-radius: 6px;
min-width: 200px;
}
legend {
padding: 0 .25em;
font-size: 1.25em;
color: rgb(34, 34, 34);
}
/* Labels */
label {
display: block;
margin-top: .5em;
}
label:first-of-type {
margin-top: 0;
}
/* Inputs and textarea */
input {
width: 15em;
padding: .5em;
border: solid 1px #999;
font-size: 12px;
}
#first_name, #last_name {
font-weight: bold;
font-size: 14px;
}
#email {
font-style: italic;
}
textarea {
min-height: 8em;
width: 100%;
padding: .5em;
border: solid 1px #999;
}
input[type="checkbox"] {
width: 1em;
}
input[type="submit"] {
padding: .5em 1em;
margin-top: 1em;
border-radius: 6px;
background-color: #333;
color: #fff;
font-size: .8em;
width: 7em;
}
input, textarea {
border-radius: 2px;
}
#terms_and_conditions_div {
display: flex;
flex-direction: row;
align-items: baseline;
}
/* Footer css*/
footer {
display: flex;
justify-content: center;
position: absolute;
width: 100%;
text-align: center;
font: 18px bold;
}
/* Large screen rules */
#media screen and (min-width: 430px) {
legend {
font-size: 1.75em;
}
fieldset {
padding: 1em 2em;
}
}
<header>
<img id="header_img" src="https://placeimg.com/1000/150/animals" alt="Event location image">
</header>
<form id="contact_form" name="contact_form" method="POST">
<fieldset>
<legend>Contact Details</legend>
<label for="first_name">First name:</label>
<input type="text" id="first_name" name="first_name" placeholder="First name" maxlength="30">
<label for="last_name">Last name:</label>
<input type="text" id="last_name" name="last_name" placeholder="Last name" maxlength="30">
<label for="phone">Phone:</label>
<input type="tel" id="phone" name="phone" placeholder="Phone number" maxlength="30">
<label for="email">Email:</label>
<input type="email" name="email" id="email" placeholder="Email" maxlength="50">
<label for="promo_code">Promo Code:</label>
<input type="text" name="promo_code" id="promo_code" placeholder="Promo code" maxlength="7">
<label for="how_did_you_hear_menu">How did you hear:</label>
<select name="how_did_you_hear_menu" id="how_did_you_hear_menu">
<option value="Social Media">Social Media</option>
<option value="From a friend">From a friend</option>
<option value="Email">Email</option>
<option value="Other">Other</option>
</select>
<label for="how_did_you_hear_other">Please specify:</label>
<textarea name="how_did_you_hear_other" id="how_did_you_hear_other" rows="4" placeholder="Please specify" maxlength="255"></textarea>
<div id="terms_and_conditons_div">
<input type="checkbox" name="terms_and_conditions" id="terms_and_conditions">
I agree to the terms and conditions of this event.
</div>
<input type="submit" id="form_submit_btn" value="Submit">
</fieldset>
</form>
<footer>
<p id="footer_text">For assistance please call 555-5555 or email test#test.com.</p>
</footer>
Just wrap the elements you want do be in the center in a <div>, and style them to be position: relative; left: 50%;, so this will allow them to be 50% which is in the center, depending on the device.
EDIT: I have done some research and 50% positioning does not center the element, instead use position: relative; left: 50%; just like last, but them use margin-left: -30%;. And this will definetely center the element.

Insert check icon inside input box when text is present

I am attempting to insert a green check icon when text is inside an input box. I am trying to do this by adding in a fa-fa icon, but it does not seem to be working. In addition, I would like to place a red x icon inside the input box if the email is not valid. If someone has any insight how to do this your help would be greatly appreciated! Fiddle and image are attached.
Green Check and Rex x icons
JsFiddle
<form action="">
<div class="container" id="container">
<label>First Name
<input id="first_name" maxlength="40" name="first_name" size="20" type="text"><i class="fa fa-check-circle" aria-hidden="true"></i>
</label>
<label>Last Name
<input id="last_name" maxlength="80" name="last_name" size="20" type="text">
</label>
<label>Email
<input id="email" maxlength="80" name="email" size="20" type="text"><i class="fa fa-times-circle-o" aria-hidden="true"></i>
<!-- <span class="msg error">Wrong email!</span>
<span class="msg success">Correct email!</span> -->
</label>
<label>Phone
<input id="phone" maxlength="40" name="phone" size="20" type="text">
</label>
<label>City
<input id="city" name="city" maxlength="40" size="20" type="text">
</label>
<label>State/Province
<input id="state" maxlength="20" name="state" size="20" type="text">
</label>
<label id="company">Company
<input id="company" name="company" type="text">
</label>
<label>Comments
<textarea name="" id="" cols="30" rows="10"></textarea>
<button type="submit" name="submit">SUBMIT</button>
</label>
</div>
</form>
body {
color: #fff;
background-color: #f78e2a;
text-align: center;
}
form {
color: #fff;
background-color: #f78e2a;
text-align: center;
font-family: Lato;
}
* {
box-sizing: border-box;
}
h1 {
font-size: 20px;
text-align: center;
}
input[type="text"] {
width: 100%;
padding: 10px;
background-color: #f9a558;
border: 1px solid #fff;
}
input[type="text"]:focus {
background-color: #fff;
}
input[type="text"]:visited {
background-color: #fff;
}
.container {
display: flex;
flex-direction: column;
padding: 5px 0;
margin-left: 10%;
margin-right: 10%;
}
textarea {
width:100%;
background-color: #f9a558;
border: 1px solid #fff;
}
textarea:focus {
background-color: #fff;
}
#company {
flex-basis: 100%;
max-width: 100%;
}
label:nth-last-child(-n+2)
{
flex-basis: 100%;
max-width: 100%;
}
select, label {
height: 50px;
width: 48%;
margin: 2% 1%;
text-align: left;
}
button {
margin-top: 10px;
background-color: #B9B9B9;;
color: #959595;
border-radius: 6px;
width: 120px;
height: 35px;
margin-left: 1%;
display: block;
}
.fa fa-check-circle {
color: green;
}
button:focus{
background-color: #fff;
color: #f78e2a;
}
#media (max-width: 426px) {
label {
width: 98%;
}
}
#media (min-width: 426px) {
.container {
flex-direction: row;
flex-wrap: wrap;
align-self: flex-start;
}
}
A way to do this using javascript is to check keyup on the inputs, and if there is a value, show the icon. I'm using jQuery below.
And for your email, you need to use some sort of validation pattern - if the email input has content, show a red icon by default, but if it matches the validation pattern, change the icon to green. I got the email validation regex from How to validate email address in JavaScript?
function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\]\\.,;:\s#\"]+)*)|(\".+\"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
$('input[type="text"]').on('keyup', function() {
if ($(this).val().trim() != '') {
if ($(this).is('#email')) {
if (validateEmail($(this).val())) {
$(this).attr('data-valid','valid');
} else {
$(this).attr('data-valid','error');
}
} else {
$(this).attr('data-valid','valid');
}
} else {
$(this).removeAttr('data-valid');
}
});
body {
color: #fff;
background-color: #f78e2a;
text-align: center;
}
form {
color: #fff;
background-color: #f78e2a;
text-align: center;
font-family: Lato;
}
* {
box-sizing: border-box;
}
h1 {
font-size: 20px;
text-align: center;
}
input[type="text"] {
width: 100%;
padding: 10px;
background-color: #f9a558;
border: 1px solid #fff;
}
input[type="text"]:focus {
background-color: #fff;
}
input[type="text"]:visited {
background-color: #fff;
}
.container {
display: flex;
flex-direction: column;
padding: 5px 0;
margin-left: 10%;
margin-right: 10%;
}
textarea {
width: 100%;
background-color: #f9a558;
border: 1px solid #fff;
}
textarea:focus {
background-color: #fff;
}
#company {
flex-basis: 100%;
max-width: 100%;
}
label:nth-last-child(-n+2) {
flex-basis: 100%;
max-width: 100%;
}
select,
label {
height: 50px;
width: 48%;
margin: 2% 1%;
text-align: left;
}
button {
margin-top: 10px;
background-color: #B9B9B9;
;
color: #959595;
border-radius: 6px;
width: 120px;
height: 35px;
margin-left: 1%;
display: block;
}
.fa fa-check-circle {
color: green;
}
button:focus {
background-color: #fff;
color: #f78e2a;
}
#media (max-width: 426px) {
label {
width: 98%;
}
}
#media (min-width: 426px) {
.container {
flex-direction: row;
flex-wrap: wrap;
align-self: flex-start;
}
}
label {
position: relative;
}
.fa {
position: absolute;
bottom: 0;
right: 0;
transform: translate(-50%, -45%);
opacity: 0;
transition: opacity .5s, color .5s;
}
[data-valid] + .fa {
opacity: 1;
}
[data-valid="valid"] + .fa {
color: green;
}
[data-valid="error"] + .fa {
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="">
<div class="container" id="container">
<label>First Name
<input id="first_name" maxlength="40" name="first_name" size="20" type="text"><i class="fa fa-check-circle" aria-hidden="true"></i>
</label>
<label>Last Name
<input id="last_name" maxlength="80" name="last_name" size="20" type="text">
</label>
<label>Email
<input id="email" maxlength="80" name="email" size="20" type="text"><i class="fa fa-times-circle-o" aria-hidden="true"></i>
<!-- <span class="msg error">Wrong email!</span>
<span class="msg success">Correct email!</span> -->
</label>
<label>Phone
<input id="phone" maxlength="40" name="phone" size="20" type="text">
</label>
<label>City
<input id="city" name="city" maxlength="40" size="20" type="text">
</label>
<label>State/Province
<input id="state" maxlength="20" name="state" size="20" type="text">
</label>
<label id="company">Company
<input id="company" name="company" type="text">
</label>
<label>Comments
<textarea name="" id="" cols="30" rows="10"></textarea>
<button type="submit" name="submit">SUBMIT</button>
</label>
</div>
</form>
First, you have your input and i tags inside label. This needs to be fixed. After that's complete, you should create a parent div for each field. This will allow you to use position: absolute; for the icons.
Something like this:
HTML
<div class="field">
<label>First Name</label>
<input id="first_name" maxlength="40" name="first_name" size="20" type="text" />
<i class="fa fa-check-circle" aria-hidden="true"></i>
</div>
CSS
.field {
position: relative;
margin: 10px;
}
.field i.fa {
position: absolute;
top: 5px;
right: 5px;
}

HTML/CSS sign up form

I am currently working on creating a sign up form html/css. I realised that different browsers work differently on the width of inputs. How can i rectify this issue and make sure that my sign up form is compatible with all browsers. My sign up form works perfectly for chrome as it is where i do coding on.
ul {
background-color: #000000;
}
li a:hover {
background-color: #0cf72a;
}
.word-container {
width: 500px;
height: 50px;
margin: auto;
position: relative;
top: 80px;
}
.word-container h1 {
margin: 0;
text-align: center;
color: #ab0a0a;
}
.register-container {
width: 600px;
height: 350px;
margin: auto;
position: relative;
top: 100px;
border: 1px solid #000;
}
.fname input[type="text"] {
position: relative;
left: 115px;
top: 30px;
padding: 8px;
}
.lname input[type="text"] {
position: relative;
left: 314px;
top: -5.5px;
padding: 8px;
}
.userid input[type="text"] {
position: relative;
left: 115px;
padding: 8px;
top: 10px;
}
.pwd input[type="password"] {
position: relative;
padding: 8px;
left: 115px;
top: 25px;
}
.email input[type="email"] {
position: relative;
padding: 8px;
left: 115px;
top: 40px;
}
.btn button[type="submit"] {
position: relative;
left: 115px;
top: 55px;
padding: 8px;
width: 382px;
border: 1px solid #000000;
color: #ffffff;
background-color: #ab0a0a;
}
div.btn button[type="submit"]:hover {
background-color: rgb(255, 0, 0);
}
<div class="word-container">
<h1>Create your account</h1>
</div>
<div class="register-container">
<form action="" method="POST">
<div class="fname">
<label>
<input type="text" placeholder="First Name" name="fname" size="20">
</label>
</div>
<div class="lname">
<label>
<input type="text" placeholder="Last Name" name="lname" size="20">
</label>
</div>
<div class="userid">
<label>
<input type="text" placeholder="Username" name="userid" size="50">
</label>
</div>
<div class="pwd">
<label>
<input type="password" placeholder="Password" name="pwd" size="50">
</label>
</div>
<div class="email">
<label>
<input type="email" placeholder="Email Address" name="email" size="50">
</label>
</div>
<div class="btn">
<button type="submit">Create Account</button>
</div>
</form>
</div>
It's always a good idea to use something like normalize.css or any other CSS reset code (eric meyer css reset is very popular too) to reset CSS across all browsers.
Any browser come with it's defaults values for padding's,margins,widths, heights etc...
I guess it won't be an 100% solution but it will defiantly will take you closer to what you're looking for.
Do not jump to position relative and absolute. If you are new to all this, I can understand it seems the most natural way to go about positioning elements; just using a top and left position and that's that. But this is not how you should do it on the web!
Below you can find how I would do it.
Matan G. is right in pointing out that a CSS reset/normalize is often used, and I do so myself as well. However, before you do that (and considering you're new) it would be wise to take a look at the code that I posted and see if it makes any sense to you. If not, ask.
It is important to note that you should avoid these things when possible:
setting a fixed width to text items such as headings, paragraphs, lists.
using relative/absolute positioning. They are very useful but only when necessary.
using too many divs/classes than actually needed. Don't overcrowd your HTML.
* {box-sizing: border-box;}
ul {
background-color: #000000;
}
li a:hover {
background-color: #0cf72a;
}
.word-container {
width: 500px;
height: 50px;
margin: 80px auto auto;
}
.word-container h1 {
margin: 0;
text-align: center;
color: #ab0a0a;
}
.register-container {
width: 600px;
margin: 20px auto auto;
border: 1px solid #000;
padding: 20px;
}
label {
display: block;
}
.name::after {
content: "";
display: table;
clear: both;
}
.name label:first-child {
margin-right: 20px;
}
.name label {
width: calc(100% / 2 - 10px);
float: left;
}
input, [type="submit"] {
padding: 8px;
margin-bottom: 20px;
width: 100%;
}
[type="submit"] {
border: 1px solid #000000;
color: #ffffff;
background-color: #ab0a0a;
margin: 0;
}
[type="submit"]:hover {
background-color: red;
}
<div class="word-container">
<h1>Create your account</h1>
</div>
<div class="register-container">
<form action="" method="POST">
<div class="name">
<label>
<input type="text" placeholder="First Name" name="fname">
</label>
<label>
<input type="text" placeholder="Last Name" name="lname">
</label>
</div>
<label>
<input type="text" placeholder="Username" name="userid">
</label>
<label>
<input type="password" placeholder="Password" name="pwd">
</label>
<label>
<input type="email" placeholder="Email Address" name="email">
</label>
<button type="submit">Create Account</button>
</form>
</div>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>HTML Forms</title>
<style>
.container{
width: 45%;
margin: auto;
}
.form-content{
margin: 40px;
}
.form-content input{
width: 100%;
}
label{
font-weight: bold;
}
input[type=text],[type=email],[type=tel],[type=date],[type=password]{
font-size: 16px;
border-radius: 5px;
background: #D9F1F7;
border: #000000;
padding: 10px;
}
input[type=submit]{
background: #4C63ED;
padding: 10px;
border: none;
border-radius: 5px;
font-size: 16px;
color: #fff;
cursor: pointer;
}
input[type=submit]:hover{
background: #330EEF;
font-weight: bold;
}
</style>
</head>
<body>
<div class = "container">
<form name="signup" method="get" action="">
<div class="form-content">
<label>First Name : </label>
<input type="text" name="firstname" />
</div>
<div class="form-content">
<label>Last Name : </label>
<input type="text" name="lastname" />
</div>
<div class="form-content">
<label>E-Mail : </label>
<input type="email" name="email" />
</div>
<div class="form-content">
<label>Telephone : </label>
<input type="tel" name="telephone" />
</div>
<div class="form-content">
<label>Date of Birth : </label>
<input type="date" name="dob" />
</div>
<div class="form-content">
<input type="submit" name="submit" value="Submit" />
</div>
</form>
</div>
</body>
</html>

Issue getting a submit button to center in a wrap

For some reason I cannot get the submit button to center. I have tried everything from text-align: center; to margin: 0 auto; to a left and right margin at auto. The button will not center anyway I try. What am I failing to do?
.contactForm {
border: 2px solid black;
background-color: #F0F0F0;
width: 40%;
margin: 0 auto;
padding: 40px;
}
.contactButton {
margin-top: 15px;
margin-right: auto;
margin-bottom: 15px;
margin-left: auto;
width: 425px;
font-size: 20px;
font-weight: bold;
padding: 14px;
cursor: pointer;
background-color: #800000;
border: none;
color: #FFFFFF;
text-align: center;
}
<div class="contactForm">
<form action="" method="post" id="mycontactform">
<input type="text" class="inputbar" name="name" placeholder="Full Name" required>
<input type="email" class="inputbaremail" name="email" placeholder="Email" required>
<textarea rows="4" cols="50" name="message" class="inputbarmessage" placeholder="Message" required></textarea>
<label for="contactButton">
<input type="button" class="contactButton" value="Send Message" id="submit">
</label>
</form>
</div>
It's hard to center is, because it is inside a label. The label is an inline element too and will size around the button. Therefor text-align: center and other solutions don't work.
In the snippet below, I've made the label show itself as a block element, which automatically occupies the available width of the parent. Then you can easily center the button inside it:
The added piece:
label[for="contactButton"] {
display: block;
text-align: center;
}
The whole code:
.contactForm {
border: 2px solid black;
background-color: #F0F0F0;
width: 40%;
margin: 0 auto;
padding: 40px;
}
.contactButton {
margin-top: 15px;
margin-right: auto;
margin-bottom: 15px;
margin-left: auto;
width: 425px;
/* Make sure the button isn't too wide on small screens */
max-width: 80%;
font-size: 20px;
font-weight: bold;
padding: 14px;
cursor: pointer;
background-color: #800000;
border: none;
color: #FFFFFF;
text-align: center;
}
label[for="contactButton"] {
display: block;
text-align: center;
}
<div class="contactForm">
<form action="" method="post" id="mycontactform">
<input type="text" class="inputbar" name="name" placeholder="Full Name" required>
<input type="email" class="inputbaremail" name="email" placeholder="Email" required>
<textarea rows="4" cols="50" name="message" class="inputbarmessage" placeholder="Message" required></textarea>
<label for="contactButton">
<input type="button" class="contactButton" value="Send Message" id="submit">
</label>
</form>
</div>
Use text-align:center on the parent div.
Fiddle: http://jsfiddle.net/gopal/su3vg018/
.contactForm {
text-align:center;
}
Add this to your css file:
.contactForm label[for="contactButton"] {
display: block;
text-align: center;
}

Responsive form - controlling height of a textarea

I've got this HTML form:
<form method="post" action="#" class="cf">
<div class="left">
<label for="first-name">First Name</label>
<input type="text" name="first-name" placeholder="First Name" id="first-name" required />
<label for="last-name">Middle Name</label>
<input type="text" name="middle-name" placeholder="Middle Name" id="middle-name" />
<label for="last-name">Last Name</label>
<input type="text" name="last-name" placeholder="Last Name" id="last-name" required />
</div>
<div class="right">
<label for="details">Details</label>
<textarea name="details" placeholder="Details" id="details" required></textarea>
</div>
<input type="submit" name="submit" value="Submit Form" />
</form>
And this is my CSS:
/* Clearfix */
.cf:before,.cf:after { content: " "; display: table; }
.cf:after { clear: both; }
form > * {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.left {
background: rgba(255, 0, 0, .2);
}
.right {
background: rgba(0, 255, 0, .2);
}
form {
background: #ccc;
}
input[type="text"],
textarea {
width: 100%;
border: none;
}
input[type="text"] {
margin-bottom: 10px;
}
label {
display: inline-block;
margin-bottom: 5px;
}
#media only screen and (min-width: 600px) {
form {
background: rgba(0, 0, 255, .3);
}
.left {
float: left;
width: 50%;
}
.right {
float: right;
width: 50%;
}
input[type="submit"] {
clear: both;
}
}
Fiddle
http://jsfiddle.net/gRRmh/
Basically it's three text input fields, one textarea and one submit button (aka input type submit). When the breakpoint is reached, the form flows into a two column layout. That's the part that is working.
The part that is not working is the height of the textarea. I want it to be the same height as the three input fields on the left.
Setting it to height: 100%; does not work for two reasons:
The height of the label needs to be taken into account. Sure I could just give it a height in percentages and subtract that value from the textarea's height (10% / 90%) ...
...but for this to work, one parent elements needs a fixed height, so I need to give the form e.g. a height of 200px. The problem with that is I actually need to match the height of the left column by hand which isn't really a good solution.
So what I am actually looking for is something like the following, just without nudging pixels by hand:
(also with fiddle if you want, but please note: Its a bit messy. http://jsfiddle.net/mnBEh/1/)
How to solve this problem?
It is only posible by giving manually height to textarea.So give height to textarea on media queries.
Try this. It uses CSS tables, but I think it gets the result you're looking for, by setting the textarea to be 100% height, and then minusing the height of it's label. But for some reason, the height is only calculating correctly in Chrome, even though the other browsers supposedly support it.
http://jsfiddle.net/73cyorL1/
CSS:
.table {
display: table;
width: 100%;
}
.row {
display:table-row;
width: 100%;
}
.left {
background: red;
}
.right {
background: blue;
}
label {
display: block;
padding: 10px;
font-family: arial;
font-style: italic;
font-size: 0.75em;
line-height: 1em;
}
form {
background: grey;
}
input[type="text"], textarea {
width: 100%;
border: none;
background: #ccc;
font-family: arial;
padding: 10px;
box-sizing: border-box;
display: block;
}
input[type="text"]:focus, textarea:focus {
outline: 0;
background: #ddd;
}
input[type="submit"] {
display: block;
margin: 10px;
padding: 10px;
cursor: pointer;
}
#media only screen and (min-width: 600px) {
.left {
width: 50%;
height: 100%;
display: table-cell
}
.right {
width: 50%;
height: 100%;
display: table-cell;
overflow: hidden;
}
textarea {
height: calc(100% - 0.75em);
/* 100% fill height, minus height of details label */
}
}
HTML:
<form method="post" action="#" class="table">
<div class="row">
<div class="left">
<label for="first-name">First Name</label>
<input type="text" name="first-name" placeholder="First Name" id="first-name" required="required" />
<label for="last-name">Middle Name</label>
<input type="text" name="middle-name" placeholder="Middle Name" id="middle-name" />
<label for="last-name">Last Name</label>
<input type="text" name="last-name" placeholder="Last Name" id="last-name" required="required" class="last" />
</div>
<div class="right">
<label for="details">Details</label>
<textarea name="details" placeholder="Details" id="details" required></textarea>
</div>
</div>
<input type="submit" name="submit" value="Submit Form" />
</form>
Set "box-sizing: border-box;" and it will works