Margin Vs Padding - CSS Properties

"Padding" - defines space between border and element content. "Margin" - defines space between border and other outer elements.

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

Demonstration

Look at the code example for visual clarification.
Margin set to 25px
Padding set to 25px
Padding & Margin set to 25px

Above visualization is built using below code.
<div style="margin:25px;  background:#ffff81; border:#000000 2px dashed;">
    Margin set to 25px
</div>
<div style="padding:25px; background:#ffff81; border:#000000 2px dashed;">
    Padding set to 25px
</div>
<div style="padding:25px; margin:25px; background:#ffff81; border:#000000 2px dashed;">
    Padding & Margin set to 25px
</div>

In the first <div>, space is set between the border and outside elements but in the second one it is between the border and text. Third one is a combination of both.

Hope this will help.

COMMENTS

BLOGGER: 67
Loading...
Name

About,2,Adsense,3,Ant,1,Apache,3,Axis,3,Blogger,1,Books,1,CentOS,2,Chrome,2,CSS,2,Database,3,Earn Online,3,Eclipse,10,Facebook,1,Firefox,10,Gmail,4,GNU/Linux,9,Google,26,GWT,8,Hardware,2,IE,5,Interesting,15,Internet,14,Java,49,Javascript,7,JBoss,1,Jenkins,1,Log4j,2,Me,6,Microsoft,2,Miscellaneous,1,News,11,Opinion,10,OSGi,1,PHP,1,Productivity,3,Programming,36,Puzzle,3,Security,4,Software,41,Sports,9,Spring,2,Story,6,Subversion,3,TDD,4,Tech,2,Tips,1,Tomcat,6,Tutorial,13,Ubuntu,4,Web application,14,Web Design,2,Web services,3,Windows,10,Yahoo,1,Zip,2,
ltr
item
Digizol: Margin Vs Padding - CSS Properties
Margin Vs Padding - CSS Properties
"Padding" - defines space between border and element content. "Margin" - defines space between border and other outer elements.
https://2.bp.blogspot.com/_vgTLQ5mShiI/RYpoweMMXvI/AAAAAAAAACk/nmzK6_AGOoc/s1600/margin_padding.bmp
https://2.bp.blogspot.com/_vgTLQ5mShiI/RYpoweMMXvI/AAAAAAAAACk/nmzK6_AGOoc/s72-c/margin_padding.bmp
Digizol
http://www.digizol.com/2006/12/margin-vs-padding-css-properties.html
http://www.digizol.com/
http://www.digizol.com/
http://www.digizol.com/2006/12/margin-vs-padding-css-properties.html
true
7440473
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