overflow:hidden不会剪辑绝对定位的内容 [英] overflow:hidden does not clip absolute positioned content

查看:120
本文介绍了overflow:hidden不会剪辑绝对定位的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个包含滚动文字的框。问题是,当文本在文本框的一半时,它会在我希望它被剪辑时显示在外面。



HTML / CSS非常简单,我没有线索可能会出错:

#vbox {width:100px;身高:500px;溢出:隐藏;背景:#afa;}#vtext {position:absolute;宽度:100px;

溢出:隐藏;是否隐藏溢出内容?



编辑:

相对的问题解决了这个问题,但我仍然困惑......有人可以解释这种行为吗?

解决方案

绝对 - 如果具有该设置的元素不包含(或不包含)其包含的块(通常,这意味着它没有定位),那么定位元素不会受到任何 overflow 设置的影响。



在您的情况下,该框未定位,因此文本将锚定到视口而不是框。该框不知道文本,并且视口足够大以包含文本,因此根本不发生剪切。您可以在规格的第11.1节中找到血淋淋的细节。

给出您的方格 position:relative 将导致文本相对于方框被定位,并被裁剪为一个结果。



#vbox {position:relative;宽度:100px;身高:500px;溢出:隐藏;背景:#afa;}#vtext {position:absolute;宽度:100px;

I am trying to make a box with scrolling text inside. The problem is, when the text is halfway up the box, it is showing outside it when I expect it to be clipped.

The HTML/CSS is very simple and I have no clue what could be going wrong :

#vbox {
  width: 100px;
  height: 500px;
  overflow: hidden;
  background: #afa;
}
#vtext {
  position: absolute;
  width: 100px;
  top: 250px;
}

<div id="vbox">
  <div id="vtext">
    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.</p>
    <p>Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p>
    <p>Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim.</p>
    <p>Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo.</p>
    <p>Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi.</p>
    <p>Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a,</p>
  </div>
</div>

Isn't "overflow:hidden;" suppposed to hide the overflowing content ?

Edit :

Adding "postion:relative" to the box solves the issue but I'm still puzzled... Could someone explain this behaviour ?

解决方案

Absolutely-positioned elements are not affected by any overflow setting if the element with that setting is not (or does not contain) its containing block (usually, this means it's not positioned).

In your case, the box is not positioned, so the text is anchored to the viewport instead of the box. The box doesn't know about the text and the viewport is large enough to contain the text, so no clipping occurs at all. You can find the gory details in section 11.1 of the spec.

Giving your box position: relative will cause the text to be positioned relative to the box, and be clipped as a result.

#vbox {
  position: relative;
  width: 100px;
  height: 500px;
  overflow: hidden;
  background: #afa;
}
#vtext {
  position: absolute;
  width: 100px;
  top: 250px;
}

<div id="vbox">
  <div id="vtext">
    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.</p>
    <p>Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p>
    <p>Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim.</p>
    <p>Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo.</p>
    <p>Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi.</p>
    <p>Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a,</p>
  </div>
</div>

这篇关于overflow:hidden不会剪辑绝对定位的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆