What is the difference between padding and margins?

For those who would like to know more about HTML and CSS, in this post we will learn the differences between margin and padding CSS properties that are used to add space between the HTML elements, thus preventing the elements from getting ‘stuck’ together.

Although both have similar functions, each work in a different way.

  1. Padding determines the inner margins, being used for changing the distance between the content of a given element and its border.
  2. Margin is the space outside of an element affecting how far the element is away from other elements.

For a better understanding of how the padding and margin work, take a look at the picture below:

padding and margin, css proprieties
Difference between margin and padding

Just as you have the option to define the borders for each side of your HTML elements (eg. borders for the sidebar), you can do the same with the margin and padding properties. All you need to to do is to change the top, right, bottom and left values​​.

The values that you can use include: pixels (px), centimeters (cm), millimeters (mm). em units (em), x-height (ex), inches (in), picas (pc) and point sizes (pt).

When using margin and padding, you need to add a value for each 4 corners of an element, as follows: top, right, bottom, left. Each value must refer to the each side of the HTML element. The first value is for the top, and they follow a clockwise direction, so the value would be close to the right, then bottom, and finally, left.

Example 1:

padding:5px 15px 5px 15px;

  • top padding is of 5px 
  • right padding is of 15px 
  • bottom padding is of 5px 
  • left padding is of 15px

margin: 10px 5px 10px 5px;

  • top margin is of 10px
  • right margin is of 5px
  • bottom margin is of 10px
  • left margin is of 5px

If you define a single value (instead of 4), the distance applies on the all sides, i.e. it will be applied on the 4 sides of the HTML element that you want to modify.

Example 2:
padding: 5px (top and bottom) 15px (left and right);
margin: 10px (top and bottom) 5px (left and right);

If it helps you visually, think of an A4 page of type inside a picture frame. The margin is the space between the edge of the sheet of paper and the other elements (the parts of the frame), while the padding would be the space between the edge of the sheet and where the type starts on the page. The border would be the edge of the sheet of paper.

It’s probably easier to understand if you have borders, then it also becomes obvious which one to use. Do you want “space” after the border (outside the element = margin) or before the border (inside the element = padding).

Related Posts

How to Increase Thumbnail Resolution on Blogger

When we add a widget on Blogger, the thumbnails will maintain a default size of 72 x 72px which might not look good if we try to…

Responsive CSS Timeline with 3D Effect for Blogger

Here’s another amazing way to display the Blogger posts. By applying the following Responsive CSS Timeline View with 3D Effect on Blogger Posts, we will have a…

Add a Cool Style to Blogger Threaded/Nested Comments

If you need a fresh style to the threaded comments of a standard Blogger template, here’s a simple CSS that will help you to apply a different…

Redirect Blogger 404 Error (Page Not Found) To Homepage

A 404 error page tells you and search engines that a page is not found by displaying a text similar to “Sorry, the page you were looking…

CSS Rollover Image Effect – Change Image on Hover

Rollover image is a design feature where an image changes when your mouse hovers over it. Think of a light bulb that turns on and off when…

How to Hide Posts from Blogger Homepage

The point of blogging is to make your blog posts visible to your target audience as well as to search engines. However, there are times when you…

Leave a Reply

Your email address will not be published.