Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove gap between input and button

Tags:

html

css

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

like image 268
Sig Avatar asked Nov 08 '14 03:11

Sig


People also ask

How do you get rid of spaces between buttons?

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.

How can I remove space between two input fields in HTML?

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.

How do you put a space between input and button?

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”.


2 Answers

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/

like image 59
RahulB Avatar answered Oct 06 '22 09:10

RahulB


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>
like image 25
Vitorino fernandes Avatar answered Oct 06 '22 10:10

Vitorino fernandes