Home Web Front-end CSS Tutorial Graphical demonstration of in-depth analysis of CSS margin border overlay_Experience exchange

Graphical demonstration of in-depth analysis of CSS margin border overlay_Experience exchange

May 16, 2016 pm 12:07 PM

Boundary overlay is a fairly simple concept. However, it can cause a lot of confusion when laying out web pages in practice. Simply put, when two vertical boundaries meet, they form a boundary. The height of this boundary is equal to the greater of the heights of the two superimposed boundaries.

When an element appears on top of another element, the bottom border of the first element overlaps with the top border of the second element, as shown in the figure:


元素的顶边界与前面元素的底边界发生叠加


  当一个元素包含在另一个元素中时(假设没有填充或边框将边界分隔开),它们的顶和/或底边界也发生叠加,见图:


元素的顶边界与父元素的顶边界发生叠加


  尽管初看上去有点儿奇怪,但是边界甚至可以与本身发生叠加。假设有一个空元素,它有边界,但是没有边框或填充。在这种情况下,顶边界与底边界就碰到了一起,它们会发生叠加,见图:


元素的顶边界与底边界发生叠加


  如果这个边界碰到另一个元素的边界,它还会发生叠加,见图:


空元素中已经叠加的边界与另一个空元素的边界发生叠加


  这就是一系列空的段落元素占用的空间非常小的原因,因为它们的所有边界都叠加到一起,形成一个小的边界。

  边界叠加初看上去可能有点儿奇怪,但是它实际上是有意义的。以由几个段落组成的典型文本页面为例(见图2-8)。第一个段落上面的空间等于段落的顶边界。如果没有边界叠加,后续所有段落之间的边界将是相邻顶边界和底边界的和。这意味着段落之间的空间是页面顶部的两倍。如果发生边界叠加,段落之间的顶边界和底边界就叠加在一起,这样各处的距离就一致了


边界叠加在元素之间维护了一致的距离


  只有普通文档流中块框的垂直边界才会发生边界叠加。行内框、浮动框或绝对定位框之间的边界不会叠加。

  边界叠加的问题
  边办叠加是一个如果误解就会导致许多麻烦的CSS特性。请参考div元素内嵌套段落的简单示例:
复制代码 代码如下:

 

This paragraph has a 20px margin.

 
 

div 框设置了10像素边界,段落设置了20像素的边界:

复制代码 代码如下:

#box{ 
margin:10px; 
background-color:#d5d5d5; 

p{ 
margin:20px; 
background-color:#6699ff; 


  你会自然地认为产生的样式会像图1-1那样,在段落和div之间有20像素的距离,在div外边围绕着10像素的边界。


图1-1

  但是,产生的样式实际上像图1-2。

图1-2

[Ctrl A Select all Note:If you need to introduce external Js, you need to refresh it before it can be executed]

Two situations happened here. First, the paragraph's 20-pixel top and bottom borders overlap with the div's 10-pixel border to form a single 20-pixel vertical border. Second, instead of being surrounded by the DIV, these borders protrude beyond the top and bottom edges of the DIV. This occurs due to the way elements that have block-level children calculate their height.

If an element has no vertical border or padding, its height is the distance between the top and bottom border edges of its containing child elements. Therefore, the top and bottom margins of the containing child element protrude outside the container element. However, there is a simple solution. By adding a vertical border or padding, the whitespace no longer overlaps, and the element's height is the distance between the top and bottom whitespace edges of its containing child elements.

To make the previous example look like Figure 1-1, just add padding or a border around the div:
Copy code The code is as follows:

#box{
margin:10px;
padding:1px;/*or border:1px solid color;*/
background-color:#d5d5d5;
}
p{
margin:20px;
background-color:#6699ff;
}

Border Most issues with overlays can be fixed by adding a transparent border or a 1px padding.

Supplementary solutions:
Copy code The code is as follows:

   1.Outer layer padding
 2.Transparent border border:1px solid transparent;
 3.Absolute positioning postion:absolute:
 4.Outer layer DIV overflow:hidden;
 5.Inner layer DIV plus float:left; display:inline;
6. The outer DIV sometimes uses zoom:1;
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Working With GraphQL Caching Working With GraphQL Caching Mar 19, 2025 am 09:36 AM

If you’ve recently started working with GraphQL, or reviewed its pros and cons, you’ve no doubt heard things like “GraphQL doesn’t support caching” or

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Creating Your Own Bragdoc With Eleventy Creating Your Own Bragdoc With Eleventy Mar 18, 2025 am 11:23 AM

No matter what stage you’re at as a developer, the tasks we complete—whether big or small—make a huge impact in our personal and professional growth.

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It's out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That's like this.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I'd say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

See all articles