277

I am creating a site with DIVs. Everything's working out except when I create a DIV. I create them like this (example):

newdiv {
    width: 200px;
    height: 60px;
    padding-left: 20px;
    text-align: left;
}

When I add the padding-left property, the width of the DIV changes to 220px, and I want it to remain at 200px.

Let's say I create another DIV named anotherdiv exactly the same as newdiv, and put it inside of newdiv but newdiv has no padding and anotherdiv has padding-left: 20px. I get the same thing, newdiv's width will be 220px.

How can I fix this problem?

1
  • 5
    I'm sad to say, but I like how IE handles this... Makes much more sense to me ...
    – Nicu Surdu
    Commented Jun 6, 2012 at 13:22

9 Answers 9

461

Add property:

-webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
-moz-box-sizing: border-box;    /* Firefox, other Gecko */
box-sizing: border-box;         /* Opera/IE 8+ */

Note: This won't work in Internet Explorer below version 8.

11
  • 19
    This is a great solution for borders, but how does this help for padding?
    – Kato
    Commented Aug 29, 2012 at 5:01
  • 7
    This is pure magic! - It fixes the box model we all know and hate! i.e. makes it work the way intuition suggests it should - rather than the specs - but as far as I know doesn't break any specs either :D This article clarifies it well... stackoverflow.com/questions/779434/… Commented Sep 5, 2012 at 15:45
  • 2
    @technicalbloke Your link just goes back to this question.
    – mhenry1384
    Commented Oct 23, 2012 at 18:08
  • 12
    Whoops yeah, copy and paste cock up. This is the link I meant to share... paulirish.com/2012/box-sizing-border-box-ftw Commented Oct 24, 2012 at 19:54
  • 1
    Many of you should consider the width: auto trick below. Works across browsers, less code, etc. Commented Oct 3, 2013 at 21:07
41

Put a div in your newdiv with width: auto and margin-left: 20px

Remove the padding from newdiv.

The W3 Box model page has good info.

1
  • 2
    Why use two divs when you can use one? Commented Dec 9, 2019 at 10:36
37

Try this

box-sizing: border-box;
14

If you would like to indent text within a div without changing the size of the div use the CSS text-indent instead of padding-left.

.indent {
  text-indent: 1em;
}
.border {
  border-style: solid;
}
<div class="border">
  Non indented
</div>

<br>

<div class="border indent">
  Indented
</div>

1
  • 2
    This is the only solution that I found worked for my fixed width div. box-sizing did not stop the padding from impacting the width unfortunately, so thanks a tonne for pointing out this awesome little property.
    – Stevo
    Commented Jan 21, 2020 at 0:02
14

A lot of the answers above are correct, but provided little explanation, so i decided to add this for anyone that might need it.

By default, every element box-sizing parameter is set to content-box. Which means, that if you set an element width to 200px and then add a padding of 20px on both horizontal end, this would result to a total width of 240px for that element.

to fix this, you simply need to update the box-sizing parameter and set this to border-box in your css. Or you can do this for all elements by simply adding the following.

* {
     box-sizing: border-box 
  }

This tells the browser to account for any border and padding in the values you specify for an element's width and height.

So for an element set to border-box with a width of 200px, and a padding of 20px on both sides, it's total width would still remain 200px (160px as content box and 40px as padding).

Hope that helps. You read more on css box-sizing

9

simply add box-sizing: border-box;

2
  • 25
    This is simply exactly the same as what other answers already say here.
    – Neil Lunn
    Commented Jun 24, 2017 at 4:14
  • box-sizing: border-box; = perfect answer - uncomplicated- short- works Commented Nov 30, 2023 at 4:13
0

when I add the padding-left property, the width of the DIV changes to 220px

Yes, that is exactly according to the standards. That's how it's supposed to work.

Let's say I create another DIV named anotherdiv exactly the same as newdiv, and put it inside of newdiv but newdiv has no padding and anotherdiv has padding-left: 20px. I get the same thing, newdiv's width will be 220px;

No, newdiv will remain 200px wide.

6
  • 3
    Guffa, that's not how the standards are supposed to work at all. Padding is definitely not supposed to affect the dimensions of the element to which it's being applied. With the greatest respect, is it possible you're confusing 'padding' with 'margin'?
    – da5id
    Commented Apr 22, 2009 at 22:37
  • 1
    Yes, the padding is definitely supposed to affect the dimensions of the element. I think that it's you who is confused... how do you propose that the margin would affect the dimensions of the element?
    – Guffa
    Commented Apr 22, 2009 at 22:44
  • Actually, you know I think we're both right in a way. I'm so used to coping with the effects that I'd entirely forgotten the standard. I found a good explanation here quirksmode.org/css/box.html
    – da5id
    Commented Apr 22, 2009 at 22:46
  • So, my apologies Mr Guffa. But for practical purposes (which is after all what we're talking about) my advice is still good no?
    – da5id
    Commented Apr 22, 2009 at 22:47
  • Well, you are right so far as that there is a box model bug, but it doesn't apply to this question... :)
    – Guffa
    Commented Apr 22, 2009 at 23:13
0

This would work in all cases, with this the extra padding included in predefined width

box-sizing: border-box;

-2

just change your div width to 160px if you have a padding of 20px it adds 40px extra to the width of your div so you need to subtract 40px from the width in order to keep your div looking normal and not distorted with extra width on it and your text all messed up.

1
  • 2
    This is the same as the answer given by @rvarcher.
    – 8bitjunkie
    Commented Jan 10, 2014 at 11:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.