html – 显示“表格单元格”的元素将不会像表格单元格填充可用空间

前端之家收集整理的这篇文章主要介绍了html – 显示“表格单元格”的元素将不会像表格单元格填充可用空间前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
更新APRL 30:我已经决定反对 javascript解决方案.确保它始终工作的唯一方法是将它放在setInterval()中每隔几秒钟.不想这样做我知道这个CSS是可行的,我已经看到它的工作.如果结束,我会重新打开赏金更多150.

所以我有这个小模态弹出窗口.它由2个部分组成;左和右.在两部分中都有上面的标签和下面的内容.标签固定在一定数量的像素,但是底部区域需要能够填充剩余的空间,所以我使用左侧和右侧的display:table和内部的table-cell来实现这个“填补剩余空间”效果.它在Chrome和Safari中效果很好:

http://hashtraffic.com(点击“尝试”,然后点击其中一个主题标签)

这是CSS魔术:

  1. #tagBoxLeft,#tagBoxRight {
  2. display: table;
  3. height: 100%;
  4. width: 50%;
  5. position: absolute;
  6. right: 0;
  7. opacity: 0;
  8. }
  9. #tagBoxLeft { left: 0 }
  10. #tagBoxDescription {
  11. display: table-row;
  12. -webkit-border-top-left-radius: 20px;
  13. width: 100%;
  14. word-break: break-all;
  15. word-wrap: break-word;
  16. -webkit-Box-shadow: 0 1px 0 #FFF;
  17. -moz-Box-shadow: 0 1px 0 #FFF;
  18. Box-shadow: 0 1px 0 #FFF;
  19. }
  20. .nano {
  21. position: relative;
  22. width: 100%;
  23. height: 100%;
  24. overflow: hidden;
  25. display: table-cell;
  26. }
  27. #taglabel {
  28. display: table-row;
  29. z-index: 10000;
  30. border-top: 1px solid #FFF;
  31. width: 100%;
  32. height: 39px;
  33. }

它只是使一堆div成一个表,以便它们可以相对于彼此的高度.还要注意左侧和右侧是相对于浏览器窗口,所以这就是为什么我不能只使用百分比.

问题是在Firefox和歌剧,#tagBoxLeft和#tagBoxRight两个部分拒绝接受高度:100%;而他们有显示:表.那么那么它不会强制反应性的底部.我有一个jsfiddle演示,可以在Firefox和Opera,所以我知道这是可能的:http://jsfiddle.net/Qxswa/但是为什么我的所有内容溢出在Firefox和Opera?

这是一个问题的屏幕截图:
http://cl.ly/G0iP

谢谢!

解决方法

这是使用display:table和friends的替代方法,它使用绝对定位元素的忽略能力来设置它们的顶部和底部(和左右)值.它基本上“粘住”顶部和底部边缘,给您相对于容器的高度,但没有明确设置高度.

UDPATED:正如杰克逊提到的,此代码的CSS版本不提供列中的自动高度固定面板.一个简单的JS将修复这一点 – 您只需要为没有JS的用户设置一个明智的默认高度. JS只需要在加载模态时运行,而不是间隔.

这是更新的小提琴:http://jsfiddle.net/cxY7D/5

这里是简化的HTML:

  1. <div id="modal">
  2. <div class="left">
  3. <div class="description">
  4. <h1>#tag_name</h1>
  5. <dl>
  6. <dt>Tags</dt> <dd>27</dd>
  7. </dl>
  8. </div>
  9. <div class="contents">
  10. <div class="header">
  11. <h2>Featured</h2>
  12. </div>
  13. <ol>
  14. <li>Something Something</li>
  15. <li>...</li>
  16. </ol>
  17. </div>
  18. </div>
  19. <div class="right">
  20. <div class="contents">
  21. <div class="header">
  22. <h2>Recent</h2>
  23. </div>
  24. <ol>
  25. <li>Something Something</li>
  26. <li>...</li>
  27. </ol>
  28. </div>
  29. </div>
  30. </div>

和CSS:

  1. body {
  2. background:#444;
  3. }
  4. #modal {
  5. background:#FFF;
  6. position: absolute;
  7. top: 4em;
  8. bottom: 4em;
  9. left: 6em;
  10. right: 6em;
  11. }
  12.  
  13. #modal .left,#modal .right {
  14. position:absolute;
  15. top: 0;
  16. bottom: 0;
  17. }
  18.  
  19. #modal .left {
  20. background:#ACF9E4;
  21. left: 0;
  22. right:50%;
  23. }
  24.  
  25. #modal .right {
  26. background:#FCFFCD;
  27. right: 0;
  28. left:50%;
  29. }
  30.  
  31. #modal .contents {
  32. position:absolute;
  33. top: 0;
  34. bottom: 0;
  35. width: 100%;
  36. overflow-y:auto;
  37. }
  38.  
  39. #modal .description {
  40. height: 8em;
  41. }
  42.  
  43. #modal .description + .contents {
  44. top: 10em;
  45. }
  46.  
  47. #modal .header,#modal .description,.contents li {
  48. border-bottom:1px solid #CCC;
  49. padding: 1em;
  50. }
  51.  
  52. #modal .description dt {
  53. float: left;
  54. padding-right: 1em;
  55. }

这是一个非常有用和可靠的技术.当你提到“绝对位置”时,很多人会得到颤抖,但是像这样使用,这真的是解放了!

JS(假设是jQuery)

  1. $(function(){
  2. $('#modal').on('display',function(){
  3. //Calculate the height of the top left panel,and provide the remaining space to the bottom left
  4. var leftColumn = $(this).find('.left'),descriptionHeight = leftColumn.find('.description').height('auto').outerHeight(); //Set the height to auto,then read it
  5.  
  6. leftColumn.find('.contents').css('top',descriptionHeight)//Apply the height to the scrolling contents pane
  7. });
  8.  
  9. $('#modal').trigger('display');
  10. });​

JS将左上角的窗格重置为自动高度,然后读取高度,并将其应用于左下方面板的顶部坐标.它被应用为自定义事件,因此您可以将其作为模态显示代码的一部分进行触发.

这是我给出的一个答案,使用类似的技术,以及更多关于hows和whys的解释:The Impossible Layout?.检查一个列表分开的文章,以进行更多的讨论,以及一些简单的修复,使其在IE6中工作(如果你关心的话).

猜你在找的HTML相关文章