search
HomeWeb Front-endCSS TutorialWhat is the difference between margin and padding

Difference: margin refers to the distance from its own frame to the border of another container, that is, the distance outside the container, which is the distance between elements; padding refers to the distance from its own frame to another container inside itself The distance between borders, that is, the distance within the container, is the distance between the box border and the elements inside the box.

The operating environment of this article: Acer S40-51, HBuilder

css Box Model

Talking about margin and padding, we cannot avoid talking about the CSS Box Model. Generally speaking, the css box model is used for design and layout. It is essentially a box, including: margin, border, padding and the middle content. Recommended:
css video tutorial

The picture below is the box model (here we only talk about the standard box model of the W3C specification, not the non-standard boxes used in IE5 and IE6 in weird mode Model):

##margin and padding

1 (1).png

margin refers to the border from itself to another container border The distance between them is the outer distance of the container; that is, the outer margin. Padding refers to the distance between its own frame and the border of another container inside itself, which is the inner distance of the container; that is, the inner margin.

The difference between margin and padding

margin is the outer margin of the box, that is, the distance between boxes, and padding is the inner edge Distance is the distance between the side of the box and the elements inside the box. (margin is used to separate the distance between elements; padding is used to separate the distance between elements and content. Margin is used for layout, which can separate elements and make them irrelevant to each other; padding is used To set the distance between elements and content, let there be a "breathing distance" between content (text) and (wrapped) elements.)

Grammar structure

(1)padding-left:10px; /margin-left:10px; Inner/outer margin(3)padding-top:10px; /margin-top:10px; Top inner/outer margin(4)padding-bottom:10px; /margin-bottom : 10px; Inner/outer distance

(5) padding: 10px;/margin: 10px; Uniform inner/outer distance of the four sides

(6) padding: 10px 20px;/margin: 10px: 10px: 10px: 10px: 10px: 10px: 10px: 10px: 10px: 10px: 10px: 10px: 10px: 10px: 10px: 10px: 10px 20px;      Top, left and right inner/outer margins

(7)padding:10px 20px 30px;/margin:10px 20px 30px; :10px 20px 30px 40px;/margin:10px 20px 30px 40px; Top, right, bottom, left inner/outer margin

Margin usage instructions:

(1 ) When you need to add a blank space outside the border,

(2) When the blank space does not need to have a background (color),

(3) The blank spaces between the two boxes connected up and down need to be mutually exclusive. When offset, say 15px 20px margin, you will get 20px white space.

Padding usage instructions:

(1) When you need to add a blank space inside the border (often the setting of the distance between text and border),

(2) When a background (color) is required in the blank space,

(3) When the blank space between two boxes connected up and down is expected to be equal to the sum of the two, for example, a 15px 20px padding will result in a 35px blank space. .

About padding:

Note: Setting Padding to a negative value has no effect:

In block elements :

Therefore, padding is under the block-level element, and the top, bottom, left, and right can be set at will, which will change the interior of the element.

In inline elements:

Padding set on inline non-replaced elements does not affect line height calculations; therefore, if an element has both padding The distance and background may visually extend to other lines and may overlap with other content. The element's background extends across the padding. It is not allowed to specify negative margin values. For padding elements of inline elements, only padding-left and padding-right have an effect, and top and bottom are not recognized;

What is the difference between margin and padding

##About margin:

margin: 0 auto; Only works on block-level elements

In block elements:

Therefore, margin is under block-level elements, top, bottom, left, and right Can be set as desired. And the reference base of the margin of the block-level element is the previous element, that is, the margin distance relative to the

element before itself. If the element is the first element, it is the margin distance relative to the parent element.

In inline elements:

margin-top and margin-bottom have no effect on the height of inline elements (rows). If you want to change the inline The line height of an element is similar to the line spacing of text, so you can only use these three attributes: line-height, fong-size, and vertical-align. Remember, it is line-height, not height, that affects the height of inline elements.

What is the difference between margin and paddingwant to For more related articles, please visit PHP中文网

! !

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
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
Simulating Mouse MovementSimulating Mouse MovementApr 22, 2025 am 11:45 AM

If you've ever had to display an interactive animation during a live talk or a class, then you may know that it's not always easy to interact with your slides

Powering Search With Astro Actions and Fuse.jsPowering Search With Astro Actions and Fuse.jsApr 22, 2025 am 11:41 AM

With Astro, we can generate most of our site during our build, but have a small bit of server-side code that can handle search functionality using something like Fuse.js. In this demo, we’ll use Fuse to search through a set of personal “bookmarks” th

Undefined: The Third Boolean ValueUndefined: The Third Boolean ValueApr 22, 2025 am 11:38 AM

I wanted to implement a notification message in one of my projects, similar to what you’d see in Google Docs while a document is saving. In other words, a

In Defense of the Ternary StatementIn Defense of the Ternary StatementApr 22, 2025 am 11:25 AM

Some months ago I was on Hacker News (as one does) and I ran across a (now deleted) article about not using if statements. If you’re new to this idea (like I

Using the Web Speech API for Multilingual TranslationsUsing the Web Speech API for Multilingual TranslationsApr 22, 2025 am 11:23 AM

Since the early days of science fiction, we have fantasized about machines that talk to us. Today it is commonplace. Even so, the technology for making

Jetpack Gutenberg BlocksJetpack Gutenberg BlocksApr 22, 2025 am 11:20 AM

I remember when Gutenberg was released into core, because I was at WordCamp US that day. A number of months have gone by now, so I imagine more and more of us

Creating a Reusable Pagination Component in VueCreating a Reusable Pagination Component in VueApr 22, 2025 am 11:17 AM

The idea behind most of web applications is to fetch data from the database and present it to the user in the best possible way. When we deal with data there

Using 'box shadows' and clip-path togetherUsing 'box shadows' and clip-path togetherApr 22, 2025 am 11:13 AM

Let's do a little step-by-step of a situation where you can't quite do what seems to make sense, but you can still get it done with CSS trickery. In this

See all articles

Hot AI Tools

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.

Undress AI Tool

Undress AI Tool

Undress images for free

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

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.