【问题标题】:Set height relative to parents parent but stretch parent设置相对于父母父母的高度,但拉伸父母
【发布时间】:2015-10-08 21:23:42
【问题描述】:

这个 HTML 结构有一个div#page,当前页面内容将通过 Ajax 加载在其中。内容始终由section 标记组成,可以具有动态高度(相对于浏览器的百分比)或静态高度(以像素为单位)。

div#page 的高度应该调整,以便footer 紧跟在最后一个div#page > section 之后。

为了能够为div#page > section 标签设置百分比值,我给div#page 设置了100% 的高度。因此,它的 DOM 高度不会拉伸。

如果footer 标签在div#page 内,它会起作用。这对我来说不是一个好的解决方案,因为页脚会被动态加载的页面内容覆盖。

是否有一些神奇的 CSS 解决方案可以正确拉伸 div#page

body, html { margin: 0; padding: 0; }
#outer { background: red; position: absolute; width: 100%; height: 100%; }
#page { height: 100%; }

#page > section { background: #666; width: 100%; }
#page > section:nth-of-type(2n) { background: #333; }
#page > section:nth-of-type(1) { height: 100%; }
#page > section:nth-of-type(2) { height: 160px; }
#page > section:nth-of-type(3) { height: 220px; }
#page > section:nth-of-type(4) { height: 120px; }

footer { background: green; height: 160px; }
<div id="outer">

    <!-- The current page content will be loaded into this div. -->
    <div id="page">
      <section>Full height.</section>
      <section>Static height 1.</section>
      <section>Static height 2.</section>
      <section>Static height 3.</section>
    </div>

    <!-- The footer is static and therefore not inside of the #page div. -->
    <footer>
      Immediately after static height 3.
    </footer>
  </div>

【问题讨论】:

标签: html css dom


【解决方案1】:

如果您降低#page div 的高度并将第一个section 设置为100vh,我想它会按您的意愿工作,尽管仅适用于支持“视口”单元vh 的较新浏览器.

浏览器支持:http://caniuse.com/#feat=viewport-units

  body, html { margin: 0; padding: 0; }
  #outer { background: red; position: absolute; width: 100%; height: 100%; }
  #page { }

  #page > section { background: #666; width: 100%; }
  #page > section:nth-of-type(2n) { background: #333; }
  #page > section:nth-of-type(1) { height: 100vh; }
  #page > section:nth-of-type(2) { height: 160px; }
  #page > section:nth-of-type(3) { height: 220px; }
  #page > section:nth-of-type(4) { height: 120px; }

  footer { background: green; height: 160px; }
<div id="outer">

    <!-- The current page content will be loaded into this div. -->
    <div id="page">
      <section>Full height.</section>
      <section>Static height 1.</section>
      <section>Static height 2.</section>
      <section>Static height 3.</section>
    </div>

    <!-- The footer is static and therefore not inside of the #page div. -->
    <footer>
      Immediately after static height 3.
    </footer>
  </div>

【讨论】:

    【解决方案2】:

    您好,我尝试通过更改 outer div 的位置来解决此问题

    body, html { margin: 0; padding: 0; }
      #outer { background: red; position: relative; width: 100%; height: auto; }
      #page { height: 100%; }
    
      #page > section { background: #666; width: 100%; }
      #page > section:nth-of-type(2n) { background: #333; }
      #page > section:nth-of-type(1) { height: 100%;background: red; }
      #page > section:nth-of-type(2) { height: 160px;background: yellow; }
      #page > section:nth-of-type(3) { height: 220px;background: aqua; }
      #page > section:nth-of-type(4) { height: 120px;background: darkblue; }
    
      footer { background: green; height: 160px; }

    我已经包含了工作演示代码,以便您可以检查是否如您所愿..

    Demo Code you can check out

    【讨论】:

    • @ChristophBühler 请检查答案是否有效。?
    • 红色容器应该是页面高度的 100%。暂时没有。
    猜你喜欢
    • 2012-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    • 2016-11-12
    • 1970-01-01
    相关资源
    最近更新 更多