首页 文章

为什么不高度:100%工作将div扩展到屏幕高度?

提问于
浏览
229

我希望旋转木马DIV(s7)扩展到整个屏幕的高度 . 我没有成功.2766153没有成功 . 要查看该页面,您可以去here .

body {
  height: 100%;
  color: #FFF;
  font: normal 28px/28px'HelveticaWorldRegular', Helvetica, Arial, Sans-Serif;
  background: #222 url('') no-repeat center center fixed;
  overflow: hidden;
  background-size: cover;
  margin: 0;
  padding: 0;
}
.holder {
  height: 100%;
  margin: auto;
}
#s7 {
  width: 100%;
  height: 100%: margin: auto;
  overflow: hidden;
  z-index: 1;
}
#s7 #posts {
  width: 100%;
  min-height: 100%;
  color: #FFF;
  font-size: 13px;
  text-align: left;
  line-height: 16px;
  margin: auto;
  background: #AAA;
}
<div class="nav">
  <a class="prev2" id="prev2" href="#">
    <img src="http://static.tumblr.com/ux4v5bf/ASslogxz4/left.png">
  </a>
  <a class="next2" id="next2" href="#">
    <img src="http://static.tumblr.com/ux4v5bf/swslogxmg/right.png">
  </a>
</div>

<div class="holder">
  <tr>
    <td>
      <div id="s7">
        {block:Posts}
        <div id="posts">

9 回答

  • 6

    为了使百分比值适用于身高,必须确定父亲的身高 . 唯一的例外是 root 元素 <html> ,它可以是百分比高度 . .

    所以,除了 <html> 之外,你已经给出了所有元素的高度,所以你应该做的就是添加:

    html {
        height: 100%;
    }
    

    你的代码应该可以正常工作 .

    * { padding: 0; margin: 0; }
    html, body, #fullheight {
        min-height: 100% !important;
        height: 100%;
    }
    #fullheight {
        width: 250px;
        background: blue;
    }
    
    <div id=fullheight>
      Lorem Ipsum        
    </div>
    

    JsFiddle example .

  • 1

    既然没人提到这个......

    现代方法:

    作为将 html / body 元素的高度设置为 100% 的替代方法,您还可以使用视口百分比长度:

    5.1.2 . 视口百分比长度:'vw','vh','vmin','vmax'单位视口百分比长度相对于初始包含块的大小 . 当初始包含块的高度或宽度改变时,它们相应地缩放 .

    在这种情况下,您可以使用值 100vh (这是视口的高度) - (example)

    body {
        height: 100vh;
    }
    

    设置 min-height 也有效 . (example)

    body {
        min-height: 100vh;
    }
    

    大多数现代浏览器都支持这些单元 - support can be found here .

  • 4

    您还需要在 html 元素上设置100%高度:

    html { height:100%; }
    
  • 116

    或者,如果您使用 position: absolute ,则 height: 100% 将正常工作 .

  • 0

    你应该尝试使用父元素;

    html, body, form, main {
        height: 100%;
    }
    

    那就足够了:

    #s7 {
       height: 100%;
    }
    
  • 21

    如果你想要,例如,左列(高度100%)和内容(高度自动)你可以使用绝对:

    #left_column {
        float:left;
        position: absolute;
        max-height:100%;
        height:auto !important;
        height: 100%;
        overflow: hidden;
    
        width : 180px; /* for example */
    }
    
    #left_column div {
        height: 2000px;
    }
    
    #right_column {
        float:left;
        height:100%;
        margin-left : 180px; /* left column's width */
    }
    

    在HTML中:

    <div id="content">
          <div id="left_column">
            my navigation content
            <div></div>
          </div>
    
          <div id="right_column">
            my content
          </div>
       </div>
    
  • 19

    这可能不太理想,但你总是可以用javascript做到这一点 . 或者在我的情况下jQuery

    <script>
    var newheight = $('.innerdiv').css('height');
    $('.mainwrapper').css('height', newheight);
    </script>
    
  • 0

    在页面源代码中,我看到以下内容:

    <div class="holder"> 
        <div id="s7" style="position: relative; width: 1366px; height: 474px; overflow: hidden;">
    

    如果将高度值放在标记中,它将使用此值而不是css文件中定义的高度 .

  • 330

    如果绝对将元素放在div中,可以将填充顶部和底部设置为50% .

    所以像这样:

    #s7 {
        position: relative;
        width:100%;
        padding: 50% 0;
        margin:auto;
        overflow: hidden;
        z-index:1;
    }
    

相关问题