Margin Vs Padding - CSS Properties - Digizol6

Post Top Ad

Responsive Ads Here

Post Top Ad

Thursday, December 21, 2006

Margin Vs Padding - CSS Properties

Responsive Ads Here
margin_padding
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. blank

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

    ReplyDelete
  2. blank
  3. blank

    Excellent example and explanation.

    ReplyDelete
  4. blank
  5. blank
  6. blank
  7. blank
  8. blank

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

    ReplyDelete
  9. blank

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

    ReplyDelete
  10. blank

    Exactly what I was looking for. Many thanks!

    ReplyDelete
  11. blank

    very easy to understand and helpful thank you!

    ReplyDelete
  12. blank

    That was really the best explanation seen on the Internet

    ReplyDelete
  13. blank
  14. blank

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

    ReplyDelete
  15. blank
  16. blank

    Hey Thanks for this article

    ReplyDelete
  17. blank

    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
  18. blank

    Hi Berkeley,

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

    ReplyDelete
  19. blank
  20. blank

    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
  21. blank

    Very good explanation!

    ReplyDelete
  22. blank

    Thank you very much for your crystal clear explanation.

    ReplyDelete
  23. blank

    Brilliant. So fresh and so clean.

    ReplyDelete
  24. blank

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

    ReplyDelete
  25. blank

    It's a exhaustive tutorial...
    Thz.

    ReplyDelete
  26. blank

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

    ReplyDelete
  27. blank

    very clear. Thanks a lot!

    ReplyDelete
  28. blank

    Thank you very much.

    ReplyDelete
  29. blank

    very clear. thanks.

    ReplyDelete
  30. blank

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

    ReplyDelete
  31. blank

    Really nice... Easy to understand.

    Thanks,

    ReplyDelete
  32. blank

    So simple yet so powerful! Just like the iPod!

    ReplyDelete
  33. blank

    Thanks, great explanation !!!!!

    ReplyDelete
  34. blank
  35. blank

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

    ReplyDelete
  36. blank

    Good Example. Thanks a lot Kamal...

    ReplyDelete
  37. blank
  38. blank

    OH! How come nobody else made it this simple?

    ReplyDelete
  39. blank

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

    ReplyDelete
  40. blank
  41. blank

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

    ReplyDelete
  42. blank

    Comment should not be empty

    ReplyDelete
  43. blank

    Thanks so much, Kamal! Very well explained.

    ReplyDelete
  44. blank

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

    ReplyDelete
  45. blank

    thanks for the help

    ReplyDelete
  46. blank
  47. blank

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

    ReplyDelete
  48. blank

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

    ReplyDelete
  49. blank

    Very helpful, thanks!

    ReplyDelete
  50. blank
  51. blank

    Hey Dimuthu,
    Nice to hear that it helped.

    ReplyDelete
  52. blank

    Simple and clear, Merci !

    ReplyDelete
  53. blank
  54. blank

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

    ReplyDelete
  55. blank
  56. blank

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

    ReplyDelete
  57. blank

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

    ReplyDelete
  58. blank

    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
  59. blank

    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
  60. blank

    Good explanation and example. Thank you.

    ReplyDelete
  61. blank

    Good explanation with example.

    ReplyDelete
  62. blank

    Best explanation i found on this topic over internet

    ReplyDelete

Post Top Ad