固定位置溢出不滚动

时间:2018-06-26 02:45:46

标签: javascript html css3

在我的html中,我不知道header的高度。那么如何使内容在溢出时可滚动?

.parent{
  border:2px solid red;
  position:fixed;
  top:0;
  left:0;
  width:20%;
  height:50%;
}

header{
  width:100%;
  border:1px solid red;
}

.content{
  flex:1;
  position: relative;
  overflow-y:scroll;
  background:lightgreen;
}
<div class="parent">
  <section>
    <header>
      <h1>Title</h1>  
    </header>
    <div class="content">
      Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

      Why do we use it?
      It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
    </div>
  </section>
</div>  

2 个答案:

答案 0 :(得分:3)

我看到您已将.content的div设置为flex:1;,需要将flex项目的父项设置为display:flex,否则flexbox的特性将不起作用。您可能还需要将flex-direction设置为column。另外,如果您希望弹性项目填充容器,则弹性项目必须是附加了display:flex的父级的直接子级。因此,请删除<section>或将<section>设置为flexbox并为其指定display:flex。这是一个片段:

.parent{
  border:2px solid red;
  position:fixed;
  top:0;
  left:0;
  width:20%;
  height:50%;
  display:flex;
  flex-direction:column;
}

header{
  width:100%;
  border:1px solid red;
}

.content{
	position: relative;
  background:lightgreen;
  width:100%;
  flex:1;
  overflow-Y:scroll;
}
<div class="parent">
  <header>
    <h1>Title</h1>  
  </header>
  <div class="content">
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
  </div>
</div>  

答案 1 :(得分:0)

您需要为.content类指定一个高度值,否则溢出将不起作用。

.parent{
  border:2px solid red;
  position:fixed;
  top:0;
  left:0;
  width:20%;
  height:50%;
}

header{
  width:100%;
  border:1px solid red;
}

.content{
  flex:1;
  position: relative;
  overflow-y:scroll;
  height: 250px;
  background:lightgreen;
}
<div class="parent">
  <section>
    <header>
      <h1>Title</h1>  
    </header>
    <div class="content">
      Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

      Why do we use it?
      It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
    </div>
  </section>
</div>  

编辑::如果使用动态内容,则使用max-height,因此静态高度不可接受。然后,您从后端接收到的内容将占用所需的空间,直到达到最大高度值,然后溢出。

相关问题