The Difference Between Margin And Padding

Posted under:
October 23rd, 2008

When messing around in the CSS of your blog’s theme or a website, you will most likely run into settings for margin and padding. While the 2 of these do somewhat similar effects, the way that they handle things is slightly different, in the end all that margins and padding does is separate content from other content.

Margin

A margin is used to separate content OUTWARD from the border of the element such as a DIV. So looking at the example code

#div1 {

margin: 10px;     <— Sets a margin of 10 pixels around the entire element

}

If you had a layout of your website, there would be a 10 pixel space in between the element with an ID of div1 and surrounding elements. Rememeber, you can also set each border’s margin by using the following code

margin: 10px 5px 10px 5px;

This would set a top and bottom margin of 10 pixels on the element and a 5 pixel border along the left and right sides of the element.

Padding

Now I will admit that padding has screwed me up quite a number of times when modifying a blog theme or developing my own website. The mechanics of it are different from margin because padding actually changes the size of the element. Take a look at the following code

#div1 {

width: 200px;

padding: 5px;

}

You can probably guess what width does or at least you should be able to. We can see that there is a padding of 5 pixels set. Padding deals with the area BETWEEN the content itself and the border of the element. So, instead of separating other elements 5 pixels away instead they will remain just as close but the content inside of the element will move inward. The content inside of the element with an ID of div1 will be 5 pixels from the top, right, bottom and left borders of that element.

This is where it can get tricky as I mentioned before that padding actually modifies the size of the element. Considering we used a padding of 5 pixels the actual real width of our element would be 210 pixels because it adds 5 pixels to the left hand side and 5 pixels to the right hand side. Margin would have just moved elements 5 pixels away from the outside and the element would still be only 200 pixels wide.

So then why would you use both margin and padding or use one over the other? It all depends on how you are going to have your site or blog setup and the look that you want to achieve. As an example, if you wanted to set a border around the element with an ID of div1 you would use

#div1 {

border: solid 1px black;

}

Maybe you decide to do the same to another element by adding a border to it as well but you find out that the borders are too close and you want to separate them. This is where margin would come in and you would use that to separate them.

However, you may decide that the content itself is too close to the border and this is where you would use padding. Remember though, if you use padding you have to take into account that it will alter the width and height of the element if you have a width, height or both set.

Related Posts

7 Comments

  1. Comment by: Christopher Ross

    Always keep in mind that IE interprets margin and padding differently then the other browsers so it’s really important that you remember to test your CSS in both and accommodate IE with an extra CSS file if needed.

  2. Comment by: Jarret

    Hey Christopher, thanks for stopping by :)

    You are definitely right about IE and having to check in that. In fact, I have that subject lined up for a post next week ;)

  3. Comment by: Dennis Edell

    Now he tells me….lol

    Dennis Edells last blog post..7 Very Common Startup Mistakes - Are You Guilty? ;)

  4. Comment by: kristarella

    Good old box model. Actually it would be kid of nice if the standards model used IE’s method, it makes more sense to me that width would include padding… Oh well!

    Have you seen this demo? It’s pretty spiffy.

    kristarellas last blog post..God works

  5. Comment by: Jarret

    @kristarella - Hi, thanks for stopping by :)

    Yeah, it would be nice for us coders and web designers if everybody decided to follow a set standard. But until that happens we have to continually do our best to support multiple browsers.

    I have not seen that demonstration though, but it does provide a great perspective on exactly what I was trying to explain in this post. Thanks for the resource :)

    Saw your post on the Google Custom Search Engine. I personally love it as it provides far more relevant data as you stated in your post. In fact I have even thought about writing up a post about how to install it on a blog as you can also use it to generate extra income if searchers click the ads.

  6. Comment by: Mark Sierra at MeAndMyDrum.com

    Thanks for explaining the difference between the two. I’d always just try one or the other to see what worked best, but next time I’ll have a little more insight to work with. :)

  7. Comment by: Normal Joe

    Wow…I never knew the difference. I don’t do much, but I do mess around a lil bit with the css and such. This is good to know.

    Normal Joes last blog post..JoeCow.com,IMwithJohnCow.com - What do you think? Should I Take Over The Cow Pasture?

This site uses KeywordLuv. Enter YourName@YourKeywords in the Name field to take advantage.