Home Tutorials News Software Online Money Web Designing
| Contact | Twitter

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.

Labels: ,

Search Related Articles

Do you like this post?

Subscribe to get latest
Digg This! Digg this!
reddit it!Reddit it!
Bookmark in del.icio.usSave to Delicious
Print it
SlashdotSlashdot It!


Post a Comment

We appreciate your opinions, suggestions and criticism.

  1. This explication was so clear, thx for helping me understand the difference !
  2. THNKS
  3. Excellent example and explanation.
  4. Excellent answer!
  5. Thks! very helpfull
  6. thanks very helpful
  7. Very useful tutorial, This tutorial has helped me a lot.
    Thank you for sharing this with us.
  8. Really very helpful tutorial. Thank you for sharing such an informative and knowledgeable post with us.
  9. Exactly what I was looking for. Many thanks!
  10. Anonymous Anonymous (April 05, 2009 7:33 AM)  
    very easy to understand and helpful thank you!
  11. Anonymous Anonymous (April 07, 2009 12:10 PM)  
    That was really the best explanation seen on the Internet
  12. Thanks for your encouraging comments.
  13. Anonymous Anonymous (April 22, 2009 8:44 AM)  
    Thanx so much for your short but clear and easy-to-understand explanation. You rock!
  14. Anonymous Anonymous (April 30, 2009 4:14 AM)  
    thanks
  15. Hey Thanks for this article
  16. 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.
  17. Hi Berkeley,

    I'll try to give info on these in another post. Thanks for the request.
  18. Anonymous Anonymous (August 26, 2009 1:35 AM)  
    great post
  19. 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..
  20. Very good explanation!
  21. Thank you very much for your crystal clear explanation.
ARCHIVES
Select Month:
ABOUT AUTHOR
Kamal Mettananada is a Software Architect, Java Explorer and Blogger. Digizol consists of computer related articles, tutorials, tips and other information.

Free counter and web stats

Subscribe to our feed using your online news reader, approximately 1-2 articles per week.
^top^