I'm trying to put a button right near to a text field.
Here the code I thought would make it work
<input class="txt" type="text" name="name">
<button class="btn">Submit</button>
.txt {
display: inline-block;
width: 80%;
outline: none;
margin:0;
}
.btn {
display: inline-block;
width: 20%;
margin:0;
}
http://jsfiddle.net/rm9etsny/
However, there is still a gap between the 2 elements. How can I remove it?
Thanks
Browsers always add spaces between some elements, including buttons. To remove these, you need to set font-size to zero for their parent container. Then, to restore text size inside buttons, set font-size for them.
Approach 1: We can remove space between any two tags by simply using margin-bottom property. The margin-bottom property sets the bottom margin of an element.
You can add more space between a button and text box by using “margin” attribute. If you want to add right side more space then add “margin- right”, for left side “magin-left”, for top side “margin-top”, for bottom “margin-bottom”.
You can put your html content inline. That will remove the space without having margin-left as negative
<input class="txt" type="text" name="name"><button class="btn">Submit</button>
Also to put them align in a horizontal row, you need to consider giving some less % to input.
Demo - http://jsfiddle.net/RahulB007/rm9etsny/5/
the issue is because inline-blocks give extra spacing between two items you can prevent it by using float:left
or adding parent with font-size:0
by using box-sizing:border-box
the padding is given from inside
eg
if the input width is 100%
and padding is 15px
so the total width will be 100% + 15px box-sizing:border-box
gives the padding
from inside
In my example i have changed the border to outset
demo - http://jsfiddle.net/rm9etsny/11/
body {
background-color: red;
}
div {
font-size:0;
}
.txt {
display: inline-block;
width: 80%;
font-size: 2em;
outline: none;
padding: 15px;
margin:0;
box-sizing:border-box;
}
.btn {
display: inline-block;
margin: 0;
outline: 0 none;
padding: 0;
width: 20%;
box-sizing:border-box;
}
<div>
<input class="txt" type="text" name="name" />
<button class="btn">Submit</button>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With