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


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.

COMMENTS

BLOGGER: 67
Loading...
Name

About,8,Adsense,3,Ant,1,Apache,2,Axis,3,Database,2,Earn Online,3,Eclipse,8,Firefox,9,Google,27,GWT,8,Hardware,3,IE,1,Internet,1,Java,48,Javascript,8,Jenkins,1,Linux,6,Miscellaneous,18,News,46,Opinion,9,PHP,1,Productivity,3,Puzzle,3,Security,3,Software,25,Sports,9,Spring,2,TDD,4,Tech,87,Tips,18,Tomcat,5,Tools,19,Tutorial,13,Ubuntu,4,Web application,12,Web Design,2,Web services,4,Windows,3,
ltr
item
digizolm12: Margin Vs Padding - CSS Properties
Margin Vs Padding - CSS Properties
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZoP6H2Q_a5GicC6YyjjMX-3hpeZ5zrwLEOmo1hJuZ_BI2z0hOJYXzGnZMy7-iJRO_BIQ5crZNfj5YLvvDBvrpzDIthZe9XrAsGSLwFzUYjKjR6gu6Y2_T8juJ4Tqwb5ek0bBGBIN9kguC/s400/margin_padding.bmp
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZoP6H2Q_a5GicC6YyjjMX-3hpeZ5zrwLEOmo1hJuZ_BI2z0hOJYXzGnZMy7-iJRO_BIQ5crZNfj5YLvvDBvrpzDIthZe9XrAsGSLwFzUYjKjR6gu6Y2_T8juJ4Tqwb5ek0bBGBIN9kguC/s72-c/margin_padding.bmp
digizolm12
https://digizolm12.blogspot.com/2006/12/margin-vs-padding-css-properties.html
https://digizolm12.blogspot.com/
http://digizolm12.blogspot.com/
http://digizolm12.blogspot.com/2006/12/margin-vs-padding-css-properties.html
true
1218430210337039745
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy