【问题标题】:100% Height div, scrollbars 50px off screen due to margin-top 50px. overflow auto100% 高度 div,由于 margin-top 50px,滚动条离开屏幕 50px。溢出自动
【发布时间】:2013-06-14 14:02:56
【问题描述】:

我正在尝试布局一个页面,该页面顶部有一个水平导航栏,内容区域会在内容太大时获得滚动条。

我目前的做法是 div 宽度为 100%;高度 50 像素;绝对位置;前 0;左0;然后是高度为 100% 的第二个 div;宽度 100%;边距顶部 50px;溢出:自动;但是,出现的滚动条会偏移 50px 边距,因此会将内容推离页面底部。如果我删除边距,一切正常,但它会将内容放在导航栏后面。

我还尝试将它包装在一个容器中,尝试将它们的边距和溢出放在孩子身上,但这似乎仍然没有达到所需的效果。

jsFiddle,用 cmets 来更好地解释一下。

http://jsfiddle.net/Piercy/hggXJ/

HTML

<div id="nav">
  <h1>Navigation</h1>
</div>
<!-- <div id="contentContainer"> -->
  <div id="content">
    <div id="oversizeContent">
        <p>You can see the black border on this green box, but you cannot see the bottom black border. Similarly you cannot see the bottom arrow of the scrollbar.</p>
        <p>I tried putting a wrapper around the content and putting the margin on that but the scrollbar still over flows  Uncomment the contentContainer div and comment/uncomment the corresponding  css.</p>
    </div>
  </div>
<!-- <div> -->

CSS

html, body
{
  height:100%;
  overflow:hidden;
  color:white;
  font-weight:bold;
}

#nav
{
  height:50px;
  width:100%;
  background-color:red;
  position:absolute;
  top:0;
  left:0;
  z-index: 2;
}
#contentContainer
{
  /* uncomment this if you bring #contentContainer into play */
  /* 
      margin-top:50px;
      position:absolute;
      top:0;
      left:0;   
  */


  height:100%;
  width:100%; 

}
#content
{
  /* moving this into #contentContainer doesnt help either */
  /* comment this if you bring #contentContainer into play */

      margin-top:50px;
      position:absolute;
      top:0;
      left:0;



  width:100%;
  height:100%;

  background-color:blue;
  z-index: 1;
  overflow: auto;
}
#oversizeContent 
{
  background-color:green;
  width:400px;
  height:1000px;
  border:10px solid black;
}

【问题讨论】:

  • 您是否尝试过使用#oversizeContent { margin-bottom: 50px; } 来解决此问题?

标签: html css layout


【解决方案1】:

而不是height:100%;bottom:0; 如果没有容器设置为position:relative;,它将转到相对父级或视口的底部

DEMO

【讨论】:

  • 我不得不保留顶部边距但添加底部:0;并删除高度:100%;似乎做到了!谢谢,jsfiddle.net/Piercy/E69TZ/1
  • 很高兴为您提供帮助! ^_^ ...如果您有绝对定位的元素,通常使用 top、bottom、left 和 right 来代替 height 和 width 属性非常方便。
  • 我还必须做“位置:固定”而不是绝对,这样如果主页有滚动条,100% 高度的内容就不会滚出窗口。
猜你喜欢
  • 2018-04-30
  • 1970-01-01
  • 1970-01-01
  • 2013-12-08
  • 1970-01-01
  • 1970-01-01
  • 2011-06-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多