search
HomeWeb Front-endCSS TutorialAbout the reasons why overflow hiding (overflow:hidden) fails

This article mainly introduces the reasons for the failure of overflow hiding (overflow:hidden). It has a certain reference value. Now I share it with you. Friends in need can refer to it.

There are often problems in the project A classmate encountered such a problem. The phenomenon was that overflow:hidden was set for the element, but the part beyond the container was not hidden. Could it be that the hidden setting was invalid?

In fact, there are reasonable explanations behind seemingly unreasonable phenomena.

We know that the overflow attribute values ​​have the following types:

visible:声明内容不会被剪裁。比如内容可能被渲染到容器外面。
hidden:声明内容将被剪裁,并且也甭想使用滚动条来查看剪裁掉的内容。
scroll:声明内容将被剪裁,但有可能出现滚动条来查看被剪裁掉的内容。滚动条出现的位置在inner border adge和outer padding adge之间。
auto:声明决策将依赖于客户端,优先使用scroll。

The W3C standard states:
Usually the content of a box is limited to the boundaries of the box. But sometimes overflow occurs, that is, part or all of the content runs outside the bounds of the box. Overflow will occur when one of the following conditions is met:

1. 一个不换行的行元素宽度超出了容器盒子宽度。
2. 一个宽度固定的块元素放在了比它窄的容器盒子内。
3. 一个元素的高度超出了容器盒子的高度。
4. 一个子孙元素,由负边距值引起的部分内容在盒子外部。
5. text-indent属性引起的行内元素在盒子的左右边界外。
6. 一个绝对定位的子孙元素,部分内容在盒子外。但超出的部分不是总会被剪裁。子孙元素的内容就不会被子孙元素和其包含块之间的祖先元素的overflow的设置所剪裁。

When overflow occurs, the overflow attribute stipulates whether the container box clips the part beyond its inner boundary, and determines whether a scroll bar appears to access the clipped content. It will affect the clipping of all content of the element, but there is an exception, which is mentioned in item 6 above: the containing block of the descendant element of the element (Containing blocks) is the entire viewport (viewport) or the ancestor element of the element , the content will not be clipped. What is a containing block? Simply put, it is a block that can determine the position and size of an element. Typically an element's containing block is determined by the content boundaries of its nearest block-level ancestor. But when an element is set to absolute positioning, the containing block is determined by the nearest ancestor element whose position is not static.

It seems a bit convoluted, let’s listen to a simple story.

html fragment:

<p class=”ocean”>
    <p class=”land”>
        <p class=”joke”>
                Mrs. Smith couldn’t get her husband to exercise. 
                She asked Mrs. Jones what she should do. Jones replied, 
                “Tape the remote control between his toes.”
        </p>
    </p>
</p>

style:

p.ocean{
    position:relative;
    background-color:blue;
    width:120px;
    height:120px;
    }
p.land{
    width:100px;
    height:100px;
    background-color:red;
    overflow:hidden;
    }
p.joke{
    width:150px;
    height:110px;
    margin-top:30px;
    margin-left:30px;
    background-color:yellow;
    }

The above code tells a story: There is a red earth in the blue ocean, and there is a red earth in the red earth. A pornographic joke. Due to the setting of the paragraph style, some of its content exceeds the red earth. In order to prevent yellow jokes from contaminating the blue ocean, the red earth vigilantly sets overflow:hidden for itself; so that the yellow part beyond the earth is cut off, and what we see will be such a harmonious scene, as shown in Figure 1:

About the reasons why overflow hiding (overflow:hidden) fails

Figure 1: Harmonious Planet

If everything were so reasonable and orderly, the world would not be peaceful. Not long after, Huang Duanzi felt that because of his prominent status, he should not be controlled by the red earth, so he racked his brains to change himself into an absolute position, and suddenly got rid of the shackles of the earth, as shown in Figure 2:

p.joke{
    position:absolute;
    width:150px;
    height:110px;
    top:30px;
    left:30px;
    background-color:yellow;
    }

About the reasons why overflow hiding (overflow:hidden) fails

Picture 2: Rampant jokes

Why is this happening? This creates the sixth condition mentioned above. When the yellow paragraph changes to position:absolute, its containing block has been upgraded from the original content boundary of the red earth to the blue ocean where the position closest to it is not static. The ocean doesn't know anything about it at this moment. It has not set the overflow:hidden attribute, so all the parts of the pornographic jokes that should have been cropped are visible. It not only pollutes the ocean, but also affects the entire planet. The situation is extremely urgent. Even if the ocean is set to overflow:hidden at this time, it can only clip the yellow part beyond the blue ocean. Just like Figure 3, the ocean is at a loss at this time.

About the reasons why overflow hiding (overflow:hidden) fails

Picture 3: Ocean of Innocence

As the saying goes, the devil is as high as the road, and to untie the bell, you must tie the bell. How can the red earth be willing to run away with jokes? After all, the earth is the ancestral element of Duanzi, so how can he be willing to let Duanzi do whatever he wants? So Dadi went through a lot of hardships, found the secret book, added the position:relative attribute to his style, and changed the containing block of the paragraph to Dadi to decide. Now Duanzi was locked up obediently. The planet looks like it's back to its original state.

p.ocean{
    position:relative;
    background-color:blue;
    width:120px;
    height:120px;
    }
p.land{
    position:relative;
    width:100px;
    height:100px;
    background-color:red;
    overflow:hidden;
    }
p.joke{
    position:absolute;
    width:150px;
    height:110px;
    top:30px;
    left:30px;
    background-color:yellow;
    }

So, hidden has not failed, but it is possible that the situation we encountered happened to meet the sixth condition, causing the containing block of the element to change. In the above story, it is also mentioned that when encountering the situation where 'hidden' fails, you can change the containing block of the element as needed to achieve the purpose of justice.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

How to solve the overflow:auto invalid method in ie7

About the pseudo-class hover in IE Usage and BUG

How to use css3 to make a 0.5px thin line

The above is the detailed content of About the reasons why overflow hiding (overflow:hidden) fails. 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
Where should 'Subscribe to Podcast' link to?Where should 'Subscribe to Podcast' link to?Apr 16, 2025 pm 12:04 PM

For a while, iTunes was the big dog in podcasting, so if you linked "Subscribe to Podcast" to like:

Browser Engine DiversityBrowser Engine DiversityApr 16, 2025 pm 12:02 PM

We lost Opera when they went Chrome in 2013. Same deal with Edge when it also went Chrome earlier this year. Mike Taylor called these changes a "Decreasingly

UX Considerations for Web SharingUX Considerations for Web SharingApr 16, 2025 am 11:59 AM

From trashy clickbait sites to the most august of publications, share buttons have long been ubiquitous across the web. And yet it is arguable that these

Weekly Platform News: Apple Deploys Web Components, Progressive HTML Rendering, Self-Hosting Critical ResourcesWeekly Platform News: Apple Deploys Web Components, Progressive HTML Rendering, Self-Hosting Critical ResourcesApr 16, 2025 am 11:55 AM

In this week's roundup, Apple gets into web components, how Instagram is insta-loading scripts, and some food for thought for self-hosting critical resources.

Git Pathspecs and How to Use ThemGit Pathspecs and How to Use ThemApr 16, 2025 am 11:53 AM

When I was looking through the documentation of git commands, I noticed that many of them had an option for . I initially thought that this was just a

A Color Picker for Product ImagesA Color Picker for Product ImagesApr 16, 2025 am 11:49 AM

Sounds kind of like a hard problem doesn't it? We often don't have product shots in thousands of colors, such that we can flip out the with . Nor do we

A Dark Mode Toggle with React and ThemeProviderA Dark Mode Toggle with React and ThemeProviderApr 16, 2025 am 11:46 AM

I like when websites have a dark mode option. Dark mode makes web pages easier for me to read and helps my eyes feel more relaxed. Many websites, including

Some Hands-On with the HTML Dialog ElementSome Hands-On with the HTML Dialog ElementApr 16, 2025 am 11:33 AM

This is me looking at the HTML element for the first time. I've been aware of it for a while, but haven't taken it for a spin yet. It has some pretty cool and

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

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)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.