简单的100%CSS高度示例不起作用,请有人帮忙



我有一个非常简单的CSS 100%示例,根据我的理解,从逻辑上讲,它应该可以工作,但不能。有人能解释一下原因吗?

HTML:

<div id="header">header</div>
<div id="nav">Nav</div>
<div id="title">title</div>
<div id="content">
    Content
</div>

CSS:

html {
    height:100%;
    padding: 0;
    margin: 0;
}
body {
    height: 100%;
    padding: 0;
    margin: 0;
}
#header {
    background-color:red;
    padding: 0;
    margin: 0;
}
#nav {
    background-color:gray;
    padding: 0;
    margin: 0;
}
#title {
    background-color:azure;
    padding: 0;
    margin: 0;
}
#content {
    background-color:antiquewhite;
    height:100%;
    padding: 0;
    margin: 0;
}

据我所知,不应该有垂直滚动条。然而一个出现了。

下面是一把小提琴:http://jsfiddle.net/codeowl/9wABW/

感谢您抽出时间,

问候,

Scott

更新:

以下是我最终所做的:

我开发了一种堆叠和填充方法,如下所示。不幸的是,fiddle在我尝试用java脚本访问窗口时遇到了问题,所以我只能粘贴代码:

CSS:

#header {
    background-color:red;
}
#nav {
    background-color:gray;
}
#title {
    background-color:azure;
}
#content {
    background:green;
}

HTML:

<div id="header" class="stack-y">header</div>
<div id="nav" class="stack-y">Nav</div>
<div id="title" class="stack-y">title</div>
<div id="content" class="fill-y">
    <div data-role="splitter"
        data-panes="[
        { scrollable: false, collapsible: true, size: '300px' },
        { scrollable: false, collapsible: true }
        ]" 
        class="fill-y">
        <div>
            Left Pane
        </div>
        <div>
            Right Pane
        </div>
    </div>
    <div class="stack-y">Test Content</div>
</div>

Java脚本:

$(document).ready(function () {
    var fResizeLayout = null;
    fResizeLayout = function() {
        var aFillElements = $('.fill-y');
        $.each(aFillElements, function (i, e) {
            var p = null,
                iPY = 0,
                iY = 0,
                iH = 0;
            e = $(e);
            p = e.parent();
            if (p.prop('tagName') === 'body') { iPY = $(window).height(); }
            else { iPY = p.innerHeight(); }
            e.siblings('.stack-y').each(function () {
                iY += $(this).outerHeight(true);
            });
            iH = (iPY - iY - parseInt(e.css('border-top-width'), 10) - parseInt(e.css('border-bottom-width'), 10));
            e.height(iH);
        });
        kendo.resize($('#content'));
    };
    kendo.init($('#content'));
    fResizeLayout();
    $(window).on('resize', function () {
        if (this.resizeTO) clearTimeout(this.resizeTO);
        this.resizeTO = setTimeout(function () {
            $(this).trigger('resizeEnd');
        }, 200);
    });
    $(window).on('resizeEnd', function () {
        fResizeLayout();
    });            
});

当然,为了让剑道部分发挥作用,你需要包括剑道库。

<link href="http://cdn.kendostatic.com/2013.3.1119/styles/kendo.common.min.css" rel="stylesheet" type="text/css" />
<link href="http://cdn.kendostatic.com/2013.3.1119/styles/kendo.default.min.css" rel="stylesheet" type="text/css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://cdn.kendostatic.com/2013.3.1119/js/kendo.all.min.js"></script>

没有keno库:

HTML:

<div id="header" class="stack-y">header</div>
<div id="nav" class="stack-y">Nav</div>
<div id="title" class="stack-y">title</div>
<div id="content" class="fill-y">
    Test Fill Content
</div>
<div class="stack-y">Test Stacked Content</div>

Java脚本:

$(document).ready(function () {
    var fResizeLayout = null;
    fResizeLayout = function() {
        var aFillElements = $('.fill-y');
        $.each(aFillElements, function (i, e) {
            var p = null,
                iPY = 0,
                iY = 0,
                iH = 0;
            e = $(e);
            p = e.parent();
            if (p.prop('tagName') === 'body') { iPY = $(window).height(); }
            else { iPY = p.innerHeight(); }
            e.siblings('.stack-y').each(function () {
                iY += $(this).outerHeight(true);
            });
            iH = (iPY - iY - parseInt(e.css('border-top-width'), 10) - parseInt(e.css('border-bottom-width'), 10));
            e.height(iH);
        });
    };
    fResizeLayout();
    $(window).on('resize', function () {
        if (this.resizeTO) clearTimeout(this.resizeTO);
        this.resizeTO = setTimeout(function () {
            $(this).trigger('resizeEnd');
        }, 200);
    });
    $(window).on('resizeEnd', function () {
        fResizeLayout();
    });            
});

感谢Carlos的调整结束部分:https://stackoverflow.com/a/12692647/2109254

感谢所有做出贡献的人。

希望这能帮助其他人。

问候,

Scott

编辑

这将使用包装内容中的display:table行给出您正在查找的布局。

html {
    height:100%;
    padding: 0;
    margin: 0;
}
body {
    height: 100%;
    padding: 0;
    margin: 0;
}
#header {
    background-color:red;
    padding: 0;
    margin: 0;
    display: table-row;
    height:1px;
}
#nav {
    background-color:gray;
    padding: 0;
    margin: 0;
    display:table-row;
    height:1px;
}
#title {
    background-color:azure;
    padding: 0;
    margin: 0;
    display:table-row;
    height:1px;
}
#content {
    background:green;
    padding: 0;
    margin: 0;
    display:table-row;
}
#wrapper {height:100%;width:100%;margin:0;padding:0;display:table}

<div id="wrapper">    
    <div id="header">header</div>
    <div id="nav">Nav</div>
    <div id="title">title</div>
    <div id="content">
        Content
    </div>
</div>

检查您更新的小提琴

您需要元素的包装器,这取决于您可能想要使用表css属性(显示:表行等)的特定布局。

<div id="wrapper">    
    <div id="header">header</div>
    <div id="nav">Nav</div>
    <div id="title">title</div>
    <div id="content">
        Content
    </div>
</div>

html {
    height:100%;
    padding: 0;
    margin: 0;
}
body {
    height: 100%;
    padding: 0;
    margin: 0;
}
#header {
    background-color:red;
    padding: 0;
    margin: 0;
}
#nav {
    background-color:gray;
    padding: 0;
    margin: 0;
}
#title {
    background-color:azure;
    padding: 0;
    margin: 0;
}
#content {
    padding: 0;
    margin: 0;
}
#wrapper {height:100%;margin:0;padding:0;background-color:antiquewhite;}

不使用height:100%使用height:auto。。它会起作用…:)

想想你在做什么。你告诉content是它包含元素的100%。这将是body

因此,content将占据窗口的大小,但您仍有其他三个具有高度的div,因此总内容大小将=100%(正文大小)+标题+导航+标题

如果你想解决这个问题,你可以简单地将内部内容添加到100%,并根据你的需要调整百分比。看看:

http://jsfiddle.net/9wABW/3/

最新更新