Margin Vs Padding - CSS Properties - Digizol6

Post Top Ad

Responsive Ads Here

Post Top Ad

Responsive Ads Here

Thursday, December 21, 2006

Margin Vs Padding - CSS Properties


CSS provides two properties named margin and padding for keeping space between HTML Box type elements. But why do we have two properties for the same reason? Are they similar?
No. They have a main difference.
Padding - defines space between border and element content
Margin - defines space between border and other outer elements
(Look at the above diagram)

So when elements needs space between them, better to use margins. When text or an inner element needs space between the parent box and itself go for paddings.

Look at this example for visual clarification generated with this code.
<div style="margin:25px; background:#cccccc; border:#000000 2px dashed;">
Text inside element
</div>
<div style="padding:25px; background:#cccccc; border:#000000 2px dashed;">
Text inside element
</div>


Text inside element

Text inside element



In the first <div>, space is set between the border and outside elements but in the second one it is between the boder and text.

Hope this will help.

62 comments:

  1. This explication was so clear, thx for helping me understand the difference !

    ReplyDelete
  2. Excellent example and explanation.

    ReplyDelete
  3. Thks! very helpfull

    ReplyDelete
  4. thanks very helpful

    ReplyDelete
  5. Very useful tutorial, This tutorial has helped me a lot.
    Thank you for sharing this with us.

    ReplyDelete
  6. Really very helpful tutorial. Thank you for sharing such an informative and knowledgeable post with us.

    ReplyDelete
  7. Exactly what I was looking for. Many thanks!

    ReplyDelete
  8. very easy to understand and helpful thank you!

    ReplyDelete
  9. That was really the best explanation seen on the Internet

    ReplyDelete
  10. Thanks for your encouraging comments.

    ReplyDelete
  11. Thanx so much for your short but clear and easy-to-understand explanation. You rock!

    ReplyDelete
  12. Hey Thanks for this article

    ReplyDelete
  13. This is a good explanation of margin vs padding. Although, you should mention the various differences between the popular browsers and how they render them. There are some slight variations that will make you pull your hair out.

    ReplyDelete
  14. Hi Berkeley,

    I'll try to give info on these in another post. Thanks for the request.

    ReplyDelete
  15. yeah.. thks so much.. i am not familiar with html and css and everytime i need to do some simple text or image spacing without asking for help in forum, i am always confused and messed out on the padding and margins portion. but i still unable to space out two images in a wordpress sidebar. if i need two images align vertically between each other, i try to put in some value eg. 20px in both the padding and margins but there is no effect when i viewed them on the browser!!!
    i guess there is some overwriting of the css in other area of the theme css style sheet? any helpful advise on how to troubleshoot this? thanks for the tips here... i learn a lot..

    ReplyDelete
  16. Very good explanation!

    ReplyDelete
  17. Thank you very much for your crystal clear explanation.

    ReplyDelete
  18. Brilliant. So fresh and so clean.

    ReplyDelete
  19. Dont think all web savvys are aware of this!!!
    Thank you very much. Gives a clear idea. Rock On

    ReplyDelete
  20. It's a exhaustive tutorial...
    Thz.

    ReplyDelete
  21. Perfect.... simple and clear. That first image does it. Thanks.

    ReplyDelete
  22. very clear. Thanks a lot!

    ReplyDelete
  23. Thank you very much.

    ReplyDelete
  24. very clear. thanks.

    ReplyDelete
  25. Thanks machan,
    Hey I was with u in M2 in Ananda college,
    Exactly what I needed ,
    keep the good work

    ReplyDelete
  26. Really nice... Easy to understand.

    Thanks,

    ReplyDelete
  27. So simple yet so powerful! Just like the iPod!

    ReplyDelete
  28. Thanks, great explanation !!!!!

    ReplyDelete
  29. very clear, thanks!

    ReplyDelete
  30. Thanks for the nice information. I am sure, I will tweet this to my twitter account. This will help a lot of users.

    ReplyDelete
  31. Good Example. Thanks a lot Kamal...

    ReplyDelete
  32. OH! How come nobody else made it this simple?

    ReplyDelete
  33. Can one set the top, bottom, left and right padding or margins different?

    ReplyDelete
  34. This article is very helpful for understanding the difference between margin and padding. Thanks for posting this article.

    ReplyDelete
  35. Comment should not be empty

    ReplyDelete
  36. Thanks so much, Kamal! Very well explained.

    ReplyDelete
  37. Thank u for posting this tutorial in detailed,
    we wish to upload more like this....

    ReplyDelete
  38. thanks for the help

    ReplyDelete
  39. This is very good article, I am very interested in its topic and read them was a pleasure.

    ReplyDelete
  40. I finally have the answer! Thank you. I always get confused when I use margin.

    ReplyDelete
  41. Very helpful, thanks!

    ReplyDelete
  42. Hey Dimuthu,
    Nice to hear that it helped.

    ReplyDelete
  43. Simple and clear, Merci !

    ReplyDelete
  44. Excellent example.

    ReplyDelete
  45. Great posting...Thanks for this helpful and useful post sharing here.

    ReplyDelete
  46. Your definition is very clear. Now I know the difference between the two. Thank you.

    ReplyDelete
  47. Solid explanation. Helped me realize and solve a design defect. Thanks!

    ReplyDelete
  48. Nice posts here, really looking forward to checking out your other posts whe I have got more time, I shall bookmark your blog now.

    ReplyDelete
  49. I noticed that it's hard to find your website in google, i found it on 22th spot, you should get some quality backlinks to rank it in google and increase traffic. I had the same problem with my website, your should search in google for - insane google ranking boost - it helped me a lot

    ReplyDelete
  50. Good explanation and example. Thank you.

    ReplyDelete
  51. Good explanation with example.

    ReplyDelete
  52. Best explanation i found on this topic over internet

    ReplyDelete

Post Top Ad

Responsive Ads Here