【问题标题】:DIV stretch to height 100% in a table cellDIV 在表格单元格中拉伸到 100% 的高度
【发布时间】:2012-12-10 19:28:36
【问题描述】:

我有以下布局:

4 个圆角背景和两个面板(左面板和右面板)。

目前我实现的布局如下:

包含 9 个单元格的表格:

左上角 | |右上角 |左格右格| 左下角| |右下角

代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
    <table id="content" width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
            <td>
                <img src="corner1.jpg" /></td>
            <td>
            </td>
            <td>
                <img src="corner2.jpg" /></td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td valign="top" height="100%">
                <div id="menu" style="float: left; width: 235px; height: 445px; background-color: #660000">
                    Menu
                </div>
                <div id="mainContent" style="float: left; margin-left: 10px; height: 100%; background-color: #888888">
                    Main Content
                </div>
            </td>
            <td>
            </td>
        </tr>
        <tr>
            <td>
                <img src="corner3.jpg" /></td>
            <td>
            </td>
            <td>
                <img src="corner4.jpg" /></td>
        </tr>
    </table>
</body>
</html>

我想问的是如何将右div的高度拉伸到100%,使其等于td的高度(左div的高度会根据用户的增加而增加)行动)。

我尝试了很多方法,仍然无法弄清楚。将 div 的高度设置为 100% 不起作用。

我应该怎么做才能实现这一目标?不使用表格?

PS:

代码已更改。您可以将代码粘贴到记事本中并在 IE 中进行测试。

再次编辑代码以添加 DOCTYPE

【问题讨论】:

    标签: html css


    【解决方案1】:

    鉴于您已经有了用于布局的表格,我真的不会费心尝试以 “正确” 的方式进行操作。

    只需使用另一张桌子。
    CSS

        <style type="text/css" media="screen">
            #menu {
                width:235px;
                height:445px;
                background-color:#660000
    
            }
            #mainContent {
    
                height:100%;
                width:auto;
                background-color:#888888
            }
            #container {
                width:100%;
            }
        </style>
    

    HTML

    <table id="content" width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
        <td><img src="corner1.jpg"/></td>
        <td>&nbsp;</td>
        <td><img src="corner2.jpg"/></td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td valign="top" style="padding:0px;">
            <table id="container" border="0" cellspacing="0" cellpadding="0">
    
                <tr>
                    <td id="menu"><div>Menu</div></td>
                    <td style="width:10px"></td>
                    <td id="mainContent"><div>Main Content</div></td>
    
                </tr>
            </table>
        </td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td><img src="corner3.jpg"/></td>
        <td>&nbsp;</td>
        <td><img src="corner4.jpg"/></td>
    </tr>
    </table>
    

    【讨论】:

    • 假设因为使用了一张表,所以事情正在以“错误的方式”完成,这是愚蠢的。如果我碰巧正在显示表格数据,并且我希望在每一行中都有一个单元格,其中包含一个我想要将其高度拉伸到单元格的完整高度的元素?
    • @Jez 问题是关于已经使用表格的布局,而不是关于显示表格数据。我还引用了“正确”,因为有时这实际上并不是最好的方法。
    • 此外,Jez,问题中的代码显然不是最优的。在说别人白痴之前,你应该更加小心。
    【解决方案2】:

    也许你可以在右边的 div 中添加一个 min-height 等于左边的高度值......它也许可以达到你想要的效果......

    【讨论】:

      【解决方案3】:

      您可以将 div 的位置设置为绝对位置,但当然您必须绝对定位它。

      【讨论】:

      • 这对我有用。我在父容器上设置了 position:relative 值,在我想要垂直拉伸 100% 的那个上设置了 position:absolute。感谢您的帮助!
      【解决方案4】:

      或者只使用 span 怎么样?

      例如。

      <table width="100%">
          <tr>
              <td style="height:540px; width:100%; background-color:green;">
                 my name is earl!
              </td>
          </tr>
      
          <tr>
              <td style="height:540px; width:100%; background-color:yellow;">
                  <span style="height:100%; display:inline-block; width: 100%; background-color:blue;">
                      blinkers on!
                  </span>
              </td>
          </tr>
      </table>
      

      不要太担心表格元素中的100%,而是看跨度..
      跨度:

              <span style="height:100%; display:inline-block; width: 100%; background-color:blue;">
                  blinkers on!
              </span>
      

      看到'display:inline-block'了吗?现在这是重要的部分..
      我希望这会有所帮助..

      【讨论】:

        【解决方案5】:

        我已经使用 jquery 和 animate 调用来设置 div 的高度,但您可以将动画速度设置为 0。

        下面的示例是一些用于快速玩转 2、3、5... 块设计的模型。 Jquery animate 调用用于调整主要内容、侧边栏等的 div 的高度......但这可能有点超出您的要求。

        <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Mock_2_ColGoldenRatio.aspx.cs" Inherits="ia._HtmlMocks.Mock_2_ColGoldenRatio" %>
        
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        
        <html xmlns="http://www.w3.org/1999/xhtml" >
        <head runat="server">
            <title></title>
        
            <style type="text/css" >
        
        
        /*normalise*/
        html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, font, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td {
        margin:0;
        padding:0;
        font-size:100%;
        font-style:normal ;
        font-weight:normal;
        border-collapse:collapse ;
        border-spacing:0;
        border:0;
        border-width: 0px ;
        text-align:left;
        }
        q:before,q:after
        {
            content:'';
        }
        
        /*center everything using html container*/
        html{
        text-align: center ; 
        }
        
        /* wash with olive ; make body width max 976 px ; no margin ; relative position so other elements within */
        body
        {
        background-color:Olive; 
        width:976px; 
        margin:0 auto ; 
        text-align: left ; 
        position: relative  ;
        }
        
        #HeaderEnc
        {
        background-color : Maroon ; 
        }
        
        #SearchEnc{
        background-color: red ;
        border-width: 0px ;
        }
        
        #MainBlogNav{
        background-color:Yellow  ;
        }
        
        /* postion relative to normal flow */
        #ContentEnc{
        background-color: Teal  ;  
        position: relative  ;
        }
        
        
        
            #ContentSideBarA
            {
            position: absolute ; 
            background-color: #AAA ; 
            left: 62% ; 
            width: 38% ; 
            }
        
            #ContentMainEnc{
            position: absolute ; 
            background-color: #888; 
            left: 0% ; 
            width: 62% ; 
            }
        
            #ContentSideBarB{
            position:absolute ; 
            background-color : #AAA ; 
            left: 0% ; 
            width: 0px ; 
            }
        
            #FooterEnc{
            position: absolute ; 
            background-color: blue ; 
            left: 0 ; 
            width: 100% ; 
            }
        </style>
        </head>
        <body>
            <div id="HeaderEnc">
            The quick brown fo jumped over the lazy dog. 
            <br />
            HeaderEnc
                <div id="SearchEnc">SearchEnc</div>        
                <div id="MainBlogNav">MainBlogNav</div>        
            </div> 
        
            <div id="ContentEnc">
                <div id="ContentMainEnc"><i>ContentMainEnc</i> : 
                    <% Response.Write( GetContentBig()) ; %>
                </div> 
        
                <div id="ContentSideBarA"><i>ContentSideBarA</i> 
                    <% Response.Write( GetContentSmall()) ; %>
                </div> 
        
                <div id="ContentSideBarB"><i>ContentSideBarB</i>
                    <% /*Response.Write( GetContentMedium()) ;*/ %>
                </div>
        
                <i>ContentEnc-Start</i>
                <br />
                <br />
                <br />
                <br />
                <i>ContentEnc-Start</i>
            </div>
            <div id="FooterEnc" >FooterEnc</div> 
        
        </body>
        </html>
        
        
        <script src="../../Scripts/jquery-1.3/jquery-1.3.1.js" type="text/javascript"></script>
        <script  src="../../Scripts/plugins/dimensions_1.2/jquery.dimensions.js" type="text/javascript" ></script>
        <!--<script  src="../../Scripts/Common/IaCommon.js" type="text/javascript" ></script>-->
        
        <script  type="text/javascript" >
        
            /*
            * ia.common 0.1.0 
            *
            */
        
            var ia = {}
            ia: "0.1.0";
        
            // gets max settings from set of elements
            var getBoundaries = function(elements) {
                var maxHeight = 0;
                var maxTop = 0;
                var itemSelector = '';
                var itemHeight = 0;
                var itemPosition = {};
                var itemTop = 0;
        
                for (i = 0; i < elements.length; i++) {
                    itemSelector = elements[i]
                    itemHeight = $(itemSelector).height();
                    itemPosition = $(itemSelector).position();
                    itemTop = itemPosition.top;
                    maxHeight = itemHeight > maxHeight ? itemHeight : maxHeight;
                    maxTop = itemTop > maxTop ? itemTop : maxTop;
                }
                return { 'maxHeight': maxHeight, 'maxTop': maxTop };
            }
        
            // Rets truthy  ; used by property get/set public fns
            var SetIfOk = function(val) { return val; }
        
            // IaPage class
            var IaPage = function(objSpec) {
                // override default page object with defaults from caller
                var that = $.extend({
                    'speedFooterAnimate': 3000
                    , 'speedContentAnimate': 1000
                }, objSpec || {});
        
                // fn to move footer, ... to correct position : inmann clearing
                that.arrangePageStructure = function() {
                    boundaries = getBoundaries(this.ContentElements);
                    $(this.FooterEncSelector[0]).animate({ 'top': boundaries.maxTop + boundaries.maxHeight }, this.speedContentAnimate);
                    $(this.ContentEncSelector[0]).animate({ 'height': boundaries.maxHeight }, this.speedFooterAnimate);
                };
                // Ret
                return that;
            }
        
        
            var iaPage = IaPage(
            {
                'ContentEncSelector': ['#ContentEnc']
                , 'FooterEncSelector': ['#FooterEnc']
                , 'ContentElements': ['#ContentEnc', '#ContentSideBarA', '#ContentMainEnc', '#ContentSideBarB']
            }
            );
        
            $(function() {
                iaPage.arrangePageStructure();
            }
            );
        
        </script>
        

        【讨论】:

          猜你喜欢
          • 2011-08-25
          • 2016-11-09
          • 2012-09-10
          • 2012-04-14
          • 1970-01-01
          • 2013-10-01
          • 2016-12-14
          • 2017-12-24
          • 1970-01-01
          相关资源
          最近更新 更多