2009-03-05 223 views
6

我使用了这个page中概述的等高CSS技巧。避免当元素在“overflow:hidden”元素内时被截断

直到今天,这一切都工作正常,当我需要添加一个对话框内的一列,这是绝对定位,把它从流中。问题是,由于容器上有“溢出:隐藏”,当溢出时对话将被切断。

除了在容器元素外部引入对话之外,是否有任何可能的方式让它通过CSS显示?

下面是一个小例子,演示了我提到过的内容。

<style> 
.container { overflow: hidden; margin-top: 30px } 
.col { 
    float: left; 
    padding-bottom: 2000px; 
    margin-bottom: -2000px; 
    border-right: 1px solid black; 
    width: 100px; 
    background-color: grey; 
} 
.col.third { border-right: none } 

#div-a { position: relative } 
#div-b { 
    position: absolute; 
    background-color: red; 
    width: 35px; 
    height: 350px; 
    bottom: 0; 
    right: 0; 
    margin: 5px; 
    border: 2px solid black; 
} 
</style> 

<div class="container"> 
    <div class="col first"> 
     <p style="height: 100px">One</p> 
    </div> 
    <div class="col second"> 
     <p style="height: 300px">Two</p> 
     <div id="div-a"> 
      <!-- this gets cut off by the "overflow: hidden" on div.container --> 
      <div id="div-b"> 
       Foo 
      </div> 
     </div> 
    </div> 
    <div class="col third"> 
     <p style="height: 200px">Three</p> 
    </div> 
</div> 

你看到div#div-b在顶部时,它在div.container元素溢出切断。

回答

3

不幸的是,如果不在容器元素之外引入对话,你想做的事情是不可能的。

最好的办法是让对话框元素成为容器的同级并定位它。

1

不幸的是......我不认为有一种方法来规避溢出:隐藏着绝对的位置。您可以尝试使用position:fixed,但如果您使用它,则不会在完全相同的条件下进行定位。

1

一个选择是将你的overflow:hidden容器的内容放到一个子容器(也许是一个子div)中。然后,使子容器与容器的尺寸匹配,并将溢出:从容器隐藏到子容器。

然后,您可以使对话框成为容器的子对象(子容器的同级),它现在将存在于没有溢出的元素中:hidden。

我还没有测试过这一点,并删除溢出:从容器隐藏可能会打破你的设计。如果是这样的话,我会建议像其他人那样做,并将对话框完全移出容器。如果你没有把对话框的代码放到其他地方的话,甚至可以通过Javascript来完成。 (当你需要显示时,Javascript可以使对话框成为BODY的子项或其他标签)