This question already has answers here:
spacing between form fields
(4 answers)
Closed 3 years ago.
I'm working on an autocomplete form, but I seem to have some issues with the padding between the text and submit form. I cannot seem to adjust the spacing between the text and submit items, which I need because when I increase the size of the text field, it overwrites the submit button. I believe I've tried all types of padding but they do nothing. Can someone give me a clue please?
.autocomplete {
position: relative;
display: inline-block;
}
input {
border: 1px solid;
padding: 5px;
font-size: 24px;
}
input[type=text] {
border-color: #808080;
background-color: #fff;
width: 100%;
-webkit-border-radius: 5px;
padding: 5px;
}
input[type=submit] {
margin-left: 5px;
padding: 5px;
background: #ccc;
cursor: pointer;
-webkit-border-radius: 5px;
border: 1px solid;
background-color: DodgerBlue;
border-color: #CCCCCC;
color: #fff;
}
<form autocomplete="off" action="/action_page.php">
<div class="autocomplete" style="width:300px;">
<input id="myInput" type="text" name="myCountry" placeholder="Country">
</div>
<input type="submit">
</form>
Add box-sizing: border-box to your input[type=text] - the default content-box setting means that when you add padding to an element it increases its dimensions.
See demo below:
.autocomplete {
position: relative;
display: inline-block;
}
input {
border: 1px solid;
padding: 5px;
font-size: 24px;
}
input[type=text] {
border-color: #808080;
background-color: #fff;
width: 100%;
-webkit-border-radius: 5px;
padding: 5px;
box-sizing: border-box; /* added */
}
input[type=submit] {
margin-left: 5px;
padding: 5px;
background: #ccc;
cursor: pointer;
-webkit-border-radius: 5px;
border: 1px solid;
background-color: DodgerBlue;
border-color: #CCCCCC;
color: #fff;
}
<form autocomplete="off" action="/action_page.php">
<div class="autocomplete" style="width:300px;">
<input id="myInput" type="text" name="myCountry" placeholder="Country">
</div>
<input type="submit">
</form>
You can see more examples here:
Add border to div increase div width?
border-box isn't working as expected
Have you tried using between your input text and submit button?
.autocomplete {
position: relative;
display: inline-block;
}
input {
border: 1px solid;
padding: 5px;
font-size: 24px;
}
input[type=text] {
border-color: #808080;
background-color: #fff;
width: 100%;
-webkit-border-radius: 5px;
padding: 5px;
}
input[type=submit] {
margin-left: 5px;
padding: 5px;
background: #ccc;
cursor: pointer;
-webkit-border-radius: 5px;
border: 1px solid;
background-color: DodgerBlue;
border-color: #CCCCCC;
color: #fff;
}
<form autocomplete="off" action="/action_page.php">
<div class="autocomplete" style="width:300px;">
<input id="myInput" type="text" name="myCountry" placeholder="Country">
</div>
<input type="submit">
</form>
You can increase the value of margin-left in input[type=submit] {} like shown below:
input[type=submit] {
margin-left: 10px; /* increased value */
padding: 5px;
background: #ccc;
cursor: pointer;
-webkit-border-radius: 5px;
border: 1px solid;
background-color: DodgerBlue;
border-color: #CCCCCC;
color: #fff;
}
or add margin right to the previous element like shown below:
input[type=text] {
border-color: #808080;
background-color: #fff;
width: 100%;
-webkit-border-radius: 5px;
padding: 5px;
margin-right: 5px; /* add margin right */
}
Related
I want to grow the element background and font on hover. However it does impact all other elements position like the border of the form, elements side.
I tried to do it with the box-shadow but only the shadow is enlarged and not the text.
please Help
LINk: Code for HTML and CSS
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
text-align: center;
}
.contenedor {
display: inline-block;
width: 400px;
height: 400px;
}
form {
padding: 30px;
border: 1px solid #ccc;
}
input[type="submit"] {
width: 80px;
background: #24b080;
padding: 10px;
border: 1px solid #24b080;
cursor: pointer;
color: white;
margin-top: 10px;
margin-bottom: 10px;
transition: all 0.3s ease-out;
}
input[type="submit"]:hover {
background-color: #24b080;
border: 2px solid #24b080;
color: #fff;
transform: scale(1.2, 1.2);
}
<form name="formulario" action="">
<input type="submit" value="Send">
<input type="submit" value="OK">
<input type="submit" value="Cancel">
</form>
You can change this
form {
padding: 30px;
border: 1px solid #ccc;
}
to this:
form {
padding: 30px;
border: 1px solid #ccc;
height: 120px;
}
If you give the Form a fixed height then it cannot move when the buttons get bigger.
You can apply vertical-align: top; to input[type="submit"] to avoid the slight vertical movement on hover (I guess that's what you meant?):
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
text-align: center;
}
.contenedor {
display: inline-block;
width: 400px;
height: 400px;
}
form {
padding: 30px;
border: 1px solid #ccc;
}
input[type="submit"] {
width: 80px;
background: #24b080;
padding: 10px;
border: 1px solid #24b080;
cursor: pointer;
color: white;
margin-top: 10px;
margin-bottom: 10px;
transition: all 0.3s ease-out;
vertical-align: top;
}
input[type="submit"]:hover {
background-color: #24b080;
border: 2px solid #24b080;
color: #fff;
transform: scale(1.2, 1.2);
}
<form name="formulario" action="">
<input type="submit" value="Send">
<input type="submit" value="OK">
<input type="submit" value="Cancel">
</form>
Id like to have a text input with a submit button inside the input and have the edges rounded using something like border-radius
Im using the css from the second answer in this question:
How to add button inside input
#form {
display:flex;
flex-direction:row;
border:1px solid grey;
padding:2px;
border-radius:50px; /* this is me trying to round edges of input*/
}
#input {
flex-grow:2;
border:none;
}
but when I add that border-radius the button no longer appears inside the input
how could I do it?
There are a couple of errors in your code. First, form and input are native tags, they don't need a hashtag in the css. If you assign an id to the form, that wil be your #id (i've given the form an id of 'form' for demo purposes.
You can give the form a border radius, but to make the buttons rounded without rounding the entire form, you need to add a border-radius to the input. You can assign a border-radius to all inputs by using input (general) or specify the type input[type=submit] in your css
Hope this helps
#form {
width: 60%;
display: flex;
flex-direction: row;
border: 1px solid grey;
padding: 2px;
border-radius: 50px; /*you can remove this to avoid rounded corners on the form*/
}
input {
flex-grow: 2;
border: none;
border-radius: 50px;
}
<form id="form">
<input type="text" placeholder="text" />
<input type="submit" value="Click here" />
</form>
How about something like this? The <form> is now just a flex container, and the border styles are actually applied to the input and the button.
#my-form {
display: flex;
flex-direction: row;
}
#my-form input,
#my-form button {
border-radius: 10px;
border: 1px solid grey;
padding: 2px;
}
#my-form input {
flex-grow: 2;
border-right-width: 0;
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}
#my-form button {
background: teal;
color: white;
border-left-width: 0;
border-top-left-radius: 0;
border-bottom-left-radius: 0;
}
<form id="my-form">
<input type="text" />
<button type="submit">my button</button>
</form>
:root {
--border-width: 4px;
--border-radius: 10px;
--height: 40px;
}
input[type="text"],
input[type="submit"] {
transition: box-shadow .3s;
}
input[type="text"] {
width: 200px;
height: var(--height);
padding: 3px 60px 3px 5px;
border-radius: calc(var(--border-width, 1px) + var(--border-radius, 10px));
border-width: var(--border-width, 1px);
box-sizing: border-box;
border-color: blue;
border-style: solid;
}
input[type="text"]:hover,
input[type="text"]:focus {
outline: none;
box-shadow: 0px 0px 2px 3px blue;
}
input[type="submit"] {
transform: translateX(calc(-100% - var(--border-width, 1px)));
height: calc(var(--height) - 2 * var(--border-width));
background: blue;
color: white;
border: 0;
-webkit-appearance: none;
border-radius: 0 var(--border-radius, 10px) var(--border-radius, 10px) 0;
}
input[type="submit"]:hover,
input[type="submit"]:focus {
outline: none;
box-shadow: inset 0px 0px 2px 3px white;
}
<input type="text"><input type="submit">
You have to add a border and the border-radius to the input text element.
In the example you linked, it would be like this:
input[type="text"] {
width: 200px;
height: 20px;
padding-right: 50px;
border: 2px solid black;
border-radius: 30px;
}
EDIT:
If the border is supposed to go around both elements, then you could add this CSS to the button:
border: 3px solid black;
border-left: 0;
border-radius: 0 30px 30px 0;
Like here: http://jsfiddle.net/jeft24m6/
This is my current input field styling however for some reason which I am unable to see, they are not aligning properly next to each other.
Code:
<div class="labelInputField">Confirm Password</div><input type="text" class="inputField">
.labelInputField {
border-top-left-radius: 5px;
border-bottom-left-radius: 5px;
padding: 8px;
width: 150px;
color: white;
background: rgba(0,0,0,1);
border: 2px solid #000;
display: inline-block;
height: 30px;
margin-top: 15px;
font-size: 16px;
}
.inputField {
border-top-right-radius: 5px;
border-bottom-right-radius: 5px;
padding: 8px;
width: 180px;
color: white;
background: rgba(0,0,0,0.4);
border: 2px solid #000;
display: inline-block;
height: 30px;
font-size: 16px;
}
JSFiddle:
https://jsfiddle.net/0Lsake9j/
Any solutions?
Remove margin-top:15px and add line-height:30px; in .labelInputField style. That will make your div and input alignment proper.
.labelInputField {
border-top-left-radius: 5px;
border-bottom-left-radius: 5px;
padding: 8px;
width: 150px;
color: white;
background: rgba(0,0,0,1);
border: 2px solid #000;
display: inline-block;
height: 30px;
line-height:30px;
font-size: 16px;
}
.inputField {
border-top-right-radius: 5px;
border-bottom-right-radius: 5px;
padding: 8px;
width: 180px;
color: white;
background: rgba(0,0,0,0.4);
border: 2px solid #000;
display: inline-block;
height: 30px;
font-size: 16px;
}
<div class="labelInputField">Confirm Password</div><input type="text" class="inputField">
You can also test it here
Offload layout responsibility to a container and/or additional layers of markup.
Remove margin-top on the div
Use a <label for="[YOUR_INPUT_ID]"></label> and ensure your input has id="[YOUR_INPUT_ID]".
Add type="password" on your input, unless you have a specific reason not to.
Remove existing layouts styles from selectors that are styling the input and label (i.e. inline-block in this case.).
Apply your layout styling with an object that's sole purpose is to handle layout of your elements
.iHandleLayout {
display: flex;
/* rest of your layout styling etc. */
}
.labelInputField {
border-top-left-radius: 5px;
border-bottom-left-radius: 5px;
padding: 8px;
min-width: 150px;
color: white;
background: rgba(0,0,0,1);
border: 2px solid #000;
font-size: 16px;
}
.inputField {
border-top-right-radius: 5px;
border-bottom-right-radius: 5px;
padding: 8px;
width: 180px;
color: white;
background: rgba(0,0,0,0.4);
border: 2px solid #000;
font-size: 16px;
}
<div class="iHandleLayout">
<label class="labelInputField" for="password">Confirm Password</label>
<input type="password" class="inputField" id="password">
</div>
In the end, there are many ways to control the layout (display: inline-block, flex, table, etc), but the main principle is the separation of concerns with your styling and the markup structure as part of that separation.
I am trying to have some pre-input on my forms and I want the input field to expand to the end of the container div. The problem is the input field seems to expand past the container div due to the length of the pre-input text.
.container {
width: 70%;
}
.input-field {
display: inline-block;
white-space: nowrap;
vertical-align: bottom;
background: #fff;
position: relative;
vertical-align: middle;
width: 100%;
min-width: 16px;
border: 1px solid #ddd;
}
.input-field .addon {
padding-left: 8px;
padding-right: 8px;
padding-top: 2px;
padding-bottom: 2px;
text-align: center;
text-shadow: 0 1px 0 #fff;
background: #f0f0f0;
}
.input-field input {
padding-left: 5px;
padding-top: 0;
padding-bottom: 0;
margin: 0;
border: 0;
outline: none;
background: transparent;
resize: none;
width: 100%;
}
.input-field:hover {
border: 1px solid #05c9f0;
}
<div class="container">
<div class="input-field">
<span class="addon">some preinput</span>
<input id="" type="text" name="" value="test text" />
</div>
</div>
Another example here: http://codepen.io/anon/pen/wgdErO
If you try the example code, you will note you can click the input box way outside where I want it to end.
Any thoughts on a fix?
Try using the flexbox layout.
.container {
width: 300px;
}
.input-field {
background: #fff;
border: 1px solid #ddd;
display: flex;
align-items: center;
}
.input-field .addon {
padding: 2px 8px;
text-shadow: 0 1px 0 #fff;
background: #f0f0f0;
}
.input-field input {
padding: 0 4px;
margin: 0;
border: 0;
outline: none;
background: transparent;
resize: none;
flex: 1;
}
.input-field:hover {
border: 1px solid #05c9f0;
}
<div class="container">
<div class="input-field">
<span class="addon">some preinput</span>
<input id="" type="text" name="" value="test text" />
</div>
</div>
<img src="//dummyimage.com/300x50">
Just decrease the input width from 100% to a lower value like 99.7% and that will do the trick:
.container {
width: 70%;
border: 1px solid red;
}
.input-field {
display: inline-block;
white-space: nowrap;
vertical-align: bottom;
background: #fff;
position: relative;
vertical-align: middle;
width: 99.7%;
min-width: 16px;
border: 1px solid #ddd;
}
.input-field .addon {
padding-left: 8px;
padding-right: 8px;
padding-top: 2px;
padding-bottom: 2px;
text-align: center;
text-shadow: 0 1px 0 #fff;
background: #f0f0f0;
}
.input-field input {
padding-left: 5px;
padding-top: 0;
padding-bottom: 0;
margin: 0;
border: 0;
outline: none;
background: transparent;
resize: none;
width: 100%;
}
.input-field:hover {border: 1px solid #05c9f0;}
<div class="container">
<div class="input-field">
<span class="addon">some preinput</span>
<input id="" type="text" name="" value="test text" />
</div>
</div>
You can calculate the width like this! width: calc(100% - 125px); rather than set it to 100%
I am trying to disable/remove the border or the blue glow in the text box.
.user {
width: 300px;
height: 50px;
background-color: rgba(222,222,222,.0);
border: 2px solid rgba(222,222,222,0);
padding-left: 5px;
padding-right: 5px;
font-family: arial;
margin-bottom: -8px;
color: #ffffff;
}
.user::-webkit-input-placeholder {
color: #ffffff;
}
.password {
width: 300px;
height: 50px;
border: #e9e9e9;
background-color: rgba(222,222,222,.0);
border: 2px solid rgba(222,222,222,0);
padding-left: 5px;
padding-right: 5px;
font-family: arial;
margin-top: -8px;
}
.password::-webkit-input-placeholder {
color: #ffffff;
}
The form part:
<form method="post" class="login_form">
<div style="font-size: 20px; font-family: bebasregular; text-align: right; color: #ffffff;">Login</div>
<input type="text" name="user" class="user" id="user" placeholder="Username or Email"/>
<hr/>
<input type="password" name="password" class="password" placeholder="Password"/><br/>
<input type="submit" name="login_btn" class="login_btn" value="Login"/>
</form>
Now, what I'm trying to make is that the two text boxes in the form will become transparent, even they're in focus.
i tried this code below, but it doesn't work.
.user{
width: 300px;
height: 50px;
background-color: rgba(222,222,222,.0);
border: 2px solid rgba(222,222,222,0);
padding-left: 5px;
padding-right: 5px;
font-family: arial;
margin-bottom: -8px;
color: #ffffff;
}
.user:focus{
width: 300px;
height: 50px;
background-color: rgba(222,222,222,.0);
border: 2px solid rgba(222,222,222,0);
padding-left: 5px;
padding-right: 5px;
font-family: arial;
margin-bottom: -8px;
color: #ffffff;
}
.user::-webkit-input-placeholder {
color: #ffffff;
}
.password {
width: 300px;
height: 50px;
border: #e9e9e9;
background-color: rgba(222,222,222,.0);
border: 2px solid rgba(222,222,222,0);
padding-left: 5px;
padding-right: 5px;
font-family: arial;
margin-top: -8px;
}
.password:focus {
width: 300px;
height: 50px;
border: #e9e9e9;
background-color: rgba(222,222,222,.0);
border: 2px solid rgba(222,222,222,0);
padding-left: 5px;
padding-right: 5px;
font-family: arial;
margin-top: -8px;
}
.password::-webkit-input-placeholder {
color: #ffffff;
}
You can remove this by adding: outline: none; to the .user class (or any element that may receive the outline):
JS Fiddle
Sidenote:
I think its worth noting that on your hover states, you only need to specify the properties that change on that that state. For instance, if the element text color is white initially, and you want it to still be white on hovered state, you can omit that on the hover state.
The bluish glow is coming from the default outline styles. To remove it try:
input {
outline: none;
}
This will remove it from the user, password, and when the button is pressed.
JS Fiddle
use
input {
outline: none;
}
or change
border: 2px solid rgba(222,222,222,0);
to
border: 0px;