Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML input element wider than Containing Div

Tags:

html

css

I have an html element that is contained within a div. Height are dictated by the outer div and the height and width of the input control are 100%. At the most basic level, I am having an issue where the textbox extends past the right of the containing div.

Basic example code:

<div style="height:25px; width: 150px;">      <input type="text" style="height:100%; width:100%"  /> </div> 

The rendering of this control is far more complex than this, but still when the control is stripped down to this level, I have an issue where the textbox sticks out past the containing div.

like image 970
BrandonS Avatar asked Oct 27 '09 20:10

BrandonS


People also ask

How do you make the input field wider in HTML?

Set width in HTML In HTML, you can use the width attribute to set the width of an element. Alternatively, you can also use the size attribute to define the width of the <input> .

How do I make Div larger than content?

Answer: Use the CSS display Property You can simply use the CSS display property with the value inline-block to make a <div> not larger than its contents (i.e. only expand to as wide as its contents).

How do you write input to width?

Definition and UsageThe width attribute specifies the width of the <input> element. Note: The width attribute is used only with <input type="image"> . Tip: Always specify both the height and width attributes for images. If height and width are set, the space required for the image is reserved when the page is loaded.

Does padding add to the width of an element?

Padding and Element Width The content area is the portion inside the padding, border, and margin of an element (the box model). So, if an element has a specified width, the padding added to that element will be added to the total width of the element.


2 Answers

You can use box-sizing:border-box to take care of this. Just put the following in your css file:

input{box-sizing:border-box}  

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.

Paul Irish has really good post explaining this technique http://paulirish.com/2012/box-sizing-border-box-ftw

The points he makes about padding also apply for the border.

There's even a compass mixin to make it easier to support older browsers. (http://compass-style.org/reference/compass/css3/box_sizing/)

like image 152
heedfull Avatar answered Sep 18 '22 12:09

heedfull


This did the job for me :

input {   padding: 0.2em;    box-sizing: border-box;   width: 100%  }  
like image 28
Raphayol Avatar answered Sep 20 '22 12:09

Raphayol