What is the difference between margin and padding?
Jun 21, 2025 am 12:03 AMThe difference between margin and padding lies in their placement within the box model: margin is outside the border, while padding is inside. Margin controls spacing between elements, can collapse, and supports negative values; padding affects internal spacing, expands the element’s size, and does not collapse. Use margin for spacing elements apart and padding to adjust internal content spacing or background area.
The difference between margin and padding comes down to what part of the box model they affect. In simple terms: margin is outside the border, while padding is inside.
When you're trying to control spacing in your layout—whether it's pushing elements away from each other or adjusting space within an element itself—understanding this distinction helps avoid confusion and layout bugs.
What Margin Does
Margin sits outside the border of an element. It creates space between this element and other elements around it.
- Use margin when you want to push another element away.
- Margins can collapse (more on that later).
- You can set them individually (
margin-top
,margin-left
, etc.) or all at once with shorthand likemargin: 10px 20px
.
For example, if you have two paragraphs one after another, setting a bottom margin on the first will create space before the second starts.
p { margin-bottom: 20px; }
This is especially useful in layouts where spacing between components matters more than internal spacing.
What Padding Does
Padding is the space between the content and the border of an element.
- It affects the size of the element itself.
- Padding doesn't collapse like margins do.
- Also supports shorthand values like
padding: 10px 15px
.
Let’s say you have a <div>
with text inside. If you add padding, the text won’t stick right up against the edge anymore.
.box { padding: 15px; }
It also impacts background color or images—if you set a background on the element, it’ll extend into the padding area.
Key Differences That Matter
Here are some practical differences you’ll notice:
- Margins don’t affect background, but padding does.
- Margins can be negative, which lets you pull elements closer together. Padding can’t be negative.
- When calculating width/height:
- Total width = width padding-left padding-right border margin-left margin-right
- But padding expands the element even without considering margins.
Also, margin collapsing happens when vertical margins of block elements touch each other—they merge into one margin. Padding doesn’t do that.
When to Use Which
Use margin when you're dealing with spacing between elements.
Use padding when you're adjusting internal spacing or sizing an element’s clickable area (like for buttons).
A common use case:
- A card component might have padding so its inner content isn’t flush with the edges.
- Then, margins are used to separate that card from other cards or sections.
Another tip: if you’re using CSS Flexbox or Grid, you might still need margins or padding to fine-tune spacing beyond what gap properties offer.
Basically, think of margin as how much breathing room your element needs from others, and padding as how much personal space the content inside needs. They serve different roles but often work together.
The above is the detailed content of What is the difference between margin and padding?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

In CSS, margin is a property used to set the outer margins of an element. Margins are the space between an element's border and its content. Margin can accept the following values: 1. A single value: for example, margin: 10px; Set all four margins (top, right, bottom, left) to 10 pixels; 2. Two values: for example, margin : 10px 20px; Set the top and bottom margins to 10 pixels, and the left and right margins to 20 pixels; 3, four values, and so on.

CSS border properties explained in detail: padding, margin and borderCSS is a style sheet language used to control and layout web page elements. In web design, the border attribute is one of the most important parts. This article will introduce in detail how to use the border attribute in CSS and provide specific code examples. padding The padding property is used to set the padding of an element, which is the space between the element's content and the element's borders. We can set padding using positive numbers or percentage values

In CSS, the padding property is used to set the padding of an element. This means it defines the space between the element's content and its border. The basic syntax is "padding: value;".

Detailed explanation of CSS text layout properties: text-overflow and white-space In web design, text layout is a very important link. Reasonable layout can make the text more readable and beautiful. CSS provides several properties to control how text is displayed, including text-overflow and white-space. This article will detail the usage and sample code of these two properties. 1. text-overflow attribute text

In HTML, margin means "margin", which refers to the blank area surrounding the border of an element; setting margins will create additional "blank" outside the element, allowing a "blank" distance between boxes. . To set margins, you need to use the css margin property, which accepts any length unit, percentage value, or even negative value.

The effect of margin on inline elements is different from that of block-level elements. In inline elements, the margin attribute only affects the vertical top and bottom margins, not the horizontal left and right margins. For example, if there is a paragraph element in HTML, we can set some styles for it and observe the effect of the margin attribute on it. The HTML code looks like this:

Exploration of CSS box model properties: padding, margin and border The CSS box model is one of the important concepts in web page layout. In front-end development, understanding and correctly using padding, margin and border attributes is key. This article will delve into the usage and correlation of these three properties, and provide specific code examples. 1. Introduction to the box model The box model consists of four parts: content, padding, bo

css file margin is a css attribute used to define the space around the element; margin represents the outer margin. You can change the top, bottom, left, and right margins of the element individually, or you can change all attributes at once; the margin attribute accepts any length unit, percentage values ??or even negative values.
