简体   繁体   中英

Why does the image have overflow:hidden?

I'm wondering why overflow: hidden is automatically applied in this code. I know it has to do with float , but I still don't get why.

 img { float: right; margin: 0 0 10px 10px; } 
 <p>In this example, the image will float to the right in the paragraph, and the text in the paragraph will wrap around the image.</p> <p> <img src="http://www.w3schools.com/css/w3css.gif" alt="W3Schools.com" width="2000" height="140">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum, nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est, ultrices nec congue eget, auctor vitae massa. Fusce luctus vestibulum augue ut aliquet. Mauris ante ligula, facilisis sed ornare eu, lobortis in odio. Praesent convallis urna a lacus interdum ut hendrerit risus congue. Nunc sagittis dictum nisi, sed ullamcorper ipsum dignissim ac. In at libero sed nunc venenatis imperdiet sed ornare turpis. Donec vitae dui eget tellus gravida venenatis. Integer fringilla congue eros non fermentum. Sed dapibus pulvinar nibh tempor porta. Cras ac leo purus. Mauris quis diam velit.</p> 

I think that you are looking at one of those edge cases in the CSS specification.

In your example, if you had floated the image to the left, you would see a horizontal scroll bar, as expected.

However, it appears that elements that are floated to the right and cause an overflow condition on the left edge are clipped.

The CSS 2.1 specification alludes to this in the following line:

Even if 'overflow' is set to 'visible', content may be clipped to a UA's document window by the native operating environment.

Reference: https://www.w3.org/TR/CSS21/visufx.html#overflow

The same effect also takes place if you were to use absolute positioning on the image.

Setting the offset to left: 0 would trigger a scroll bar, whereas setting right: 0 instead would force the image to be clipped.

The people who could best answer why browsers work this way are those who actually wrote the CSS rendering engines used in the modern browsers.

Regardless, you raised an interesting point.

Actually, overflow: hidden is not being applied anywhere. The image and the containing p element are both overflow: visible . You can verify this in dev tools:

在此输入图像描述

However, by applying float: right to the image you have removed overflow from consideration. In other words, the overflow property has no effect.

Think about the flow of content.

In left-to-right language mode, content overflows to the right. It does not overflow to the left.

Hence, the overflow property doesn't apply to the left because there is technically no overflow.

From the spec:

11.1.1 Overflow: the overflow property

This property specifies whether content of a block container element is clipped when it overflows the element's box.

Again, in LTR reading/writing mode, content does not overflow to the left. The opposite would be true in RTL. Use the CSS direction property to switch between them.

This is why, as pointed out in @MarcAudet's answer , a scroll bar works with float: left . But there is no scrollbar in your code with float: right .

Here are some workarounds:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM