溢出:隐藏忽略底部填充

Jam*_*ore 23 html css xhtml

在以下示例中,将忽略底部填充,并且文本将在隐藏之前流向元素的底部.是什么造成的?

<div style="overflow: hidden; width: 300px; height: 100px; padding: 50px;">
  <p>Hello, this is text.</p>
  <p>Hello, this is text.</p>
  <p>Hello, this is text.</p>
  <p>Hello, this is text.</p>
  <p>Hello, this is text.</p>
  <p>Hello, this is text.</p>
</div>
Run Code Online (Sandbox Code Playgroud)

Firebug的视图(紫色是填充,蓝色是实际内容区域):

萤火

Mar*_*uso 17

我更喜欢使用尽可能少的<div>s.

<div class="container">
  <p>Hello, this is text.</p>
  <p>Hello, this is text.</p>
  <p>Hello, this is text.</p>
  <p>Hello, this is text.</p>
  <p>Hello, this is text.</p>
  <p>Hello, this is text.</p>
  <p>Hello, this is text.</p>
  <p>Hello, this is text.</p>
  <p>Hello, this is text.</p>
  <p>Hello, this is text.</p>
  <p>Hello, this is text.</p>
</div>

.container{
  padding: 30px 30px 0;
  position: relative;
  width: 240px;
  height: 170px;
  border:1px solid #000;
  overflow: hidden;
}
.container:after{
  content: ' ';
  display: block;
  background-color: red;
  height: 30px;
  width: 100%;
  position: absolute;
  bottom: 0;
}
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/bgaR9/

自然而然,在没有IE <9的世界里

  • 它贡献得很好.他只是试图在不添加额外div的情况下解决问题.我个人认为这是一个很好的解决方案,因为这里也很少. (5认同)
  • 请查看此答案。它似乎对这个问题没有帮助。 (2认同)

Sag*_*til 10

底部填充存在,但内容向下推动底部填充,因溢出而无法看到:隐藏.您可以做的是将内容放在容器中,如下所示:

<div style="overflow:hidden; width: 300px; height: 200px; border:1px solid #000; ">
    <div style="overflow:hidden; width:auto; height:140px; border:30px solid #ff0000;">
        <p>Hello, this is text.</p>
        <p>Hello, this is text.</p>
        <p>Hello, this is text.</p>
        <p>Hello, this is text.</p>
        <p>Hello, this is text.</p>
        <p>Hello, this is text.</p>
        <p>Hello, this is text.</p>
        <p>Hello, this is text.</p>
        <p>Hello, this is text.</p>
        <p>Hello, this is text.</p>
        <p>Hello, this is text.</p>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

你可以在这里玩小提琴 - http://jsfiddle.net/BMZeS/

希望这可以帮助.


Jam*_*gne 5

我认为如果不添加第二个 div,您将无法获得所需的效果。

http://jsfiddle.net/Mxhzf/

(添加了背景颜色以清楚显示正在发生的事情)

HTML:

<div id="outer">
    <div id="inner">

        <!-- CONTENT HERE -->

    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS:

#outer{
     width: 300px;  
     height: 300px; 
    padding: 20px;
}

#inner{
     width: 300px;
    height: 300px;
    overflow: hidden;
}
Run Code Online (Sandbox Code Playgroud)