Solving the CSS Padding Problem
Tutorials November 10th, 2008I remember when I first started using CSS, something really ticked me off. That was that when I had an HTML file with a div in it, and I wanted to add some padding to that div, not only did the content inside of the div get “padded”, but the whole div increased in size. As you can imagine, this got really annoying, until one day I found the solution to the problem.
Let’s start with an example of the problem. If I have the following HTML code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <title>CSS Padding Problem</title> <link rel="stylesheet" href="style.css" media="all" /> </head> <body> <div id="padded">I want this content to be padded.</div> </body> </html>
And the following CSS:
body {
margin: 0;
padding: 10px;
background: #EEE;
}
#padded {
width: 950px;
margin: auto;
background: #FFF;
}
Then I will get this result. It looks pretty bad, because the div doesn’t have any padding on it. This is where we run into the problem. Here’s what I mean - if I edit the style.css file and add a “padding: 20px” to the #padded selector, then my CSS file will look like this:
body {
margin: 0;
padding: 10px;
background: #EEE;
}
#padded {
width: 950px;
margin: auto;
background: #FFF;
padding: 20px;
}
And in a web browser, the result of that change would look like this file. Looks pretty good, doesn’t it? However, if you switch back and forth between the first example and the second example, you’ll see that the div in the second example is a bit wider (40px exactly) than the first one.
By now you might be thinking “So what? What’s the big deal with a div that’s a little bit wider than it should be?” True, for some templates that you create, it won’t matter if a div is wider than it should be. However, for designs that are more precise - i.e. they have been designed to be pixel-perfect - a little div width change could cause a lot of trouble.
So how do we fix this problem? Well, there is a surprisingly simple way to fix this problem, and that is by wrapping the #padded div inside another div. Here is what I mean - the new HTML file will look like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <title>CSS Padding Problem</title> <link rel="stylesheet" href="style.css" media="all" /> </head> <body> <div id="container"> <div id="padded">I want this content to be padded.</div> </div> </body> </html>
And the new CSS file:
body {
margin: 0;
padding: 10px;
background: #EEE;
}
#container {
width: 950px;
margin: auto;
background: #FFF;
}
#padded {
padding: 20px;
}
As you can see, what I did is just wrap the #padded div inside another div called #container, and then moved all of the styles for the #padded div - except for the “padding: 20px” - to the styles for the #container div.
Take a look at the finished, “fixed”, padding example. A very easy solution to a maybe not-so-easy problem.





November 13th, 2008 at 1:27 pm
quite useful.
nice explanation =)
November 13th, 2008 at 2:53 pm
hmm. Not really any use without your source code for the actual page.
This is something I have never heard or seen of in the 10years I have been a web developer so it must be more to do with how you actually structure your pages.
Also padding equates different between browsers so without knowing what browser you are testing on it is also tricky to be objective.
November 13th, 2008 at 3:14 pm
@avangelist: Sorry about that, I forgot to link to the right place for the first example. It is working now.
I agree with you that padding does work differently in different browsers (especially IE), but I tested these examples in all of the modern browsers - FF 3, Safari 3, Google Chrome 0.3, IE 7, and Opera 9 - and the results were always the same.
November 14th, 2008 at 2:33 pm
good work nice explaination
November 14th, 2008 at 2:33 pm
can you post an example of photoshop td to div based layout
November 14th, 2008 at 2:50 pm
Good Tutorial! It was chosen for the home page of http://www.tutorialsroom.com
Please submit all of your future quality tutorials in there.
November 15th, 2008 at 7:25 pm
@Tutorials Room: Thanks for the heads up! I’ll make sure to submit my tutorials on your site whenever I write them.
November 16th, 2008 at 7:17 am
I may be wrong but I thought that the width or height you specify for a div is actually the real width from which you subtract the padding and the margin. This is what I always do and I never had problems.
November 16th, 2008 at 8:14 am
My mistake - I meant only the padding.
To put it simply: code height/width=indended height/width -
padding
November 22nd, 2008 at 2:13 pm
Why not just decrease the size of the div with 40px?
then the real size of the div would be width + padding, I`ve never had a problem by doing it that way..
November 22nd, 2008 at 2:24 pm
@Trond: That would work also, but it would be a pain if you wanted to change the padding to, say, 50px instead. If you did that, you would have to change two values. But if you use my technique, you only have to change one value. It just makes it easier.
December 4th, 2008 at 9:37 pm
Or you could use basic math and subtract the padding from the width.
e.g.
width: 580px;
padding: 10px;
for a width of 600px. Requires less effort and less clutter.
December 4th, 2008 at 9:56 pm
@Jam: Personally, I think it would require more effort to do it that way. See my previous comment on this.
December 14th, 2008 at 4:33 am
I don’t use this, I just subtract the padding from the width and it really does seem a lot easier than what you’re doing here.
December 14th, 2008 at 8:02 am
@Daniel: I suppose it is your perspective, but I personally think that in the long run this would save more time, especially in the development stages of your website. See my previous two comments.
December 17th, 2008 at 1:07 am
Josh Drake : Your method is the politically ‘correct’ one.
To put it simply :
Whenever possible, when you specify a width to a div, dont specify padding as well - and vice versa. If you specify padding, don’t specify your width. This is the ‘correct’ Box Model Concept.
December 17th, 2008 at 1:13 am
http://nettuts.com/tutorials/html-css-techniques/10-principles-of-the-css-masters/
for all the ppl above me, read number 4.
December 17th, 2008 at 11:54 am
@Crescentsaber: I have read that link before (I read almost every article over at the TUTS ring), and that is partly where I got this fix. Thanks for sharing again!
December 21st, 2008 at 10:52 am
great tutorial.but you need to be express your idea.like presenting some pictures.
December 22nd, 2008 at 1:07 pm
@admin: If you read the article more closely, you’ll see that I do actually have three example pages that you can look at for reference.