Guide on Programming, Internet and Technology

DigiZol
| Contact

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: ,

Translate into your language
Reader Comments
Comments Count: 18
Write your own opinion
Share with others
E-mail
Search More Articles
  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.

Post a Comment

We appreciate your opinions, suggestions and criticism.

ABOUT AUTHOR
Kamal Mettananada is a Software Architect, Java Explorer and Blogger. Digizol consists of computer related articles, tutorials, tips and other information.
ARCHIVES
Select Month:

Free counter and web stats

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