【问题标题】:Make the BODY DIV Fill the Available Area使 BODY DIV 填充可用区域
【发布时间】:2026-01-01 06:35:01
【问题描述】:

我正在开发一个全新的网站,我正在尝试让基本布局正常运行。我正在使用 ASP.NET MVC 4 生成的 HTML,我想在为 header 腾出空间后让名为 bodyDIV 填充可用空间,从而将 footer 锚定到底部浏览器窗口。然而,我现在得到的是三个面板,它们只是堆叠在一起。

如果浏览器支持 HTML5,我想要一个可行的解决方案,如果不支持,我想要一个解决方案

请注意,我已在 CSS 中内联 cmets 以尝试解释我的尝试。

HTML

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>@ViewBag.Title - Title</title>
        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <meta name="viewport" content="width=device-width" />
        @Styles.Render("~/Content/css")
    </head>
    <body>
        <header>
            <div class="content-wrapper">
                <div class="float-left">
                    <p class="site-title">@Html.ActionLink("Title", "Index", "Home")</p>
                </div>
            </div>
        </header>
        <div id="body">
            @RenderSection("featured", required: false)
            <section class="content-wrapper main-content clear-fix">
                @RenderBody()
            </section>
        </div>
        <footer>
            <div class="content-wrapper">
                <div class="float-left">
                    <p>&copy; @DateTime.Now.Year - ACME. All rights reserved.</p>
                </div>
                <div class="float-right">
                    <ul id="social">
                        <li><a href="http://facebook.com" class="facebook">Facebook</a></li>
                        <li><a href="http://twitter.com" class="twitter">Twitter</a></li>
                    </ul>
                </div>
            </div>
        </footer>

        @RenderSection("scripts", required: false)
    </body>
</html>

CSS

body {
    /* I'VE TRIED BOTH OF THE FOLLOWING TO SEE IF THE BODY ITSELF WOULD SPAN */
    /* WITH NO OTHER CSS APPLIED TO THE body ELEMENT */

    /*height: fill-available;*/
    /*height: 100%*/
}

/* general layout
----------------------------------------------------------*/
.float-left {
    float: left;
}

.float-right {
    float: right;
}

.clear-fix:after {
    content: ".";
    clear: both;
    display: block;
    height: 0;
    visibility: hidden;
}

/* main layout
----------------------------------------------------------*/
.content-wrapper {
    margin: 0 auto;
    max-width: 960px;
}

#body {
    background-color: #efeeef;
    clear: both;
    padding-bottom: 35px;

    /* I'VE TRIED BOTH OF THE FOLLOWING TO SEE IF I COULD GET THIS ELEMENT TO SPAN */
    /* WITHOUT ANY OTHER CSS APPLIED TO THE body TAG */

    /*height: fill-available;*/
    /*height: 100%*/
}

.main-content {
    /*background: url("../Images/accent.png") no-repeat;*/
    padding-left: 10px;
    padding-top: 30px;
}

.featured + .main-content {
    /*background: url("../Images/heroAccent.png") no-repeat;*/
}

footer {
    clear: both;
    background-color: #e2e2e2;
    font-size: .8em;
    height: 100px;
}

/* site title
----------------------------------------------------------*/
.site-title {
    color: #c8c8c8;
    font-family: Rockwell, Consolas, "Courier New", Courier, monospace;
    font-size: 2.3em;
    margin: 20px 0;
}

    .site-title a, .site-title a:hover, .site-title a:active {
        background: none;
        color: #c8c8c8;
        outline: none;
        text-decoration: none;
    }

/* social
----------------------------------------------------------*/
ul#social li {
    display: inline;
    list-style: none;
}

    ul#social li a {
        color: #999;
        text-decoration: none;
    }

a.facebook, a.twitter {
    display: block;
    float: left;
    height: 24px;
    padding-left: 17px;
    text-indent: -9999px;
    width: 16px;
}

a.facebook {
    background: url("../Images/facebook.png") no-repeat;
}

a.twitter {
    background: url("../Images/twitter.png") no-repeat;
}

【问题讨论】:

  • 你不应该有一个
    因为你已经有一个
  • 拥有一个 ID 为“body”的 DIV 并没有错,只要您不将其与页面的实际“body”标签混淆即可。关于您的问题,您可以使用 javascript 计算用户屏幕的高度,然后从中删除 HEADER 和 FOOTER 的高度。
  • @BillyMoat - JavaScript 是唯一的方法吗?我的意思是我可以走那条路,但我真的不想(在这里插入悲伤的脸)。
  • @Mike - 是的,我认为这可能是做你想做的事情的唯一方法,我害怕。我可能错了,但我不这么认为。
  • @sharethis - 我可以在几天内尝试答案。感谢您的支持。

标签: css html asp.net-mvc-4


【解决方案1】:

只需使用fixed 定位将页眉和页脚捕捉到页面底部即可。

header, footer{ position:fixed; left:0; right:0; z-index:1; }
header{ top:0; }
footer{ bottom:0; }

然后你可以给你的body 提供你之前的div#body 的背景。 div 没有背景,会根据需要展开。

div#body{ background:none; }
body{ background:#eee; }

这看起来就像div 会填满页面的剩余空间。最后给你的headerfooter一个background,这样你就看不到它下面的body背景了。

header, footer{ background:#fff; }

顺便说一句,我建议删除正文边缘。 body{ margin:0; }

【讨论】:

    【解决方案2】:

    我相信仅使用 CSS 是不可能做到这一点的。您可以像这样制作一个 100% 高度的网页:

    html{
        height: 100%;
    }
    
    body{
        height: 100%;
    }
    
    #body{
        height: 100%;
    }
    

    然后对于页眉、正文和页脚,您可以这样做:

    header{
      height: 100px;
      left: 0;
      position: absolute;
      top: 0;
      width: 100%;
      background-color: #f00;
    }
    
    #body{
      bottom: 100px;
      left: 0;
      position: absolute;
      right: 0;
      top: 100px;
      background-color: #fff;
    }
    
    footer{
      bottom: 0;
      height: 100px;
      left: 0;
      position: absolute;
      width: 100%;
      background-color: #ff0;
    }
    

    它可能会工作一段时间,但它会在某个时候中断。当您调整浏览器大小时,#body 的空间将不足。如果你想要一个更好的解决方案,你应该使用 javascript。在您的 javascript 中,计算您的#body 有多少空间,然后调整headerfooter 的高度。或者改为调整#body

    【讨论】:

    • 你不能给页脚和页眉指定一个高度吗?
    • 我相信你可以。只需将那些以像素为单位的测量值更改为百分比即可。
    • 是的,我现在意识到了。但我不确定使用百分比的方法是否最好。它可能适用于某些浏览器,但可能不是全部。如果是我,我很可能会选择 jQuery。你觉得呢?
    • 我认为 jQuery 是一个 JS 框架,更常见的是用户禁用 JS 而不是使用不支持百分比的古老浏览器。或者只使用我不使用高度规格的答案。当然,在非常小的分辨率下,主要区域可能会消失,但如果不提供负责任的标记,从逻辑上讲你无法解决这个问题。如果您需要支持非常小的分辨率,这是我对您的建议,迈克。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签