What is the difference between padding and margins?
For those who want to learn more about HTML and CSS, in this article we will learn the difference between CSS margin properties and padding, which are used to increase the space between HTML elements to prevent elements from " to stick on".
Although both have similar characteristics, each works differently.
Just as you have the ability to set borders for each of your HTML elements (like the border of a sidebar), you can do the same with margin and wrap attributes. All you have to do is change the top, right, bottom, and left values.
You can use the following values: pixels ( px ), centimeters ( cm ), millimeters ( mm ). The units are em ( m ), x-height ( ex ), inches ( in ), pico ( pc ) and point sizes ( pt ).
When using margin and padding, you must add a value to each of the 4 corners of the element as follows: top, right, bottom, left. Each value must refer to each side of the HTML element. The first value is up, and they go clockwise, so the value will be close to the right, then down, and finally left.
Example 1:
Example 2:
padding: 5 pixels (top and bottom) 15 pixels (left and right);
Border : 10 pixels (top and bottom) 5 pixels (left and right);
Consider a framed A4 page if that helps visually. Margins are the space between the edges of the paper and other elements (parts of the frame), and the padding is the space between the start of the font on the page. The edge of the paper will be the frame.
It might be easier to figure out if you have any limits, so it's clear which one to use. You want a "space" after the border (outside the element = margin) or before the border (inside the element = padding).
Although both have similar characteristics, each works differently.
- Padding specifies the padding used to replace the space between the content of a given element and its border.
- Margin is the space outside an element that affects the distance between the element and other elements.
![]() |
| Difference Between Margin and Wrap |
Just as you have the ability to set borders for each of your HTML elements (like the border of a sidebar), you can do the same with margin and wrap attributes. All you have to do is change the top, right, bottom, and left values.
You can use the following values: pixels ( px ), centimeters ( cm ), millimeters ( mm ). The units are em ( m ), x-height ( ex ), inches ( in ), pico ( pc ) and point sizes ( pt ).
When using margin and padding, you must add a value to each of the 4 corners of the element as follows: top, right, bottom, left. Each value must refer to each side of the HTML element. The first value is up, and they go clockwise, so the value will be close to the right, then down, and finally left.
Example 1:
tile : 5px 15px 5px 15px ;
- The top line is 5 pixels
- 15 pixel right tile
- Mosaic in the bottom 5 pixels
- Left margin 15 pixels
border : 10px 5px 10px 5px ;
- Maximum margin 10 pixels
- Right margin 5 pixels
- The bottom margin is 10 pixels
- Left margin 5 pixels
Example 2:
padding: 5 pixels (top and bottom) 15 pixels (left and right);
Border : 10 pixels (top and bottom) 5 pixels (left and right);
Consider a framed A4 page if that helps visually. Margins are the space between the edges of the paper and other elements (parts of the frame), and the padding is the space between the start of the font on the page. The edge of the paper will be the frame.
It might be easier to figure out if you have any limits, so it's clear which one to use. You want a "space" after the border (outside the element = margin) or before the border (inside the element = padding).

Post a Comment for "What is the difference between padding and margins?"