What is the difference between padding and margin in css?

In CSS, padding and margin are two commonly used properties that define spacing around elements. Although they appear similar, there are key differences between them:

Padding:
  •              Padding is the space between the content of an element and its border.
  •              The padding is added inside the border of an element.
  •              It affects the size of the element, as the padding expands the element’s content area.
  •              Padding can be set individually for each side of an element using the padding-top, padding-right, padding-bottom, and padding-left properties, or using the shorthand padding property.
  •              Padding can be specified in various units such as pixels (px), percentages (%), ems (em), or other CSS length units.
Margin:
  •              Margin is the space between an element and its adjacent elements (outside its border).
  •              The margin is added outside the border of an element.
  •              It does not affect the size of the element, as the margin is considered outside the element’s content area.
  •              Margin can be set individually for each side of an element using the margin-top, margin-right, margin-bottom, and margin-left properties, or using the shorthand margin property.
  •              Margin can also be specified in various units such as pixels (px), percentages (%), ems (em), or other CSS length units.

In summary, padding creates space within an element, affecting the size of the content area, while margin creates space around an element, influencing its positioning and the spacing between adjacent elements.

Let us use an example to illustrate how margin differ from padding with some Html and Css code.

Html

So I have this three buttons wrapped within a div container in my Html document.

 <div>
      <button class="btn">Click me</button>
      <button class="btn">Click me</button>
      <button class="btn">Click me</button>
    </div>

CSS
Next, I will apply some styling in CSS

div {
  width: 80%;
  margin: 30px auto;
  border: 2px solid red;
}
.btn {
  background-color: #007bff;
  color: #fff;
  padding: 5px 10px;
  border: none;
  border-radius: 4px;
}

Result

difference between padding and margin

Notice how close the buttons are to the red border of the dive container. If the padding is increase, you will notice that the space inside the button will increase as shown below.

.btn {
  background-color: #007bff;
  color: #fff;
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
}

Result

Notice that the space inside (which is between the text and the border) has increased but nothing happen to the outer space. To increase the space between the border of the button and the red border of the div container, we add margin as shown below

.btn {
  background-color: #007bff;
  color: #fff;
  padding: 10px 20px;
  margin: 10px;
  /* margin-left: 10px;
  margin-top: 10px;
  margin-right: 10px;
  margin-bottom: 10px; */
  border: none;
  border-radius: 4px;
}

Result

difference between padding and margin

You can clearly see the distance between the border of the container and the button has significantly increases. The margin of 10px which is added is applied to the left, top, right and bottom of the button. That is why I commented out the other margin properties and values.

Hopefully, you understand the difference, when to use each one and how to apply it accordingly.

Leave a Comment

Your email address will not be published. Required fields are marked *