国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
What margin does
What padding does
Visualizing the difference
When to use one over the other
Use margin when:
Use padding when:
Home Web Front-end CSS Tutorial What is the difference between `margin` and `padding`?

What is the difference between `margin` and `padding`?

Jul 16, 2025 am 02:03 AM
margin padding

In CSS, the main difference between margin and padding is that they affect different parts of the box model. 1. margin controls the space outside the element border, which is used to adjust the distance between the element and other elements; 2. padding controls the space inside the element border, which is used to adjust the distance between the content and the border. For example, setting .box { margin: 20px; } will create 20px external space around the element, while setting .box { padding: 15px; } will create 15px internal space between the content and the border. In use scenarios, if you want to separate blocks or center elements, use margin; if you want to add space to text in the button or expand the background area, use padding. The two can also be used in combination, such as setting the inner and outer spacing for the card components at the same time. After understanding the box model, it will be more natural to choose margin or padding.

What is the difference between `margin` and `padding`?

The difference between margin and padding in CSS boils down to what part of the box model they affect. In short: margin is outside the border, padding is inside .

What is the difference between `margin` and `padding`?

Let's break it down into practical terms you might actually use when building layouts.


What margin does

Margin controls the space outside of an element's border — basically how much space it takes up on the page in relation to other elements.

What is the difference between `margin` and `padding`?
  • If you want two boxes to be spaced apart from each other, you'd typically adjust their margins.
  • Margins can collapse (especially top and bottom ones), which is a behavior that often surprises new developers.
  • You can set individual sides ( margin-top , margin-right , etc.) or use shorthand like margin: 10px 20px; .

Example:

 .box {
  margin: 20px;
}

This means there will be 20px of space around all four sides of .box between it and neighboring elements.

What is the difference between `margin` and `padding`?

What padding does

Padding affects the space inside the element — between the content and the border.

  • Padding increases the size of the element itself.
  • It affects background color or images — those will extend into the padding area.
  • Padding does not collapse like margins do.

Example:

 .box {
  padding: 15px;
}

This pushes the content inside the box 15px away from the edges, making the box visually larger.


Visualizing the difference

Think of a picture frame:

  • The content is the photo inside.
  • The padding is the mating around the photo.
  • The border is the actual frame.
  • The margin is how far the whole frame sits from other frames on the wall.

So:

  • Use padding if you want more breathing room inside the box.
  • Use margin if you want to push other elements away from the box.

When to use one over the other

Here are some real-world scenarios:

Use margin when:

  • You want to separate blocks from each other.
  • You're centering an element with margin: 0 auto; .
  • You need negative spacing (eg, pull an element left or right).

Use padding when:

  • You want to create space around text inside a button.
  • You're adjusting spacing without affecting adjacent elements.
  • You want background colors or borders to include the extra space.

Sometimes both are used together — for example, giving a card component some internal spacing with padding and external spacing with margin.


That's the basic idea. Once you get comfortable seeing the box model in your mind, choosing between margin and padding becomes second nature.

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What does margin mean in css What does margin mean in css Dec 18, 2023 am 10:30 AM

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.

Detailed explanation of CSS border properties: padding, margin and border Detailed explanation of CSS border properties: padding, margin and border Oct 21, 2023 am 11:07 AM

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

How to use padding attribute in css How to use padding attribute in css Dec 07, 2023 pm 02:58 PM

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 Detailed explanation of CSS text layout properties: text-overflow and white-space Oct 20, 2023 am 11:19 AM

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

The margin attribute does not affect inline elements The margin attribute does not affect inline elements Feb 18, 2024 pm 04:36 PM

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:

Exploring CSS box model properties: padding, margin and border Exploring CSS box model properties: padding, margin and border Oct 20, 2023 pm 03:09 PM

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

What does margin mean in html What does margin mean in html Sep 13, 2021 pm 04:26 PM

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.

What does css file margin mean? What does css file margin mean? Jan 30, 2023 am 09:34 AM

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.

See all articles