Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input overflows container

Tags:

html

css

I have a problem where setting width: 100% to inputs inside a container extends more than the container capacity. This problem doesn't seems to happen with buttons though:

<form>    
  <input type="text" class="input"></input>    
  <button>Button</button>
</form>

CSS:

form {
  max-width: 200px;  
  border: 1px solid #eee;   
}

.input, button {
  width: 100%;
}

In this example, the button correctly fills the container, however the input extends a bit further:

enter image description here

How can I fix this?

I've created a codepen: http://codepen.io/jviotti/pen/qfFmH

like image 306
jviotti Avatar asked Feb 27 '14 15:02

jviotti


People also ask

Why is input bigger than div?

It means that border on the input box is actually inside the width of the input rather than being added onto the outside. This is what is making the input larger than the container. The points he makes about padding also apply for the border. As noted in other answers, it may need width: 100%; height: 100% .

Why is my Div overflowing?

Overflow happens when there is too much content to fit in a box. CSS provides various tools to manage overflow. As you go further with CSS layout and writing CSS, you will encounter more overflow situations.


1 Answers

You can fix this adding

.input, button {
  width: 100%;
  box-sizing: border-box; /* add this */
      -webkit-box-sizing: border-box;
      -moz-box-sizing: border-box;
}

box-sizing - https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing

like image 124
web-tiki Avatar answered Nov 14 '22 23:11

web-tiki