


What is margin collapse? Under what circumstances does it appear? How to deal with it?
This article will take you to understand the CSS box model and introduce what is margin collapse? Under what circumstances does margin collapse occur? And talk about solutions.
In CSS, all elements are surrounded by "boxes" one by one. We widely use two kinds of "boxes" - block-level boxes (block box
) and inline box (inline box
).
What is the CSS box model?
In CSS, the box model is used during design and layout.
The definition of the box model can be divided into these parts:
-
Content box: This area is used to display content, and the size can be set by
width
andheight
. -
Padding box: The blank area enclosed outside the content area; the size is set through the
padding
related properties. -
Border box: Wrap content and padding. The size is set via the
border
related properties. -
Margin box: This is the outermost area, the blank space between the box and other elements. The size is set via the
margin
related properties.
Block-level boxes fully apply the CSS box model, while inline boxes only use part of the content defined in the box model.
box-sizing
box-sizing
attribute defines how the browser should calculate the total width of an element and total height.
-
content-box
(default value) , which is the standard box model,width: 100px
refers to the content area 100px wide.- The size of the box =
content(100px)
padding
border
- The size of the box =
-
border-box
, which is the alternative (IE) box model,width: 100px
refers to the sum of thecontent area border inner margins
being 100px wide.- The size of the box =
content
padding
border
=100px
- The size of the box =
No matter which model, margin is not included in the actual size - Of course, it will affect the space occupied by the box on the page, but it will affect the space outside the box.
display
Here you can add a concept - internal and external display types.
-
External display type, we set the
display
attribute of the box, such asinline
orblock
, to control whether the box is inline or block level. - Internal display type, which determines how the elements inside the box are laid out.
If display: flex
is set, on an element, the external display type is block
, but the internal display type is changed to flex
. All direct child elements of the box will become flex
elements and will be laid out according to the rules of the flexible box (Flexbox
).
There is also a special value -- display: inline-block
, which provides an intermediate state between inline and block. This is very useful for the following situations: no line wrapping occurs, but the width and height can be set, which means that some block-level effects are achieved:
- Setting
width
and ## The #heightproperty will take effect.
- padding
,
margin, and
borderpush other elements away.
Inline elements/block-level elements
In HTML4, elements are divided into two categories:inline (inline elements) and
block (block-level element).
1. What are inline elements?
An inline element only occupies the space contained by the border of its corresponding label. Common inline elements includea,
b,
span,
img,
strong,
sub
sup、
button、
input、
label、
select、
textarea
2. What are block-level elements?
A block-level element occupies the entire space of its parent element (container), thus creating a "block". Usually browsers will add a new line before and after block-level elements. Common block-level elements includediv,
ul,
ol,
li,
dl,
dt,
dd,
h1,
h2,
h3,
h4 ,
h5、
h6 、
p
3. The difference?
Format (by default), inline elements will not wrap, but block-level elements will.
On content (by default), inline elements can only contain data and other inline elements. Block-level elements can contain inline elements and other block-level elements.
-
On attributes:
-
Inline elements
-
width
andheight
settings are invalid (line-height can be set), - inner margin (
padding
), outer margin (margin
) and borders (border
) in the up and down direction will not affect other elements.
-
-
Block-level elements
-
width
andheight
properties come into play, - Padding (
padding
), margins (margin
), and borders (border
) will "push" other elements away from the surroundings of the current element
-
-
Inline elements
Margin (margin) collapse
block The top margin (margin-top
) and bottom margin (margin-bottom
) are sometimes merged (collapsed) into a single margin whose size is the maximum of the single margin (or If they are equal, then only one of them), a behavior called margin collapsing.
What will happen
The vertical margin of two or more adjacent block elements in the ordinary flow will collapse
- Adjacent: Refers to not being separated by non-empty content, padding, border or clear
- Vertical direction: Refers to only vertical margins. How will
be solved?
The element
BFC
created and its children/siblings will not be collapsed-
Settings
padding
/border
, some specific scenarios:-
# of the parent element ##margin-top
The overlap occurs because they are adjacent, so we can solve the problem through this. We can set theoverlaps with the child element's
margin-top.
border-top
and
padding-topvalues for the parent element to separate them.
- The
margin-bottom
One overlap occurs because they are adjacent, and the other is because the height of the parent element is not fixed. So we can setof the parent element with a height of
autooverlaps with the
margin-bottomof the child element.
border-bottom
,
padding-bottomfor the parent element to separate them, or we can set a height for the parent element,
max-heightand
min-heightcan also solve this problem.
- is an element without content, and its own
margin-top
We can solve this problem by settingand
margin-bottomoverlap.
border
,
paddingor height for it.
-
Factors that trigger BFC
- float
(Except none)
- overflow
(Except visible)
- display
(table-cell / table-caption / inline-block)
- position
(except static / relative)
Programming Video! !
The above is the detailed content of What is margin collapse? Under what circumstances does it appear? How to deal with it?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

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)

Hot Topics



Inline elements include div, p, h1-h6, ul, ol, li, table, form, etc.; block-level elements include span, a, img, strong, em, input, label, etc. The characteristics of the two elements: 1. Inline elements will occupy a row, automatically fill the width of the parent container, can set width, height, inner and outer margins and other attributes, and can contain other block-level elements and inline elements; 2. Inline elements will not It will occupy an exclusive line, the width and height are determined by the content, and the inner and outer margins only affect the arrangement of the elements themselves, etc.

There are five differences between inline elements and block-level elements: "box model", "arrangement", "content display", "relative position" and "default size": 1. Inline elements do not generate independent boxes, width and height Determined by the content, block-level elements will generate an independent rectangular frame, and attributes such as width, height, margins, and padding can be set; 2. Inline elements are arranged horizontally on the same line, while block-level elements will be pressed from top to bottom. Arrange in order; 3. Inline elements cannot contain block-level elements, while block-level elements can contain other block-level elements and inline elements.

HTML inline elements include: a (anchor element), b (bold), br (line break), code, em (emphasis), font (font setting), i (italic), img (picture), input (input box) ), span, strong (bold emphasis), textarea, u, etc.; block elements include: address (address), blockquote (block quote), center (center-aligned block), div, h1~h6 (title), hr ( Horizontal dividing line), p, ul, ol, etc.

The difference between inline elements and block-level elements: In-depth understanding of the classification of elements in HTML In HTML, elements can be divided into two categories: inline elements and block-level elements. Understanding their differences is very important to correctly master the layout and style of HTML. This article will provide an in-depth understanding of the characteristics of inline elements and block-level elements, and provide specific code examples. Inline elements Inline elements are elements that are displayed inline by default in HTML documents. They do not occupy an entire row, but appear closely within a row based on how the document flow is arranged. Common in the industry

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:

Commonly used inline elements and block-level elements in HTML are: 1. Block-level elements include semantics such as <div>, <p>, <ul>, <ol>, <li>, <h1>~<h6>, and <header>. tags; 2. Inline elements include tags such as <span>, <a>, <strong>, <em>, <img>, and <input>.

Inline elements include a, span, strong, b, em, i, label, img, input, select, textarea, button, abbr, cite, code, big, small, sub, sup, etc. Block-level elements include div, p, h1-h6, form, ul, ol, dl, dt, dd, li, table, tr, td, th, hr, blockquote, address, menu, pre, etc.

Detailed explanation of CSS inline elements and block-level elements: Explore their application scenarios and usage methods In CSS, elements can be divided into two types based on their display characteristics: inline elements and block-level elements. For web developers, it is very important to understand these two concepts, because their different characteristics determine their application scenarios and usage methods. Inline elements are elements that occupy only one line in a web page. Common inline elements include <span>, <img>, and <input>
