How to change a DIV padding without affecting the width/height ?

CssHtml

Css Problem Overview


I have a div that I want to specify a FIXED width and height for, and also a padding which can be changed without decreasing the original DIV width/height or increasing it, is there a CSS trick for that, or an alternative using padding?

Css Solutions


Solution 1 - Css

Declare this in your CSS and you should be good:

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

This solution can be implemented without using additional wrappers.

This will force the browser to calculate the width according to the "outer"-width of the div, it means the padding will be subtracted from the width.

Solution 2 - Css

Solution is to wrap your padded div, with fixed width outer div

HTML

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

        <!-- your content -->

    </div><!-- end .inner -->
</div><!-- end .outer -->

CSS

.outer, .inner {
    display: block;
}

.outer {
    /* specify fixed width */
    width: 300px;
    padding: 0;
}

.inner {
    /* specify padding, can be changed while remaining fixed width of .outer */
    padding: 5px;
}

Solution 3 - Css

Sounds like you're looking to simulate the IE6 box model. You could use the CSS 3 property box-sizing: border-box to achieve this. This is supported by IE8, but for Firefox you would need to use -moz-box-sizing and for Safari/Chrome, use -webkit-box-sizing.

IE6 already computes the height wrong, so you're good in that browser, but I'm not sure about IE7, I think it will compute the height the same in quirks mode.

Solution 4 - Css

try this trick

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

this will force the browser to calculate the width acording to the "outer"-width of the div, it means the padding will be substracted from the width.

Solution 5 - Css

To achieve a consistent result cross browser, you would usually add another div inside the div and give that no explicit width, and a margin. The margin will simulate padding for the outer div.

Solution 6 - Css

if border box doesnt work you can try adding a negative margin with your padding to the side that's getting expanded. So if your container is getting pushed to the right you could do this.

.classname{
  padding: 8px;
  margin-right: -8px;
}

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
QuestionCodeOverloadView Question on Stackoverflow
Solution 1 - CssadswebworkView Answer on Stackoverflow
Solution 2 - CssJuraj BlahunkaView Answer on Stackoverflow
Solution 3 - CsswsanvilleView Answer on Stackoverflow
Solution 4 - CssK.AView Answer on Stackoverflow
Solution 5 - CssPekkaView Answer on Stackoverflow
Solution 6 - Cssdenis View Answer on Stackoverflow