Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an element width: 100% minus padding?

I have an html input.

The input has padding: 5px 10px; I want it to be 100% of the parent div's width(which is fluid).

However using width: 100%; causes the input to be 100% + 20px how can I get around this?

Example

like image 590
Hailwood Avatar asked Mar 07 '11 11:03

Hailwood


People also ask

Does width 100% include padding?

The width property does not contain padding, borders, or margins. The width property is overridden by the min-width and max-width properties.

How do you set a padding percentage?

The padding size is relative to the width of that element's content area (i.e. the width inside, and not including, the padding, border and margin of the element). So, if your #wrapper is 940px wide, 5% padding = 0.05 × 940pixels = 47 pixels.

How do you make the width include padding?

The box-sizing property allows us to include the padding and border in an element's total width and height. If you set box-sizing: border-box; on an element, padding and border are included in the width and height: Both divs are the same size now!

How do I make padding not add width?

To avoid the width or height getting increased or decreased when using CSS properties like margin , padding , etc, we can use the CSS property called box-sizing and set its value to border-box on the element in CSS.


1 Answers

box-sizing: border-box is a quick, easy way to fix it:

This will work in all modern browsers, and IE8+.

Here's a demo: http://jsfiddle.net/thirtydot/QkmSk/301/

.content {     width: 100%;     box-sizing: border-box; } 

The browser prefixed versions (-webkit-box-sizing, etc.) are not needed in modern browsers.

like image 100
thirtydot Avatar answered Sep 24 '22 13:09

thirtydot