Padding VS Margin In CSS

Most beginners people usually do not know or are confused with the padding and margin in css. And what it is padding and margin?

Padding is the space inside an element.
Margin is the space outside of the element

And for more detail. See this picture:
padding margin

Example padding css code:

<!DOCTYPE html>
<html>
<head>
<style>
p {
  background-color:blue;
}
p.padding {
  padding-top:25px;
  padding-bottom:25px;
  padding-right:50px;
  padding-left:50px;
}
</style>
</head>
<body>
<p>This is a paragraph with no specified padding.</p>
<p class="padding">This is a paragraph with specified paddings.</p>
</body>
</html>

And this padding with shorthand property:

p.padding {
  padding:25px 50px;
/* same as:
padding-top:25px;
padding-bottom:25px;
padding-right:50px;
padding-left:50px;
*/
}

Explanation
From the above code, will produce the same results as before.
Or if we will use:

p.padding {
  padding-top:25px;
  padding-right: 40px;
  padding-bottom: 50px;
  padding-left: 40px;
}

Result for shorthand property is:

p.padding {
  padding: 25px 40px 50px;
}

And this example code for margin property:

<!DOCTYPE html>
<html>
<head>
<style>
p {
  background-color:yellow;
}
p.margin {
  margin-top:100px;
  margin-bottom:100px;
  margin-right:50px;
  margin-left:50px;
}
</style>
</head>
<body>
<p>This is a paragraph with no specified margins.</p>
<p class="margin">This is a paragraph with specified margins.</p>
</body>
</html>

And now I hope you understand the difference padding and margin. 😉

5 comments

  1. Pingback: Monster Out hack

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s