Web Front-end
HTML Tutorial
Why does the margin-top of css child elements affect the parent element_html/css_WEB-ITnose
Why does the margin-top of css child elements affect the parent element_html/css_WEB-ITnose
For details, please click
This problem has puzzled me for a long time. Although I figured out how to solve it long ago without any serious problems, I still don’t understand. What's the reason? If it's just IE that has a problem, I wouldn't care too much. But the problem is that all high-end browsers behave like this. How can I feel at ease? Today I finally found out what was going on, and it turned out to be the CSS2.1 box model specification... although it is an awkward, very awkward rule. The problem is as shown below. In the two-layer Div structure, the Outer Div attribute is "margin:0 auto", which should be close to the top of the outer frame. If there is no Inner Div, or there is no Inner Div The "margin-top" attribute works as expected. But when Inner Div sets "margin-top:10px", its parent element Outer Div is also stretched out with a "margin-top:10px" that should not be there. Effect.
Inner Div [margin-top: 10px]
Outer Div [margin: 0 auto]
HTML Demo Area
This "problem"... is it what is specified in the box model of CSS2.1? Collapsing margins:
In this specification, the expression collapsing margins means that adjoining margins (no non -empty content, padding or border areas or clearance separate them) of two or more boxes (which may be next to one another or nested) combine to form a single margin. The margins of all adjacent two or more box elements will Merge into one margin and share it. Adjacency is defined as: sibling or nested box elements, and there is no non-empty content, Padding or Border separation between them.
That’s why. "Nested" box elements are also considered "adjacent" and will also have Collapsing Margins. This merged margin is actually very common. It is the article paragraph element
. When there are many parallel elements, each one has a margin of 1em above and below, but only 1em is displayed between adjacent 2em spaced instead of added. This is easy to understand. I just wonder why W3C wants nested elements to share margins. I can’t think of any situation where such behavior is needed. There are many ways to avoid this problem, as long as you destroy the conditions for it to occur. Add padding/border to Outer Div, or set Outer Div / Inner Div to float/position:absolute (CSS2.1 stipulates that floating elements and absolutely positioned elements do not participate in margin folding ). For more information, regarding the calculation method of the collapsed performance of margins with different values, etc., you can refer to the W3C CSS2.1 specification: Collapsing margins.There are many solutions:
1. Add overflow:hidden or padding, border to the parent;
2. Use padding instead of margin
Source :http://www.cnblogs.com/hejia/archive/2013/05/26/3099697.html
For more html5 content, please click
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
AI Hentai Generator
Generate AI Hentai for free.
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
1386
52
What is the purpose of the <progress> element?
Mar 21, 2025 pm 12:34 PM
The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati
Is HTML easy to learn for beginners?
Apr 07, 2025 am 12:11 AM
HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.
What is the purpose of the <datalist> element?
Mar 21, 2025 pm 12:33 PM
The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159
What is the purpose of the <meter> element?
Mar 21, 2025 pm 12:35 PM
The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex
What is the viewport meta tag? Why is it important for responsive design?
Mar 20, 2025 pm 05:56 PM
The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.
What is the purpose of the <iframe> tag? What are the security considerations when using it?
Mar 20, 2025 pm 06:05 PM
The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.
The Roles of HTML, CSS, and JavaScript: Core Responsibilities
Apr 08, 2025 pm 07:05 PM
HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.
What is an example of a starting tag in HTML?
Apr 06, 2025 am 12:04 AM
AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.


