【问题标题】:100% Not Working100% 不工作
【发布时间】:2013-12-13 07:07:29
【问题描述】:

我遇到了 100% 身高的老问题。我知道这个问题被问了很多,我已经查看了thisthisthis 以及无数其他问题。我想创建一个基本的固定标题、侧边导航和主要文章区域,如下所示:

但是,由于某种原因,它看起来像下面这样(我在蓝色条中放置了 200 像素的内边距,只是为了让它出现)。

我的 HTML 如下所示:

<!DOCTYPE html>
<html>
<head></head> 
<body>

<header></header>

<section>

<nav></nav>

<article></article>

</section>

</body>
</html>

我的 CSS 看起来像这样:

* { -moz-box-sizing: border-box; background-color: transparent; border: 0 none; color: #000000; list-style: none outside none; margin: 0; outline: medium none; padding: 0; text-decoration: none; }

body, html { height: 100%; } 

header {
  background: #6c6363;
  top: 0;
  z-index: 100;
  height: 100px;
  left: 0;
  position: fixed;
  right: 0;
}

section {
  min-height: 100%;
  overflow: auto;
  position: relative;
  padding-top: 100px;
}

nav {
  background-color: #747feb;
  float: left;
  min-height: 100%;
  padding-bottom: 200px;
  width: 150px;
}

article {
  background: #74eb8a;
  margin: 20px 20px 20px 170px;
  min-height: 100%;
  padding: 20px;
  position: relative;
}

如您所见,没有什么特别的。我知道section 需要 100% 的高度,bodyhtml 也是如此。我可以绝对定位navacticle,然后做这样的事情:

但是,在我的实际站点中(为此我对其进行了简化),侧边导航有下拉菜单,它会动态改变导航高度。这会导致以下情况发生:

绝对定位的元素不会改变相对包装的高度,所以我需要浮动它们。但是,浮动它们不会使高度变为 100%。

我什至制作了一个 JSFiddle 来显示问题:http://jsfiddle.net/g8VjP/

如果有人可以帮助我,我将不胜感激。

谢谢!

PS:如果它有效,我完全赞成使用 calc()!

解决方案

我修改了 Mayank 的答案并设法提出了解决方案。我不得不添加几个包装器,但它起作用了。我的 HTML 现在如下所示:

<!DOCTYPE html>
<html>
    <head></head> 
<body>

<header></header>

<section>

<nav></nav>

<div class="cell-wrap">
    <div class="table-wrap">
        <article></article>
    </div>
</div>

</section>

</body>

</html>

密钥是cell-wraptable-wrap。我有nav 是一个表格单元格,.cell-wrap 是另一个。 nav 有一个固定的,.cell-wrap 填充其余部分。但是,我想在article 周围留出间距,所以我添加了.table-cell 并将其制成表格。然后扩展并填充.cell-wrap 的高度和宽度。然后我添加 30px 填充以在 article 周围留出空间(因为边距不适用于表格单元格)并将 article 设为表格单元格。

有点混乱,但它有效!

我的 CSS 如下:

* { -moz-box-sizing: border-box; background-color: transparent; border: 0 none; color: #000000; list-style: none outside none; margin: 0; outline: medium none; padding: 0; text-decoration: none; }

body, html { height: 100%; } 

header {
  background: #6c6363;
  top: 0;
  z-index: 100;
  height: 100px;
  left: 0;
  position: fixed;
  right: 0;
}

section {
  display: table;
  height: 100%;
  min-height: 100%;
  padding-top: 100px;
  position: relative;
  width: 100%;
}

nav {
  background-color: #657182;
  display: table-cell;
  min-height: 100%;
  width: 150px;
}

.cell-wrap {
  display: table-cell;
  height: 100%;
  min-height: 100%;
}

.table-wrap {
  display: table;
  height: 100%;
  padding: 30px;
  width: 100%;
}

article {
  background: none repeat scroll 0 0 #FFFFFF;
  display: table-cell;
  min-height: 100%;
  padding: 20px 20px 120px;
  z-index: 1;
}

Here's the fiddle。不知道为什么底部有一个滚动条,但是如果您在浏览器中正常显示它似乎很好。

【问题讨论】:

标签: html css


【解决方案1】:

height: 100% 表示包含块的 height 的 100%。您的包含块section 没有定义的height(而是min-height)。您可以:

  1. section 上的min-height: 100% 更改为height: 100%。或者...
  2. 保留 min-height: 100% 并添加 height: 1px(或任何小于 100% 的值)将被 min-height 覆盖。

这里的关键是在父级上设置height 属性。

【讨论】:

    【解决方案2】:

    display:tabledisplay:tabel-cell 是这里的朋友吗?
    将您的小提琴更新为轻微的解决方法,然后就可以了: DEMO

    要修改的CSS:

    section {
        min-height: 100%;
        overflow: auto;
        position: relative;
        padding-top: 100px;
        display:table;/* addition */
    }
    article {
        background: #74eb8a;
        margin: 0px 20px 0px 170px;
        min-height: 100%;
        width:100%;
        height: 100%;
        position: relative;
        display:table-cell; /* addition */
     }
    

    此外,我还冒昧地删除了您放置在 article 中的额外 padding ,在 article 中插入 divsection 并为其分配填充(如果它有效)!!

    【讨论】:

    • 这并不完全适合我,但它确实引导我朝着正确的方向解决我的问题。我从来没有想过使用表格和表格单元格。对我来说,这似乎太hacky了!但它奏效了!请参阅问题以获取解决方案。再次感谢!
    【解决方案3】:

    试试这个:

    nav {
      background-color: #747feb;
      width: 150px;
      position : absolute;
      top : 100px;
      left : 0;
      bottom : 0;
    }
    
     article {
      background: #74eb8a;
      position: absolute;
      top : 100px;
      left : 150px ; /* nav width*/
      bottom : 0;
      right : 0;
     }
    

    【讨论】:

      猜你喜欢
      • 2015-05-15
      • 1970-01-01
      • 2015-06-08
      • 2014-06-12
      • 2014-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-17
      相关资源
      最近更新 更多