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

HtmlCssWidthFluid Layout

Html Problem Overview


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

Html Solutions


Solution 1 - Html

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.

Solution 2 - Html

This is why we have box-sizing in CSS.

I’ve edited your example, and now it works in Safari, Chrome, Firefox, and Opera. Check it out: http://jsfiddle.net/mathias/Bupr3/ All I added was this:

input {
  -webkit-box-sizing: border-box;
     -moz-box-sizing: border-box;
          box-sizing: border-box;
}

Unfortunately older browsers such as IE7 do not support this. If you’re looking for a solution that works in old IEs, check out the other answers.

Solution 3 - Html

Use padding in percentages too and remove from the width:

padding: 5%;
width: 90%;

Solution 4 - Html

You can do it without using box-sizing and not clear solutions like width~=99%.

Demo on jsFiddle:
enter image description here

  • Keep input's padding and border
  • Add to input negative horizontal margin = border-width + horizontal padding
  • Add to input's wrapper horizontal padding equal to margin from previous step

HTML markup:

<div class="input_wrap">
	<input type="text" />
</div>

CSS:

div {
	padding: 6px 10px; /* equal to negative input's margin for mimic normal `div` box-sizing */
}

input {
	width: 100%; /* force to expand to container's width */ 
	padding: 5px 10px;
	border: none;
	margin: 0 -10px; /* negative margin = border-width + horizontal padding */ 
}

Solution 5 - Html

Use css calc()

Super simple and awesome.

input {
    width: -moz-calc(100% - 15px);
    width: -webkit-calc(100% - 15px);
    width: calc(100% - 15px);
}​

As seen here: https://stackoverflow.com/questions/651317/div-width-100-minus-fixed-amount-of-pixels
By webvitaly (https://stackoverflow.com/users/713523/webvitaly)<br> Original source: http://web-profile.com.ua/css/dev/css-width-100prc-minus-100px/<br>

Just copied this over here, because I almost missed it in the other thread.

Solution 6 - Html

Assuming i'm in a container with 15px padding, this is what i always use for the inner part:

width:auto;
right:15px;
left:15px;

That will stretch the inner part to whatever width it should be less the 15px either side.

Solution 7 - Html

Here is the recommendation from codeontrack.com, which has good solution examples:

> Instead of setting the width of the div to 100%, set it to auto, and be sure, that the <div> is set to display: block (default for <div>).

Solution 8 - Html

You can try some positioning tricks. You can put the input in a div with position: relative and a fixed height, then on the input have position: absolute; left: 0; right: 0;, and any padding you like.

Live example

Solution 9 - Html

Move the input box' padding to a wrapper element.

<style>
div.outer{ background: red; padding: 10px; }
div.inner { border: 1px solid #888; padding: 5px 10px; background: white; }
input { width: 100%; border: none }
</style>

<div class="outer">
    <div class="inner">
       <input/>
    </div>
</div>

See example here: http://jsfiddle.net/L7wYD/1/

Solution 10 - Html

Maybe browsers have changed since this question was last answered, but this is the only thing that has ever worked reliably for me to accomplish this:

    width: auto;
    left: 0;
    right: 0;

Then you can make the margins / padding anything you want and the element will not expand past its available width.

This is similar to @andology's answer from way back but if you make left/right both 0 then you can make margin and/or padding whatever you want. So this is always my default div.

Solution 11 - Html

Just understand the difference between width:auto; and width:100%; Width:auto; will (AUTO)MATICALLY calculate the width in order to fit the exact given with of the wrapping div including the padding. Width 100% expands the width and adds the padding.

Solution 12 - Html

What about wrapping it in a container. Container shoud have style like:

{
    width:100%;
    border: 10px solid transparent;
}

Solution 13 - Html

Try this:

width: 100%;
box-sizing: border-box;

Solution 14 - Html

For me, using margin:15px;padding:10px 0 15px 23px;width:100%, the result was this:

enter image description here

The solution for me was to use width:auto instead of width:100%. My new code was:

margin:15px;padding:10px 0 15px 23px;width:auto. Then the element aligned properly:

enter image description here

Solution 15 - Html

You can do this:

width: auto;
padding: 20px;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHailwoodView Question on Stackoverflow
Solution 1 - HtmlthirtydotView Answer on Stackoverflow
Solution 2 - HtmlMathias BynensView Answer on Stackoverflow
Solution 3 - HtmlAlexView Answer on Stackoverflow
Solution 4 - HtmlVladimir StarkovView Answer on Stackoverflow
Solution 5 - HtmlDaanView Answer on Stackoverflow
Solution 6 - HtmlAndologyView Answer on Stackoverflow
Solution 7 - HtmlM.YPCView Answer on Stackoverflow
Solution 8 - HtmlFelixView Answer on Stackoverflow
Solution 9 - HtmlMartin JespersenView Answer on Stackoverflow
Solution 10 - HtmlPeter MooreView Answer on Stackoverflow
Solution 11 - Htmluser3849374View Answer on Stackoverflow
Solution 12 - HtmlVitali ProtosovitskiView Answer on Stackoverflow
Solution 13 - HtmlManolo RamosView Answer on Stackoverflow
Solution 14 - HtmlJaime MontoyaView Answer on Stackoverflow
Solution 15 - HtmlMissHDView Answer on Stackoverflow