I want to make 3/4 input fields next to each other, with a margin in between. However, using a margin makes the last inputfield too big/too short(using calc).
Been trying to figure out how to do this but can't seem to find a solution
How my current CSS code looks like:
input {
background-color: darkgreen;
border: none;
color: white;
padding: 5px;
border-radius: 8px;
width: calc(33% - 20px);
box-sizing: border-box;
margin-right: 20px;
}
Background has a 75% width and padding
Current result
Wanted result
Hope this work but it will work for one row only
input {
background-color: darkgreen;
border: none;
color: white;
flot:left:
padding: 5px;
border-radius: 8px;
width: calc(33.333333% - 20px);
box-sizing: border-box;
}
input + input{
margin-left: 30px;
}
If you want to use for multiple rows then you can use below the structure.
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
:after,
:before {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.form-row {
margin-right: -15px;
margin-left: -15px;
}
.form-row:before,
.form-row:after {
display: table;
content: " ";
}
.input-box {
position: relative;
min-height: 1px;
padding-right: 15px;
padding-left: 15px;
width: 33.3333333%;
float: left;
}
.input-field {
display: block;
width: 100%;
height: 34px;
padding: 6px 12px;
font-size: 14px;
line-height: 1.42857143;
color: white;
background-color: darkgreen;
border: none;
border-radius: 8px;
}
<div class="form-row">
<div class="input-box">
<input type="text" class="input-field" value="input 01">
</div>
<div class="input-box">
<input type="text" class="input-field" value="input 02">
</div>
<div class="input-box">
<input type="text" class="input-field" value="input 03">
</div>
</div>
The problem is that all three of your elements have the full margin-right applied to them. If you want your final <input> element to stretch to the edge of the container, you'll want to only apply the margin-right to the first two <input> elements.
The best way to do this would be to combine the :not and :last-of-type pseudo-classes, as can be seen in the following:
input {
background-color: darkgreen;
border: none;
color: white;
padding: 5px;
border-radius: 8px;
width: calc(33% - 20px);
box-sizing: border-box;
}
input:not(:last-of-type) {
margin-right: 20px;
}
<input />
<input />
<input />
Note that this will have the added effect of making all three <input> elements slightly wider, as their width calculation is now derived from a larger container.
Related
I'm trying to put an image next to the form. I tried using inline-block and float method, but all suggestions I tried just messed up the contents. How can I put the image on the left side of the form?
My code is below:
contactContainer {
display: block;
text-align: center;
border-radius: 2px;
background-color: #f2f2f2;
padding: 20px;
}
.contactContainer form {
display: inline-block;
margin: 30px auto;
text-align: left;
}
.contactContainer img {
display: inline-block;
width: 500px;
}
.buttonHolder {
text-align: center;
}
input[type=Text], select, textarea {
width: 100%; /* Full width */
padding: 12px; /* Some padding */
border: 1px solid #ccc; /* Gray border */
border-radius: 2px; /* Rounded borders */
box-sizing: border-box; /* Make sure that padding and width stays in place */
margin-top: 6px; /* Add a top margin */
margin-bottom: 16px; /* Bottom margin */
resize: vertical /* Allow the user to vertically resize the textarea (not horizontally) */
}
input[type=Submit] {
background-color: gray;
color: white;
padding: 12px 20px;
border: none;
border-radius: 2px;
cursor: pointer;
align-items: center;
}
input[type=Submit]:hover {
background-color: white;
color: gray;
}
.twoContainer{
display: flex;
}
.twoContainer img {
width: 500px;
}
<div class="twoContainer">
<img src="images/tour2.jpg" alt="contact image">
<div class="contactContainer">
<form action="https://formspree.io/lyndall#lyndallwalker.com" method="POST">
<label for="name">Name</label>
<input type="text" id="name" name="name" placeholder="Jane Doe">
<label for="subject">Message</label>
<textarea id="subject" name="subject" placeholder=" " style="height:200px"></textarea>
<div class="buttonHolder">
<input type="submit" value="Submit">
</div>
</form>
</div>
</div>
Thank you in advance!
You need to add another div for the image but taking it out of
The contactContainer div. Then you'll wrap both of them (the image in a new div and the contact container div) in a third div (for example lets call it container). You want to remove the display properties that you have applied and you will give the container div this property
.container{
Display: flex;
}
If the image doesnt size ok you can try giving it a width of 50% and if not you can remove the img tag and apply it as an image background to the image Div.
.imagediv {
Width: 50%;
Background-image: url("yourimage.png");
}
Something like this should do it
You just need to set position: absolute to .contactContainer form and right:0 You can remove display: inline-block. It'll do the trick.
Here
HTML
<div class="twoContainer">
<img src="https://images.unsplash.com/photo-1581264296947-5a26af1aff18?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=600&q=60" alt="contact image" />
<div class="contactContainer">
<form
action="https://formspree.io/lyndall#lyndallwalker.com"
method="POST"
>
<label for="name">Name</label>
<input type="text" id="name" name="name" placeholder="Jane Doe" />
<label for="subject">Message</label>
<textarea
id="subject"
name="subject"
placeholder=" "
style="height: 200px"
></textarea>
<div class="buttonHolder">
<input type="submit" value="Submit" />
</div>
</form>
</div>
</div>
CSS
.contactContainer form {
position: absolute;
right: 0;
margin: 30px auto;
text-align: left;
margin-left: auto;
}
.contactContainer img {
display: inline-block;
width: 500px;
}
.buttonHolder {
text-align: center;
}
input[type="Text"],
select,
textarea {
width: 100%; /* Full width */
padding: 12px; /* Some padding */
border: 1px solid #ccc; /* Gray border */
border-radius: 2px; /* Rounded borders */
box-sizing: border-box; /* Make sure that padding and width stays in place */
margin-top: 6px; /* Add a top margin */
margin-bottom: 16px; /* Bottom margin */
resize: vertical; /* Allow the user to vertically resize the textarea (not horizontally) */
}
input[type="Submit"] {
background-color: gray;
color: white;
padding: 12px 20px;
border: none;
border-radius: 2px;
cursor: pointer;
align-items: center;
}
input[type="Submit"]:hover {
background-color: white;
color: gray;
}
.twoContainer {
display: flex;
flex-direction: row;
}
I have the below code to make a login form but i cant get the checkbox label to be like always against the edge of the text area. I always sits to the right of the text area. I cant get it to be dependant on the div it is in. On inspection it sits outside the div.
Different things i have tried have included giving the label a left value but this messes it up when the screen size changes.
I want something like this
Here is a jsfiddle if this is easier
function showHidePassword() {
var x = document.getElementById("pass");
if (x.type === "password") {
x.type = "text";
} else {
x.type = "password";
}
}
body {
background-color: #ffffff;
}
* {
box-sizing: border-box;
}
input[type=text],
select,
textarea {
width: 100%;
padding: 12px;
border: 1px solid #ccc;
border-radius: 4px;
resize: vertical;
}
input[type=password],
select,
textarea {
width: 100%;
padding: 12px;
border: 1px solid #ccc;
border-radius: 4px;
resize: vertical;
}
label {
padding: 12px 12px 12px 0;
display: inline-block;
}
input[type=submit] {
background-color: #4CAF50;
color: white;
padding: 12px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
float: right;
}
input[type=submit]:hover {
background-color: #45a049;
}
.container1 {
border-radius: 25px;
background-color: #f2f2f2;
padding: 40px;
position: center;
margin: 15% 30%;
}
.signup {
border-radius: 25px;
background-color: #f2f2f2;
padding: 40px;
position: center;
opacity: 0.96;
}
.container1 .new-body {
background: #f2f2f2;
}
.signup .new-body {
background: #f2f2f2;
}
.signup .row {
padding-top: 5px;
}
.col-25 {
float: left;
width: 25%;
margin-top: 6px;
}
.col-75 {
float: left;
width: 65%;
margin-top: 6px;
}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
/* Responsive layout - when the screen is less than 600px wide, make the two columns stack on top of each other instead of next to each other */
#media screen and (max-width: 600px) {
.col-25,
.col-75,
input[type=submit] {
width: 100%;
margin-top: 0;
}
.col-70,
input[type=submit] {
width: 95%;
margin-top: 0;
}
}
.passw {
cursor: pointer;
width: 30px;
height: 20px;
}
.col-75 label {
padding-top: 16px;
position: absolute;
z-index: 100;
}
<form>
<div class="row">
<div class="col-25">
<label for="pass">Password</label>
</div>
<div class="col-75">
<input type="password" id="pass" name="password" minlength="5" pattern="[A-Za-z][A-Za-z0-9]*[0-9][A-Za-z0-9]*" placeholder="Password" title="A valid password is a set of 5 characters, each consisting of an
upper or lower-case letter, or a digit. The password must begin with a letter and contain at least one digit" autocomplete="current-password" required>
<label for="passShowIcon" id="showHide"><input name="passShowIcon" type="checkbox" class="passw" onclick="showHidePassword();">
<span class=" "></span></label>
</div>
</div>
<div class="row">
<input type="submit" value="Submit">
</div>
</form>
If you wanted to make sure the checkbox appears inside the text input. You could wrap both input fields with a relative class, and then apply absolute positioning to the checkbox.
Like so:
https://jsfiddle.net/x0o46g7a/2/
.wrapper {
position: relative;
}
.text {
width: 100%;
height: 100%;
}
.checkbox {
position: absolute;
top: -8px;
right: -8px;
}
Something to note:
I would recommend adding some padding-right to your text input, to make sure it's text does not overlap/underlap the absolute positioned checkbox.
Based on your code, add the following rules in your css.
float: right to .col-75 instead of float left
right: 0 to .col-75 label
those will ensure that checkbox will remain inside the input field.
My assignment is to make a login page, and the inputs and buttons have to have a 320px width, including a 2px border and 15px padding. I create two classes for the inputs and button, and in CSS I am specifying these widths for both but the button keeps coming out shorter. Right now it's coming out like this: 1
I'm fairly new at this so I apologize if my code is messy/this might seem like a silly question.
Here's my code:
HTML
<form class="signup" action="/signup/" method ="post">
<fieldset name="sign-up">
<legend>Sign up</legend>
<div class="input">
<label for="email">Email</label></br>
<input class="inputbar" placeholder="foo#bar.com" type="email" name="email" id="email" required/></br>
<label for="password">Password</label></br>
<input class="inputbar" placeholder="1234passw0rd" type="password" name="password" id"password" required/></br>
</div>
</fieldset>
<button type="signupbutton">Sign up</button>
</form>
CSS
.signup {
width: 320px;
padding: 40px;
margin-top: 30px;
margin-left: auto;
margin-right: auto;
text-align: center;
background-color: white;
}
.signup fieldset{
border: none;
}
.input{
text-align: left;
}
.inputbar, button{
border: 2px solid lightgrey;
border-radius: 2px;
padding: 15px;
width: 250px;
}
button{
background-color: mediumseagreen;
color: white;
}
Thanks everyone!
It's best to set the container width and then the elments inside to 100%. Using box-sizing: border-box; is key here.
* {
box-sizing: border-box;
}
.signup {
width: 320px;
padding: 40px;
margin-top: 30px;
margin-left: auto;
margin-right: auto;
text-align: center;
background-color: white;
}
.signup fieldset{
border: none;
padding: 0;
margin: 0;
}
.input{
text-align: left;
}
.inputbar, button{
border: 2px solid lightgrey;
border-radius: 2px;
padding: 15px;
width: 100%;
}
button{
background-color: mediumseagreen;
color: white;
}
.inputbar, button{
border: 2px solid lightgrey;
border-radius: 2px;
padding: 15px;
width: 250px;
}
It appears you have set the width of the button to 250px. Try changing that to 320px.
Remove the text align on th input class
.input{
text-align: left;
}
and do this
.signup .input label {
text-align: left;
display: inline-block;
width: 100%;
padding-left: 20px;
}
Aside that the everything is working alright in chrome
what browser are you using to run your test?
this is whati have on my browser
I have a problem with HTML forms.
#subscription {
display: block;
margin: auto;
background-color: blue;
width: 550px;
height: auto;
}
form#subscription input#subscription-text {
width: 200px;
height: 30px;
background: orange;
border-style: none;
}
form#subscription input#subscription-submit {
width: 200px;
height: 30px;
background-color: rgb(208, 225, 125);
border-style: none;
padding: 0;
margin: 0;
}
<form id="subscription" action="subscription">
<input id="subscription-text" type="text" placeholder="INPUT">
<input id="subscription-submit" type="submit" value="SUBMIT">
</form>
Despite the fact, that I have removed all the margins and paddings for a submit button, it still has a padding-like VERTICAL spacing:
Why is that so, and how could I remove this spacing?
In fact there are TWO issues here...
The horizontal spacing is cause by whitespace in the HTML which affects inline/inline-block elements.
That subject is covered extensively in How to remove the space between inline-block elements?
The second issue is the disparity in the sizes of the two inputs.
This is caused by the fact the the two input types have different box-sizing default properties.
So we apply an overriding default reset:
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
and a solution to the whitespace (here I used floats and a quick overflow clearfix)
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
form#subscription {
display: block;
margin: auto;
background-color: blue;
width: 550px;
overflow: auto;
}
form#subscription input#subscription-text {
width: 200px;
height: 30px;
background: orange;
border-style: none;
float: left;
}
form#subscription input#subscription-submit {
width: 200px;
height: 30px;
background-color: rgb(208, 225, 125);
border-style: none;
float: left;
}
<form id="subscription" action="subscription">
<input id="subscription-text" type="text" placeholder="INPUT">
<input id="subscription-submit" type="submit" value="SUBMIT">
</form>
you have two problem.
1. positioning of element and
2. box-sizing problem.
add the two line of code in the respective section of your css code as shown below.
form#subscription input#subscription-text {
width: 200px;
height: 30px;
background: orange;
border-style: none;
float: left; // this line solves prob-1
box-sizing: border-box; // this line solves prob-2
}
Learn about box-sizing here: https://css-tricks.com/box-sizing/
Learn about float here: https://developer.mozilla.org/en/docs/Web/CSS/float
I am trying to create a custom div with input text and two buttons inside it as shown below.
But when i resize the screen it becomes like this
Is there a way to avoid the two buttons to come down ? Instead it should remain inside the original div.
Here's the code i tried:
.searchBar {
background: #DDDDDD;
width:100%;
height:50px;
padding: 10px;
position: relative;
}
.search_field {
display: inline-block;
border-radius:4px ;
background: #FFFFFF;
width: 70%;height: 32px;
position: relative;
left: 60px;
overflow: inherit;
}
.search_field input {
width: 89%;
padding: 0;
border-top-left-radius: 4px;
padding-left: 10px;
padding-top: 5px;
padding-bottom: 5px;
border:1px inset red;
}
.search_field input:focus {
outline-color: transparent;
outline-style: none;
}
.search_field button {
border: none;
background: none;
}
<div id="searchBar" class="searchBar">
<div class="search_field">
<input type="text" id="searchInput" placeholder="Search" oninput="showSearchButtons()"/>
<button id="btn1" hidden="true" onclick="alert()"><img src="assets/images/search.png"></button>
<button id="btn2" hidden="true" onclick="alert()"><img src="assets/images/saveBtn.png"></button>
</div>
</div>
Any help is appreciated. Thanks
You can use calc to calculate the width of your input element relative to your buttons:
width: calc(100% - 100px);
Just make sure the width of your buttons is taken of the 100%. In SASS it could look like this:
$buttons: 50px;
width: calc(100% - #{$buttons * 2});
Below is a simplified implementation. I still have the % values as a fallback for older browsers - but that's more a habit than necessity as every major browser supports calc, even IE9 and onward.
input, button {
float: left;
height: 50px;
display: block;
box-sizing: border-box;
margin: 0;
padding: 0;
border: none;
}
input {
width: 70%;
width: calc(100% - 100px);
padding: 10px;
}
button {
/* Note that this width is equal to 100%
/* minus the percentage width of the input
/* divided by the amount of buttons. */
width: 15%;
width: 50px;
line-height: 50px;
}
/* This rule is just to make sure your images don't decide the buttons width */
button img {
max-width: 100%;
display: inline-block;
}
<input type='text' placeholder='search' />
<button><img src="http://placehold.it/50x50" /></button>
<button><img src="http://placehold.it/50x50" /></button>
Please try this instead of your styles:
.searchBar{
background: #DDDDDD;
width:100%;
height:50px;
padding: 10px;
position: relative;
}
.search_field {
border-radius:4px ;
background: #FFFFFF;
position: relative;
padding-right: 100px; /* You can change as button width */
}
.search_field input {
width: 100%;
padding: 0;
padding-left: 10px;
padding-top: 5px;
padding-bottom: 5px;
border: solid 1px #FF0000;
-webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
-moz-box-sizing: border-box; /* Firefox, other Gecko */
box-sizing: border-box;
}
.search_field button {
border: none;
background: none;
position: absolute;
top: 0;
}
.search_field button#btn1 {
right: 50px; /* Change as your requirement */
}
.search_field button#btn2 {
right: 0; /* Change as your requirement */
}