开发者

How to have html table's height = height of parent div when div's height is not set

I have an html table, inside of a div. The div's height is based on other elements in the div.

How can 开发者_如何学运维I have the table's height be equal to its parent div's height?

(table height: 100% won't work because the div's height is not set)

Update: I need to support IE6,7,8 so the css recommendations should be understood by these browsers.


Nearly impossible, because the renderer cannot know how much 100% should be.

A really dirty workaround: Add display:table to the div and display:table-row-group to the table. Don’t expect too much … :)


Try to use position:relative and min-height:100%;

Mb even posinion:absolute, top:xxx, bottom:xxx, right:xxx and left:xxx - positing and height:auto.


To achieve what you are talking about with just css might be quite difficult. You could try using javascript, jQuery or any host of other javascript libraries to grab the height of the parent div and then apply that as the height to the table. Check out the below example. No matter how much text is added it sets the height of the table dynamically based on the parent div. It isn't unobtrusive javascript - but this should get you started.

<html>
    <head>
        <title>Table Height set via jQuery</title>
        <script src="jquery.js" type="text/javascript" charset="utf-8"></script>

        <script type="text/javascript" charset="utf-8">
            $(document).ready(function(){
                var divHeight = $('div.full').height();
                $('table.column_2').css("height", divHeight + "px");
                $('table.column_2 .parentDiv').text(divHeight);
                var tableHeight = $('table.column_2').height();
                $('table.column_2 .tableHeight').text(tableHeight);
            });
        </script>
        <style type="text/css" media="screen">
            div.full {
                width:960px;
                float:left;
                }
            div.column_1 {
                width:450px;
                padding:0 10px;
                float:left;
                margin-right:10px;
                background-color:#eee;
            }
            table.column_2 {
                width:470px;
                float:left;
                background-color:#ddd;
            }
        </style>
    </head>

    <body>
        <div class="full">
            <div class="column_1">
                <p>
                    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore
                    magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
                    consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla 
                    pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim 
                    id est laborum.

                </p>

            </div>
            <table class="column_2">
                <thead>
                    <tr>
                        <th>Table Height</th>
                        <th>Parent Div Height</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td class="tableHeight"></td>
                        <td class="parentDiv"></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </body>
</html>


Bad news, you can't. There are so many hacks for them and none of them are good solutions. Javascript as described is the only way to do it but it takes too much bandwidth over little things but useful IF you plan to use jQuery for other functions.

CSS 3 has new rules that let you have that flexibility but many browsers don't offer full CSS 3 support. Since you wanted IE6/7, you're out of luck.

If it's REALLY that important, it's OK to use minimal tables. Don't let others tell you that it's wrong when it works for virtually all desktop browsers and even mobile browsers work just fine as long as they are not nested no more than two levels deep.

Another solution is to create layout designs that don't depend on checking other DIV's heights.


A table height of 100% should fill whatever space it has available in its parent div container. If you can't set the height on the parent div, try using height="100%" on the table rather than CSS. I know, it's not semantically correct but tables work a bit quirky with divs sometimes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜