2

I'm trying put a input and a button display inline, where input need fill 70% of row, and button 30%

So, I set css 70% and 30% to elements, but elements will break line

How to can I fix this problem?

* {
  margin: 0px;
}

input {
  width: 70%;
}

button {
  width: 30%;
}
<div>
  <input type="text" />
  <button>Button</button>
</div>
Jamie Barker
  • 8,145
  • 3
  • 29
  • 64
Lai32290
  • 8,062
  • 19
  • 65
  • 99

2 Answers2

7

Add this:

* {
   box-sizing:border-box;
}

It's because the input/button has padding and border added by the browser so the total ends up > 100%.

What the code above does is tell all elements to include padding and border into the width value.

You will also be suffering from a white-spacing issue. Basically because your elements are on a separate line, it adds white space in-between. To combat this you can either put them on the same line with no space or comment the area in-between, like so:

* {
  box-sizing: border-box;
  margin: 0px;
}
input {
  width: 70%;
  border: 3px solid;
}
button {
  width: 30%;
}
<div>
  <input type="text"><!--
  --><button>Button</button>
</div>

Setting the div element to have font-size: 0px; would also work to remove the white-space, as long as you're not affecting the relative font-size of any child elements by doing so.

Jamie Barker
  • 8,145
  • 3
  • 29
  • 64
-1

The button has a border by default so 30 plus 70 is actually a little above 100% try this:

input {
  float: left;  
  width: 69%;
}
button {
  float: left;
  margin: 0px;
  width: 30%;
  box-sizing: border-box;
}
Adam Buchanan Smith
  • 9,422
  • 5
  • 19
  • 39