• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
NameHero® Blog

NameHero® Blog

Web Hosting Tips & Resources From NameHero

  • AI Agents
    • OpenClaw
    • Claude Code
    • n8n
    • Hermes Agent
    • Open WebUI
    • Docker
  • Hosting
    • Web Hosting
    • WordPress Hosting
    • WooCommerce Hosting
    • Enterprise Hosting
    • Email Hosting
    • HeroicGuard
    • GPU Hosting
    • Website Builder
  • VPS
    • Managed VPS
    • Unmanaged VPS
    • Flex VPS
  • Reseller
  • Gaming
  • Domains
  • Account
  • Blog Home
  • Categories
  • Authors

What’s the Difference Between Margin vs Padding?

Sophia Lee

Published on: September 30, 2024

Categories: Website Development 0

Margin and padding are two properties that adjust the alignment, position, and size of elements. They are important when it comes to customizing your site’s design and layout. Although these properties often get mixed up, there are fundamental differences between them.

In short, the margin refers to the area outside an element whereas the padding controls the space between the element’s border and content. This means you’ll often use margin and padding in different situations. Plus, margins support negative values and automatic settings.

In this post, we’ll take a closer look at the differences between margin and padding. Then, we’ll discuss common use cases before showing you how to add margin and padding in CSS. Let’s get started!

Table of Contents
  • Margin vs Padding Explained
  • When to Use Margin vs Padding
  • How Does the CSS Box Model Work?
  • How to Add Margin in CSS
  • How to Add Padding in CSS
  • How to Add Margin and Padding in WordPress
  • Conclusion

Margin vs Padding Explained

Both margin and padding can be used to de-clutter your web design and establish continuity across your pages. However, they are separate properties with different relationships to page elements.

The margin describes the space surrounding the element while padding is the space between an element’s border and the element’s content. It can be easier to think of the margin as controlling the outside space whereas padding changes the space inside an element.

Margin is essentially used to push other elements away to create whitespace. You’ll usually find automatic settings to adjust the margin and you can enter a negative margin value. This makes it useful for maintaining an equal difference between elements or overlapping items.

While padding also forms gaps, it does this from inside the element. It will either increase the element’s size or shrink the content inside the element. That’s why it’s often used to stop text from getting too close to the border. But it doesn’t support negative values, and it can inherit the styling of other elements (unlike margin).

When to Use Margin vs Padding

Now that you know the difference between margin vs padding, here are some scenarios to help you decide when to apply margins or padding:

  • If you want to change the size of an element (like a button), you’d use padding to expand the space around it.
  • If you want to change the position of an element, you’d use margin. It’s often used to center an element horizontally or position it to the left or right of a page.
  • If you want to increase whitespace on your website, you can use padding to increase space between content and its border box.
  • If you want to de-clutter your page design, you can use margins or padding to create additional space.
  • If you want to set the distance between adjacent elements (e.g. multiple images on a page), you’d use margin.
  • If you want to make sure your web pages adapt seamlessly to different screen sizes, you can use margin and padding (using percentages rather than fixed values).
  • If you want to overlap elements with one another, you’d use margin. More specifically, you’d use a negative margin value.

Once you understand when it makes sense to use margin vs padding, you can start learning how to use these properties with your site’s CSS files.

How Does the CSS Box Model Work?

Before we explain how to add the margin and padding properties in CSS, you need to understand the CSS box model. CSS is a programming language that handles all styling information like color.

The CSS box model shows that every HTML element is contained within a layered box that begins with content at its heart:

The CSS box model (image credit: w3schools.com)

After that, you get the padding, which is wrapped by the border. Lastly, the margin surrounds the whole thing, separating the HTML element from other components.

Having a good grasp of this model can help you make changes to your design and layout without encountering issues. For example, you’ll know how to correctly change the position, size, and alignment of elements on your website.

How to Add Margin in CSS

You can add margin CSS directly to the HTML code of the web page, but it’s better to create a separate CSS file to store this information. This means you don’t have to manually edit each web page that you want to use it on.

To get started, choose the element you want to add margin to (like a heading, button, or similar). Then, you’ll use CSS to set the margin. Note that every HTML element contains four margins that you can format:

  • margin-bottom
  • margin-top
  • margin-right
  • margin-left

If you want to set each margin to the same amount on all sides, you don’t need to specify the individual sides. Let’s say you want a section of your website to have a margin of ten pixels all around. In this case, you’d use the following CSS snippet:

{margin: 10px;}

However, you may only wish to format one margin. For example, if you want to create more space between an element and the bottom of the page, you’ll only need to edit the bottom margin. Therefore, you’ll need to specify this with the following CSS code:

{margin-bottom: 10px;}

Additionally, youcan apply more than one margin value to the same element. However, the CSS will be applied at different rates. Currently, CSS loads and displays in the following way:

  • One margin value means the same margin will be applied to all sides at once.
  • Two values apply to the top and bottom first, and to the right and left second.
  • Three values set the top margin first, the right/left sides second, and the bottom margin third.
  • Four values apply individually in the following order: top, right, bottom, left.

Using multiple margin values enables you to control exactly how your web layout loads for visitors.

How to Add Padding in CSS

Like margins, you can use the padding property along all four sides of an element. Let’s say you want to set an element’s padding to all sides simultaneously to create more whitespace. In this instance, you’d use the following CSS snippet: {padding: 10px;}

However, you can also define the padding of individual sides of an element, using the following properties:

  • padding-bottom
  • padding-top
  • padding-right
  • padding-left

Again, as is the case with margin, you can assign multiple values when defining an element’s padding. The system works exactly as it does for the margin property which we described above. So, if you want to add more than one padding property, visit the previous section to find out how to do this.

How to Add Margin and Padding in WordPress

Typically, your WordPress theme will define the margin and padding for HTML elements. However, if you’re not satisfied with the layout, you can override these settings within the WordPress dashboard.

If you use a block-based theme, you’ll need to open the Full Site Editor by going to Appearance > Editor. Select a template and choose an element on the page. Then, use the Block settings to switch to the Stylestab and scroll down to the Dimensionssection:

How to change CSS margins and CSS padding in WordPress

By default, you can adjust the top-bottom and left-right pairs of sides using the Padding and Margin sliders. If you want to format each side individually, click on the Padding Options icon or the Margin Options icon next to each property label.

If you use a classic WordPress theme, you can still change the margin and padding, but you’ll use the WordPress Customizer. To access this, go to Appearance > Customize and select the Additional CSS tab:

How to change CSS padding and margins in WordPress

In the text field, you’ll first need to define the name of the element you want to edit (e.g. my-heading or my-paragraph). You can find this information by right-clicking on the element and selecting Inspect Element.

Then, write your CSS code for the WordPress block, contained within curly brackets. It might look something like this:

my-paragraph {padding: 10px;} my-paragraph {margin: 10px;}

Note that not all themes allow CSS editing. In this case, you might need to install a separate plugin or create a child theme to carry out your changes.

Conclusion

You can set padding and margin to determine how much space an element has on your web page. These properties are important for making changes to your web design and layout, but they work in different ways.

Margin concerns the space outside the element while padding refers to the space inside the element (between the content and border). If you want to change the size of an element or increase whitespace, use padding. But margin can change the position, and control the distance between elements.

If you’re new to websites, you’ll need a web host to house key site files. NameHero is a great option since you can get a free domain name, a free SSL certificate, and a whole suite of security features. Check out our plans today!

Sophia Lee
Sophia Lee

Sophia is a staff writer at WordCandy.co, where she produces quality blog content for WordPress plugin and theme developers, hosting providers, website development and design agencies, and other online businesses.

Related Posts

How to Create a SaaS Landing Page

In this post, we’ll break down the components of a high-converting SaaS landing page. We’ll then show you how to create one for your product.

How to Create A Lead Capture Page That Converts

In this post, we’ll show you what makes a great lead capture page and how you can easily build your own.

How to Start an Online Store Without Inventory

Let's discuss the benefits of launching a store without inventory and look at some options. We’ll then show you how to sell products online.

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow & Subscribe

Exclusive promos, content and more!


Most Popular Posts

NameHero’s Recommended WordPress Plugin and Theme Setup

WordPress Hosting vs. Web Hosting – What’s The Difference?

How To Increase The InnoDB Buffer Pool Size

How To Fix A Stuck All-in-One WP Migration Import

How To Add A Subdomain In Cloudflare

Top Categories

  • WordPress
  • WordPress Tutorials
  • OpenClaw Hosting
  • Enterprise Hosting
  • WooCommerce
  • Web Hosting
  • Resellers
  • Website Security
  • Website Development
  • Website Performance
  • VPS Hosting
  • SEO Tips
  • Announcements
  • Domain Registration
NameHero

NameHero® proudly provides web hosting to over 40,000 customers with 99.9% uptime to over 750,000 websites.

  • Master Card
  • Visa
  • American Express
  • Discover
  • Paypal
Products
  • Web Hosting
  • Managed VPS Hosting
  • Unmanaged VPS Hosting
  • Flex VPS Hosting
  • WordPress Hosting
  • WooCommerce Hosting
  • Reseller Hosting
  • Enterprise Hosting
  • GPU Hosting
  • Email Hosting
  • HeroicGuard
  • Domains
  • Website Builder
  • AI Agent Hosting
Help & Support
  • NameHero Blog
  • NameHero Gaming Blog
  • Support
  • Help Center
  • Migrations
  • Affiliates
  • Gaming Affiliates
  • Call 1-855-984-6263
Company
  • About Us
  • Contact Sales
  • Reviews
  • Uptime
  • We're Hiring

Copyright © 2026 Name Hero, LLC. All rights reserved.
NameHero® is a registered trademark.

  • Privacy Policy
  • Terms of Use
  • Acceptable Use Policy
  • Payment Policy
  • DMCA