Table of Contents

All You Need To Know About CSS Margin And Padding

Our operations are supported by commissions earned from affiliates. Details.

The best practices of online store development always boil down to the primary web design aspects that determine its structure, functionality, and responsiveness. Although we no longer have to understand coding to make an eCommerce website, it is increasingly vital that you master the basic core concepts to help you make meaningful customizations to your store pages.

Margin vs Padding

CSS styling concepts are some of the main aspects that work to control the looks and functionality of your web pages, and this article will focus more on margin and padding.

For those who do not know what these are, padding and margin properties often enable developers to control the space around the elements on their store pages. In other words, they help users customize the look and feel of their web design.

Besides being widely used elements in web development, most newbie developers commonly misunderstand both margin and padding. They are crucial ingredients in any CSS box model, and understanding them will help you make meaningful changes to your eCommerce site.

Think of them as the foundation of your customer engagement strategy.

Lucky for you, this article details everything you need to know about margin and padding. It takes on the CSS margin vs padding question and will answer how they differ when to use them and how to add them.


To kick-start our padding vs margin discussion, here is what each of these properties means:

1. What Are Margin And Padding?

Padding and Margin definitions

Margin essentially refers to the area around an element such as a headline, navigation bar, image, or body content on a web page. It often occupies the area around the content, precisely the space between the borders of two separate elements. 

As part of the CSS box model, margins often wrap around HTML features and can move an element to the top and bottom or left and right of the web page. As expected, any space around an element is always transparent, meaning that margins often have no background colour. 

Margin is the space around the content area, and you can easily use it when you want to increase the size of the content space by either deleting or adding more gaps around elements.

Margin of content area

On the other hand, padding is the space between an element and all the content inside. Think of it as the area between a border and the content element within your CSS box model. 

It essentially surrounds images or texts within a specific boundary, preventing them from touching the CSS box model or border around it.

Unlike margin properties, padding can manipulate the space within a container. It means that you can use padding to determine how two elements or more sit and look within the container. It often shows the elements around specific backgrounds and is mainly affected by background colors.

Using it to create a space or gap will either increase the element size or shrink the space within. The best part is that you are free to change and use padding as you may want when designing your store pages.

Padding of content area

Online store websites often create any business's first impression to their market audiences. It is an essential tool that customers first consider before choosing to do business with you, and you only have a few seconds to impress them.

Those with unappealing and outdated websites often have a higher chance of losing potential leads than those with attractive web pages.

In other words, web design often impacts how customers perceive brands, and the choices you make here can either make or break your business. Now that you are already aware of the two main CSS properties, let us delve into their main differences and further see how padding and margin can help you keep new leads on your website.

2. The Differences Between Padding And Margin

In web design, both margin and padding are often used to create additional space or gap. They commonly target all four sides of page elements and might function similarly but remain different.

By now, you can already tell that you can use margins to add spaces between elements and padding when you want to add a space between an inner element and the styling box. Unfortunately, many developers mistakenly use the two interchangeably and eventually mess up the user experience.

Differences between padding and margin

So, here are the main difference between padding and margin:

01. Padding Is Affected By Background Color While Margin Is Not

Styling spaces created by either padding or margin are mainly invisible. But when the background color is added to the mix, padding often takes up the element's color and seizes to be invisible.

On the other hand, margins are mainly transparent and are not affected by background color changes. Therefore, padding tends to become more evident with the addition of background color while margins remain invisible regardless.

02. Margin Can Be Set To Auto While Padding Cannot

One of the biggest challenges developers often face is to center an element. But this does not need to be the case because you can easily center any element by using margin auto on any element with a fixed breadth or width if you like.

This way, the setting will automatically center the element horizontally. Think of it as a handy tip for newbie developers. However, keep in mind that this does not apply during padding because it is impossible to set them on auto.

03. Padding Controls The Space Inside While Margin Controls The Space Between Elements

I know how ironic this might sound since we agreed that both margin and padding helps to control spacing in web design. It is a significant similarity and a core point from which their main difference stems.

From their definitions, padding often gives developers the ability to control the space between the content of the elements within a specific border. At the same time, margin determines the spacing between the border and all the elements around it.

In simpler terms, margins control the spacing between elements while padding controls the space inside an element.

Padding and margin spacing

04. Padding Can Ignore Inline Elements While Margin Cannot

If you add padding to an inline element, the changes will only occur on the element's right and left sides, not on its top and bottom. However, a similar margins setting will affect all four sides, including top and bottom configurations.

05. Margin Can Be Negative While Padding Cannot

Developers often give margins a negative value whenever they want different elements to overlap. As a result, this setting usually comes in handy when creating a beautiful authentic web design.

Unfortunately, padding values can only be positive. Trying to give your padding a negative value will collapse your borders into the content, shrinking the content area beyond the content size itself.

3. When To Use Margin And Padding

When to use padding and margin

From the outlook, both margin and padding often seem to have the same special powers to web pages. But as we have already seen from the previous discussion, padding often controls the inner space within a border while margins control the outer space.

Therefore, padding in CSS mainly works to give div and other elements some breathing room from the outline of the border.

In layman's terms, you can use padding to customize the space inside the border. As a result, this will help make your content more legible and give the page a better look.

On the flip side, you can use margin to prevent all your visible elements from colliding with each other. For instance, if you want to keep your subtitles from crowding your CTA buttons or vice versa, give them an extra margin space to keep them apart.

Doing so often adds more whitespace to web pages, and revamps overall customer experience for those visiting your online store.

But keep in mind that you can give margins a negative value if you want different elements to visibly trespass into or overlap other elements' space. Developers mostly do this to create an artistic impression for their customers.

Since different styling elements often work differently with margin and padding, here are some valuable tips to take into account when choosing between the two:

  • Use padding:
    • to prevent content from touching the edges of the container
    • to increase content block size without affecting the content size
    • when you want more space between an inner element and the parent box
    • if you want the gap to reflect the background of the element
  • Use margin:
    • to add space around elements like for the case of photo captions
    • to center an element horizontally
    • use negative margins to overlap elements
    • to move an element up, down, right, or left

I know that all these might be too much to take in and try by yourself, especially if you are entirely new to web development. The good news is that there are numerous page builders like PageFly that you can use to customize your store pages faster and with much ease.

PageFly's Shopify app listing

It is compatible with most eCommerce platforms, saves time, and does not need any coding experience to use. All the reasons you should try it right away.

4. How To Add Margin And Padding

01. How To Add Margins In CSS

To add a margin, all four aspects need to be declared first to prevent them from colliding with one another. The four margins include:

  • Margin top
  • Margin right
  • Margin bottom
  • Margin left

The best part is that you will not have to set them all individually because each property allows you to set them all up in one declaration. You can do so separately for those who might want a different length or percentage for their margins.

For instance, you can use the code below to add 30 pixels on the top, 75 pixels on the right, 45 pixels on the bottom, and 100 pixels on the left of your margin:

p {

 margin: 30px 75px 45px 100px;


Note that this effect will go around the element in a clockwise manner, and remember that to add a percentage in CSS, it must be of the inline size of the parent box for all your margins to be equal-sized.

However, one of the main problems when using margins is margin collapsing, and this commonly occurs when smaller margins overlap into larger ones. 

A good example is when the margin-bottom of your heading is closely followed with a margin-top of a paragraph, they will collapse to combine.

Luckily, you can either use the Block Formatting Context (BFC) or Flex and Grid Containers to prevent such occurrences.

For your convenience, you can easily control the space outside your containers using the margin section on your PageFly Page Editor to make the changes you want.

How to add Margins in CSS

02. How To Add Padding In CSS

Like we did for margin, it is always best to first declare all your padding properties, especially when dealing with padding in CSS. It is a standard procedure that mainly includes declaring your:

  • Padding top
  • Padding right
  • Padding bottom
  • Padding left

Here, every padding property commonly features specific traits like percentage and length. But just like in margins, you can set them all in one declaration.

For example, you can use the code below to add 20 pixels on the top, 25 pixels on the right, 50 pixels on the bottom, and 75 pixels on the left of your padding: 

div {

  padding: 20px 25px 50px 75px;


Note that this will also go in clockwise order around the element.

Also, you can quickly control how much space you want inside your containers by only inserting your desired declaration measures in the padding section of your PageFly Page Editor. That’s it, everything is automated and you do not need to do any coding.

But be sure to keep in mind the four critical declarations, i.e., padding left, padding top, padding right, and padding bottom.

How to add Padding in CSS

5. Conclusion

Online store web design is one of the main things that affects how your target audience will perceive your business. It affects the fundamentals of your online store and can potentially make or break your business.

In web development, the effective use of white space is usually crucial as it essentially influences readability, page hierarchy, focus, and understanding. These are the main factors that contribute to a better user experience and that is why you must know the basic concepts of web development.

While CSS margin and padding are some of the core concepts you should master for your box model, you must not confuse the two. 

They both serve different purposes and should not be used interchangeably.

You can use margin to control the space outside the border whereas padding in CSS allows you to customize space between the borders. This article details how margin vs padding differs, when to use them, and even points out some valuable tips for you. 

Just remember that you can use page builders like PageFly to fully customize and personalize your store pages in the shortest time possible and without any coding experience.

Explore Now

Explore the #1 Shopify Page Builder today

Try PageFly Free