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.
This explication was so clear, thx for helping me understand the difference !
ReplyDeleteTHNKS
ReplyDeleteExcellent example and explanation.
ReplyDeleteExcellent answer!
ReplyDeleteThks! very helpfull
ReplyDeletethanks very helpful
ReplyDeleteThanks u r information
ReplyDeleteVery useful tutorial, This tutorial has helped me a lot.
ReplyDeleteThank you for sharing this with us.
Really very helpful tutorial. Thank you for sharing such an informative and knowledgeable post with us.
ReplyDeleteExactly what I was looking for. Many thanks!
ReplyDeletevery easy to understand and helpful thank you!
ReplyDeleteThat was really the best explanation seen on the Internet
ReplyDeleteThanks for your encouraging comments.
ReplyDeleteThanx so much for your short but clear and easy-to-understand explanation. You rock!
ReplyDeletethanks
ReplyDeleteHey Thanks for this article
ReplyDeleteThis 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.
ReplyDeleteHi Berkeley,
ReplyDeleteI'll try to give info on these in another post. Thanks for the request.
great post
ReplyDeleteyeah.. 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!!!
ReplyDeletei 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..
Very good explanation!
ReplyDeleteThank you very much for your crystal clear explanation.
ReplyDeleteBrilliant. So fresh and so clean.
ReplyDeleteDont think all web savvys are aware of this!!!
ReplyDeleteThank you very much. Gives a clear idea. Rock On
It's a exhaustive tutorial...
ReplyDeleteThz.
Perfect.... simple and clear. That first image does it. Thanks.
ReplyDeletevery clear. Thanks a lot!
ReplyDeleteThank you very much.
ReplyDeletevery clear. thanks.
ReplyDeleteThanks machan,
ReplyDeleteHey I was with u in M2 in Ananda college,
Exactly what I needed ,
keep the good work
Really nice... Easy to understand.
ReplyDeleteThanks,
So simple yet so powerful! Just like the iPod!
ReplyDeleteThanks, great explanation !!!!!
ReplyDeletevery clear, thanks!
ReplyDeleteThanks for the nice information. I am sure, I will tweet this to my twitter account. This will help a lot of users.
ReplyDeleteGood Example. Thanks a lot Kamal...
ReplyDeleteSuperb, Thanks
ReplyDeleteOH! How come nobody else made it this simple?
ReplyDeleteCan one set the top, bottom, left and right padding or margins different?
ReplyDeleteThanks for the help!
ReplyDeleteThis article is very helpful for understanding the difference between margin and padding. Thanks for posting this article.
ReplyDeleteComment should not be empty
ReplyDeleteThanks so much, Kamal! Very well explained.
ReplyDeleteThank u for posting this tutorial in detailed,
ReplyDeletewe wish to upload more like this....
thanks for the help
ReplyDeleteThanks buddy..
ReplyDeleteThis is very good article, I am very interested in its topic and read them was a pleasure.
ReplyDeleteI finally have the answer! Thank you. I always get confused when I use margin.
ReplyDeleteVery helpful, thanks!
ReplyDeleteThanks!!!
ReplyDeleteHey Dimuthu,
ReplyDeleteNice to hear that it helped.
Simple and clear, Merci !
ReplyDeleteExcellent example.
ReplyDeleteGreat posting...Thanks for this helpful and useful post sharing here.
ReplyDeleteNow i know thank you
ReplyDeleteYour definition is very clear. Now I know the difference between the two. Thank you.
ReplyDeleteSolid explanation. Helped me realize and solve a design defect. Thanks!
ReplyDeleteNice posts here, really looking forward to checking out your other posts whe I have got more time, I shall bookmark your blog now.
ReplyDeleteI 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
ReplyDeleteGood explanation and example. Thank you.
ReplyDeleteGood explanation with example.
ReplyDeleteBest explanation i found on this topic over internet
ReplyDelete