Center an input with a padding - html

I have a form the whole form needs to be centered in the page. The inputs need some space around the letters for design reasons. If I give a padding the inputs are not perfectly centered anymore.
Can I have the inputs centered and the text inside with some space at the same time?
Here to check:
https://jsfiddle.net/291thr5d/
CSS:
form {
margin: 40px auto;
width: 80%; max-width: 600px; min-width: 300px;
background-color: aliceblue;
}
.title {
width: 100%;
font-family: Arial, Helvetica, sans-serif;
font-size:40px; line-height:40px;
letter-spacing: 1px;
margin:10px 0; padding: 20px;
}
HTML:
<form action="update.php" method="post">
<input type="text" name="titol" value="Title post" class="title">
<input type="submit" name= "actualitzar" value="Submit" class="submit">
</form>

You need to add:
* {
box-sizing: border-box;
}
form {
margin: 40px auto;
width: 80%;
min-width: 300px;
background-color: aliceblue;
}
.title {
width: 100%;
font-family: Arial, Helvetica, sans-serif;
font-size: 40px;
line-height: 40px;
letter-spacing: 1px;
margin: 10px 0;
padding: 20px;
}
* {
box-sizing: border-box;
}
<form action="update.php" method="post">
<input type="text" name="titol" value="Title post" class="title">
<input type="submit" name="actualitzar" value="Submit" class="submit">
</form>
box-sizing includes padding and border in the element's total width and height
Documentation

please add text-align: center to your .title class properties.
form {
margin: 40px auto;
width: 80%;
min-width: 300px;
background-color: aliceblue;
}
.title {
width: 100%;
font-family: Arial, Helvetica, sans-serif;
font-size: 40px;
line-height: 40px;
letter-spacing: 1px;
margin: 10px 0;
padding: 20px;
text-align: center;
}
* {
box-sizing: border-box;
}
<form action="update.php" method="post">
<input type="text" name="titol" value="Title post" class="title">
<input type="submit" name="actualitzar" value="Submit" class="submit">
</form>

Related

Why isn't my background-color not working on my HTML form in CSS?

When I hit the button I want to make my code shows a success field. Choosing the background color of the <div> wouldn't work. Is it just my browser or is something wrong with my code?
.div {
float: top;
background-color: white;
border-radius: 10px;
padding: 10px;
width: 275px;
height: 375px;
margin-left: 500px;
margin-right: 500px;
margin-top: 125px;
box-shadow: 0px 0px 20px #00000010;
}
body {
background-color: #c9ecff;
overflow-y: hidden;
overflow-x: auto;
font-family: 'Ubuntu', sans-serif;
}
textarea {
border-color: #c4c4c4;
border-radius: 5px;
width: 250px;
font-family: 'Ubuntu', sans-serif;
resize: none;
}
input {
border: 1px solid #c4c4c4;
border-radius: 5px;
font-family: 'Ubuntu', sans-serif;
width: 250px;
}
form {
padding: 10px;
}
section {
align-items: center;
justify-content: center;
height: 100vh;
width: 100%;
flex-direction: column;
font-family: 'Ubuntu', sans-serif;
clear: left;
}
label {
font-family: 'Ubuntu', sans-serif;
font-weight: 300px;
}
button {
border: 0px;
border-radius: 5px;
width: 100%;
height: 50px;
font-family: 'Ubuntu', sans-serif;
color: #38b9ff;
background-color: #bae7ff;
cursor: pointer;
font-size: 16px;
transition: 1.5s ease background-color;
}
button:hover {
background-color: #8ad6ff;
}
#status {
width: 100px;
height: 200px;
max-width: 500px;
text-align: center;
align-items: center;
padding: 10px;
margin: 0 auto;
border-radius: 10px;
}
#status.success {
width: 90%;
max-width: 500px;
text-align: center;
padding: 10px;
margin: 0 auto;
border-radius: 10px;
background-color: #7de000;
}
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Ubuntu&display=swap" rel="stylesheet">
<section>
<div class="div">
<form action="" method="get">
<label for="name">Name</label><br>
<input name="name" id="name"><br><br>
<label for="mail">G-Mail</label><br>
<input name="mail" id="mail"><br><br>
<label for="sub">Subject</label><br>
<input name="sub" id="sub"><br><br>
<label for="mess">Message</label><br>
<textarea id="mess" rows="5" col="80"></textarea><br><br>
<button type="submit">Submit</button>
</form>
</div>
<div id="status" class="success">Success</div>
</section>
Removing the last div , it's working well here in my machine. The color change. So remove the last div(yes, i know that you said it was only in the copy, but check again your code).
<section>
<div class="div">
<form action="" method="get">
<label for="name">Name</label><br>
<input name="name" id="name"><br><br>
<label for="mail">G-Mail</label><br>
<input name="mail" id="mail"><br><br>
<label for="sub">Subject</label><br>
<input name="sub" id="sub"><br><br>
<label for="mess">Message</label><br>
<textarea id="mess" rows="5" col="80"></textarea><br><br>
<button type="submit">Submit</button>
</form>
</div>
<div id="status" class='success'>Success</div>
</section>

Form Input element sizing

I want to make a two column layout form. but I am getting a problem i.e input elements of form are not equally divided in size(width) and also responsive.
I want it to be responsive and also equally width with gap between them.
Attached Code Below
#import url('https://fonts.googleapis.com/css?family=Montserrat');
h2 {
font-family: Montserrat;
font-size: 3em;
line-height:50px;;
}
form {
width:70%;
margin: 10% auto;
}
input[type="text"]{
display: block;
height: 30px;
width: 47%;
float:left;
}
form > input:nth-child(3){
margin-left: 31px;
margin-right:0;
}
textarea {
width:100%;
box-sizing: border-box;
margin: 30px 0;
}
input[placeholder="Name"],input[placeholder="E-mail"]{
padding: 10px;
letter-spacing: 5px;
font-family: Montserrat;
}
textarea[placeholder="Message"]{
letter-spacing:5px;
padding: 10px;
font-family: Montserrat;
}
/*
input , textarea {
border-width: 0 0 2px 0;
border-color: #000;
}*/
form input,textarea:focus {
outline:none;
}
<section id="contact-page">
<form>
<h2>Contact Us</h2>
<input type="text" placeholder="Name">
<input type="text" placeholder="E-mail">
<textarea name="message" placeholder="Message" rows="6"></textarea>
</form>
</section>
Try This
<form>
<div class="col-sm-6 col-xs-12 form-group">
<input type="text" name="" class="form-control">
</div>
<div class="col-sm-6 col-xs-12 form-group">
<input type="email" name="" class="form-control">
</div>
<div class="col-sm-12 form-group">
<textarea name="" cols="" rows="10" class="form-control"></textarea>
</div>
<form>
Just add this to your form section and also add the required files for botstrap
1- Jquery
2- Bootsrap.min.css
Bootsrap Documentation
JS fiddle
Remove float for inputs and add a wrapper div as below
h2 {
font-family: Montserrat;
font-size: 3em;
line-height:50px;;
}
form {
width:70%;
margin: 10% auto;
}
#email{
display: block;
height: 30px;
width: 30%;
float:left;
margin:0;
margin-left:2%;
}
#name{
display: block;
height: 30px;
width: 30%;
}
form > input:nth-child(3){
margin-left: 31px;
margin-right:0;
}
textarea {
width:100%;
box-sizing: border-box;
margin: 30px 0;
}
input[placeholder="Name"],input[placeholder="E-mail"]{
padding: 10px;
letter-spacing: 5px;
font-family: Montserrat;
}
textarea[placeholder="Message"]{
letter-spacing:5px;
padding: 10px;
font-family: Montserrat;
}
/*
input , textarea {
border-width: 0 0 2px 0;
border-color: #000;
}*/
form input,textarea:focus {
outline:none;
}
<section>
<form>
<h2>Contact Us</h2>
<div style="display: flex; justify-content: space-between;">
<input type="text" placeholder="Name">
<input type="text" placeholder="E-mail">
</div>
<textarea name="message" placeholder="Message" rows="6"></textarea>
</form>
</section>
If possible use Bootstrap with Grids, if not easy solution will be add div like this:
Bootstrap Grid LinK: https://www.w3schools.com/bootstrap/bootstrap_grid_system.asp
<form>
<h2>Contact Us</h2>
<div>
<input type="text" placeholder="Name">
</div>
<div>
<input type="text" placeholder="E-mail">
</div>
<textarea name="message" placeholder="Message" rows="6"></textarea>
</form>
Your selector form > input:nth-child(3) is a problem because it counts the h2 as the first, making the email-input the third and adding left-margin to it.
Once that is gone, I added margin: 30px 0; to the existing input['text'] selector, to match what you had applied to the textarea, and you may want different values, but everything is lined up and evenly spaced:
JS Fiddle
Example :
h2 {
font-family: Montserrat;
font-size: 3em;
line-height:50px;;
}
form {
width:70%;
margin: 10% auto;
}
#email{
display: block;
height: 30px;
width: 30%;
float:left;
margin:0;
margin-left:2%;
}
#name{
display: block;
height: 30px;
width: 30%;
float:left;
}
form > input:nth-child(3){
margin-left: 31px;
margin-right:0;
}
textarea {
width:100%;
box-sizing: border-box;
margin: 30px 0;
}
input[placeholder="Name"],input[placeholder="E-mail"]{
padding: 10px;
letter-spacing: 5px;
font-family: Montserrat;
}
textarea[placeholder="Message"]{
letter-spacing:5px;
padding: 10px;
font-family: Montserrat;
}
/*
input , textarea {
border-width: 0 0 2px 0;
border-color: #000;
}*/
form input,textarea:focus {
outline:none;
}
<section id="contact-page">
<form>
<h2>Contact Us</h2>
<input type="text" placeholder="Name" id='name'>
<input type="text" placeholder="E-mail" id='email'>
<textarea name="message" placeholder="Message" rows="6"></textarea>
</form>
</section>
Well ! i tried to figure out your problem and just gave a try to make it responsive.
Max-width and Max-height
The max-width property is used to set the maximum width of an element.
The max-width can be specified in length values, like px, cm, etc., or in percent (%) of the containing block, or set to none (this is default. Means that there is no maximum width).
Using max-width instead, in this situation, will improve the browser's handling of small windows. similarly for height
You can remove margin: 0 auto; if you want don't want your content centred
#import url('https://fonts.googleapis.com/css?family=Montserrat');
h2 {
white-space: auto;
font-family: Montserrat;
font-size: 3em;
line-height: 50px;
;
}
form {
width: 70%;
margin: 0 auto;
}
input[type="text"] {
max-height: 30px;
max-width: 47%;
}
textarea {
max-width: 100%;
box-sizing: border-box;
margin: 10px 0;
}
input[placeholder="Name"],
input[placeholder="E-mail"] {
padding: 10px;
letter-spacing: 5px;
font-family: Montserrat;
margin-bottom: 10px;
}
textarea[placeholder="Message"] {
letter-spacing: 5px;
padding: 10px;
font-family: Montserrat;
}
/*
input , textarea {
border-width: 0 0 2px 0;
border-color: #000;
}*/
form input,
textarea:focus {
outline: none;
}
<section id="contact-page">
<form>
<h2>Contact Us</h2>
<input type="text" placeholder="Name">
<input type="text" placeholder="E-mail">
<textarea name="message" placeholder="Message" rows="6"></textarea>
</form>
</section>

Aligning search box to center

I picked a good css/html code for implementation of search box on the web. But I can't move the search box to center. The below css code is about search box.
#search {
width: 357px;
margin: 4px;
}
#search_text{
width: 297px;
padding: 15px 0 15px 20px;
font-size: 16px;
font-family: Montserrat, sans-serif;
border: 0 none;
height: 52px;
margin-right: 0;
color: white;
outline: none;
background: #1f7f5c;
float: left;
box-sizing: border-box;
transition: all 0.15s;
}
<li id="search">
<form action="" method="get">
<input type="text" name="search_text" id="search_text" placeholder="Search"/>
<input type="button" name="search_button" id="search_button"/>
</form>
</li>
What should I fix the code for aligning the search box?
Thanks.
Try providing text-align: center to your body.
Refer code:
body {
text-align: center;
}
#search {
width: 357px;
margin: 4px auto;
text-align: center;
display: inline-block;
}
#search_text {
width: 297px;
padding: 15px 0 15px 20px;
font-size: 16px;
font-family: Montserrat, sans-serif;
border: 0 none;
height: 52px;
margin-right: 0;
color: white;
outline: none;
background: #1f7f5c;
float: left;
box-sizing: border-box;
transition: all 0.15s;
}
<li id="search">
<form action="" method="get">
<input type="text" name="search_text" id="search_text" placeholder="Search" />
<input type="button" name="search_button" id="search_button" />
</form>
</li>
If you want to align the text in the center of the textbox than use #nashcheez answer.
If you want to align the searchbox itself to the center of the parent than:
Set the textbox to display block
Give a with to the textbox
Set margin-left to auto
Set margin-right to auto
You may have to set position to relative.
you can try to use:
#search {
width: 357px;
margin-right: auto;
margin-left: auto;
}
if its being overridden, try using:
#search {
width: 357px;
margin-right: auto !important;
margin-left: auto !important;
}
If li#search is all you've got, try this:
#search {
width: 357px;
margin: 4px auto;
}
#search_text{
width: 297px;
padding: 15px 0 15px 20px;
font-size: 16px;
font-family: Montserrat, sans-serif;
border: 0 none;
height: 52px;
margin-right: 0;
color: white;
outline: none;
background: #1f7f5c;
float: left;
box-sizing: border-box;
transition: all 0.15s;
}
<li id="search">
<form action="" method="get">
<input type="text" name="search_text" id="search_text" placeholder="Search"/>
<input type="button" name="search_button" id="search_button"/>
</form>
</li>

Position a submit button inside div

Hi I am having some problems with the positioning of a submit button. I have posted the code below.
HTML
<div class="search">
<form action="post">
<input type="text" name="fname" placeholder="First name">
<input type="text" name="lname" placeholder="Last name">
<input type="submit" name="submit" value="Search">
</form>
</div>
CSS
.search {
position: absolute;
background-color: red;
width: 100%;
height: 30px;
}
input[type="text"] {
background-color: #f7f7f7;
height: 30px;
width: 280px;
color: #4D4C4C;
font-family: 'Open Sans', sans-serif;
font-size: 16px;
padding: 0px 5px 0px 5px;
-webkit-box-sizing: border-box;
moz-box-sizing: border-box;
box-sizing: border-box;
border: 1px solid rgba(147, 184, 189,0.8);
}
input[type="submit"] {
font-family: 'Open Sans', sans-serif;
font-size: 13px;
height: 30px;
width: 100px;
}
Here is an image showing the output. My problem is that there is a small space above the submit button and I cant find out why since they both have height set to 30px . I want the button to have the same height as the .search div. Height: 100% does not seem to work.
http://i57.tinypic.com/atswm0.png
Try adding:
input[type="submit"] {
position: absolute;
}

Whats the best way to center a form that's relative, the inputs inside float left and contains images

I'm making a form which has little images in them that is either a tick or cross. I want these to appear that they are in the text box so to do this I have added it to the form. This site is a mobile site so I want the form to be centered on what ever sized screen and the images to always be in the same place. My method at the moment feels quite messy and works on small screens unless I actually look at in on the phone then its a mess! So whats the best way to do this? Here was my stab at it
html
<div class="formContainer">
<form action="contact.php" method="post" class="ajax" id="contactForm">
<div class="input">
<input type="text" name="text" placeholder="Name" id="name">
<div class="validation">
<img src="check.svg" class="imageValidation" id="nameImage">
</div>
</div>
<div class="input">
<input type="text" name="email" placeholder="email" id="email">
<div class="validation">
<img src="check.svg" class="imageValidation" id="emailImage">
</div>
</div>
<div class="input">
<select name="subject" id ="subject">
<option value="1">Subject</option>
<option value="Private Hire">Private Hire</option>
<option value="Lost Items">Lost Items</option>
<option value="Work For Us"> Work For Us</option>
<option value="Other">Other</option>
</select>
<div class="validation">
<img src="check.svg" class="imageValidation" id="subjectImage">
</div>
</div>
<div class="input" id="message">
<textarea name="message" placeholder="Your Message"></textarea>
<div class="validation">
<img src="check.svg" class="imageValidation" id="messageImage">
</div>
</div>
<input name="robotest" type="text" id="robotest">
<div class="input">
<input type="submit" value="Submit">
</div>
</form>
<p id="emailConfrimation"> Email Sent </p>
</div>
the css:
.formContainer{
height: 90%;
width: 80%;
margin: 0 auto;
}
form{
width: 100%;
height: 100%;
text-align: center;
}
.input{
width: 100%;
height: 10%;
margin-top: 5%;
}
input{
float: left;
width: 99.9%;
height: 100%;
text-indent: 8%;
background-color: #dedede;
font-family: 'Source Sans Pro', sans-serif;
color: #3a3a3a;
font-size: 1em;
border: none;
-webkit-border-radius:0;
border-radius:0;
}
select{
float: left;
width: 99.9%;
height: 100%;
text-indent: 8%;
background-color: #dedede;
font-family: 'Source Sans Pro', sans-serif;
color: #3a3a3a;
font-size: 1em;
border: none;
-webkit-border-radius:0;
border-radius:0;
}
textarea{
float: left;
width: 98%;
height: 100%;
text-indent: 8%;
background-color: #dedede;
font-family: 'Source Sans Pro', sans-serif;
color: #3a3a3a;
font-size: 1em;
border: none;
-webkit-border-radius:0;
border-radius:0;
resize:none;
}
input[type='submit']{
margin: 0 auto;
display: block;
width: 100%;
height: 100%;
background-color: #bf3737;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
-webkit-border-radius:0;
border-radius:0;
border: none;
color: #ffffff;
font-family: 'Source Sans Pro', sans-serif;
font-size: 1.5em;
}
.validation{
float: left;
width: 0.01%;
position: relative;
right: 20%;
top: 0.5em;
}
thanks!
this is the best way to put your div at the center:
#your_div {margin:0 auto;}
but if you want that your div be exactly center horizontally and vertically , you should do this:
#main_div {position:relative}
#main_div #your_div {
position:absolute;
top:50%;
margin-top:-50px; */half of your div height*/
right:50%;
margin-right:-70px; */half of your div width*/
}