固定位置溢出不滚动



在我的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>

我看到您有.contentdiv,其中包含flex:1;集,您需要将弹性项目的父项设置为display:flex,否则您的弹性框特征将不起作用。 您可能还希望将弹性方向设置为列。 此外,如果您希望弹性项目填充容器,您的弹性项目必须是附加了display:flex的父项的直接子项。 因此,要么删除<section>,要么将<section>设置为弹性框并为其提供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>

您需要为.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,因此静态高度是不可接受的。然后,您从后端接收的内容将根据需要占用尽可能多的空间,直到达到最大高度值,然后溢出。

最新更新