I 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.
As an Update, I found one shortcut list of CSS tricks from my web hosting friend. This is simple but a good refresher.




November 10, 2008 in 









quite useful.
nice explanation =)
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.
@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.
good work nice explaination
can you post an example of photoshop td to div based layout
Good Tutorial! It was chosen for the home page of http://www.tutorialsroom.com
Please submit all of your future quality tutorials in there.
@Tutorials Room: Thanks for the heads up! I’ll make sure to submit my tutorials on your site whenever I write them.
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.
My mistake – I meant only the padding.
To put it simply: code height/width=indended height/width -
padding
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..
@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.
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.
@Jam: Personally, I think it would require more effort to do it that way. See my previous comment on this.
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.
@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.
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.
http://nettuts.com/tutorials/html-css-techniques/10-principles-of-the-css-masters/
for all the ppl above me, read number 4.
@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!
great tutorial.but you need to be express your idea.like presenting some pictures.
Nice solution. I’ve been frustrated with this padding problem when using a layout that has several fixed width columns. I like this technique because it allows me to change the padding, the variable I’m thinking of and not have to recalculate other variables I’ve set and moved on from. Thanks for the post.
Works like a charm! Thanks for the straight forward explanation and examples. These guys giving you a hard time about using math to reduce div sizes instead are just being narrow minded
Hi,
you have to calculate total width if you want to use padding.
Your technique is not correct for all standarts. You can solve the problem but this is not real. If you want to teach wright information to people you have to do right code
This is my opinion
@motomutt: Right, that’s the nice thing about this technique.
@Kadir GÜNAY: I’m not really sure what you’re talking about. This is perfectly standards-compliant. Look at what the W3 Validator had to say about it.
I find it easier to have fewer div’s, but wrap the naked text.
I want this content to be padded.
Then in the css:
#padded {
width: 950px;
margin: auto;
background: #FFF;
}
#padded p {
padding: 20px;
}
Hmm… posting ate my HTML. I just added >p> and >/p> around the text, but inside the div.
It’s very useful. I often see people attempt to center a bunch of divs on a page with a class that has all the margin code in it. I simply put all my divs in a box. center that div.
Umm, your links are messed up.
@Jim: Thanks for pointing this out. When I made the switch from tutorialwow.com to tutwow.com, it messed up the links.
Good for some quick time saving if you don’t mind having the extra markup and hate the “standards” way of doing it when both work.
http://www.w3schools.com/css/css_boxmodel.asp
for all starter please check this http://eaziweb.blogspot.com/………….
Excellent a very good innovative idea. A Great thanks to the simple easy quick solution.
Simple solution but really helped me a lot
You are a lifesaver
Thank you so much
If i have a div which wrap two small div with the attribute float:left, how can i remove the float attribute in the next larger div, or using a blank div whose attribute is clear:both?
I changed my page today to add new masthead.
Added new code for this, and eliminated old rusty code.
Although masthead looks great, it’s munching on the
player, name and every thing is shoved away. How
do I correct this prob?
I can provide the entire code if you need.
The page is the website above.
Thanks!
I don’t have any experience with MySpace coding, and I don’t know how it differs from normal website coding, but it appears that your flash player is in a table data tag (
Thanks Ben, I’ll get on that fix right away!
And I may as well rewrite all of the code,
prob have to borrow someone’s editor, but
at least it will come out NOT munched!
it works well for me!
thank you for such a good solution
Another crucial use appears when relative measures are used (%).
Thanks a lot! You saved my columns!!!
Thanks for share such a good solution
nice
All these words, for nothing. CHROME DOES NOT RESPECT PADDING, may those two google morons die of cancer for the thousands and THOUSANDS of hours that are now down the drain – 271 websites with 3.2 million pages, and those child molesters have rendered them all gargage. %@#$ the internet and %@#$ you morons who promote an answer that DOES NOT WORK. Cretins
What are you talking about? Chrome respects padding fine. You’re a cretin for doing it wrong, your attitude stinks.
This is not really a fix. The symantically correct way to do this would be to wrap the text with a tag indicating what type of information, or web element it is (, …). Then simply apply the padding directly to that tag, thereby solving the problem.
h1 {
padding: 20px;
}
[...] http://www.tutwow.com/htmlcss/solving-the-css-padding-problem/ [...]
Why don’ t you just take 40px out of the div’s width (950px-40px=910px) and you have the same result
That’s very true – that would work as well, but it’s a slight pain to have to recalculate the width of the div every time you want to change the padding.
This is not really a fix as of my thinking.
u can rather use
#padded{
padding-bottom:20px;
padding-top:20px”
}
or
padded{
padding:20px 0px 20px 0px ;
}
You could definitely do that, but the point of this article is to show you a way to be able to add side padding to a div, not just top and bottom padding.
Hey… I thought I would clear something up here, even though this discussion is pretty old.
To do what you are trying to accomplish, use margin, not padding, and you won’t have to do any of that special stuff. The original code will work with margin.
Margin and padding are exactly the same thing except padding is INSIDE the container (making it bigger and making the content resize) and margin is OUTSIDE.
Other than that, they are exactly the same.
I’m not sure if I understand what you’re saying. In the example, I’m trying to give the content in the div some padding for aesthetic reasons, and giving it a margin will only affect the outside of the div, not the inside. I don’t think margin works the same as padding in this example. Thanks for your input, though!
I use a similar code, although I use a class instead of an ID so I can use it multiple times in my script and keep it valid.
You could use box-sizing: border-box nowadays when support for IE7 and lower is irrelevant. You can apply with and padding to the same element without having to calculate widths everytime you change the padding.
“Solving the CSS Padding Problem | TutWow” was indeed certainly
enjoyable and helpful! Within todays society honestly, that is challenging to do.
Many thanks, Ray