代码之家  ›  专栏  ›  技术社区  ›  user2024080

固定位置溢出不是滚动

  •  1
  • user2024080  · 技术社区  · 7 年前

    在我的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 回复  |  直到 7 年前
        1
  •  3
  •   Steve K    7 年前

    我知道你有 .content flex:1; 设置您需要将flex项的父项设置为 display:flex 或者你的Flexbox特性不起作用。您可能还需要将flex direction设置为column。另外,如果您希望flex items填充容器,则flex items必须是具有 显示:flex 依附于它。所以要么移除 <section> 或设置 <章节> 做一个曲轴箱给它 显示: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>  
        2
  •  0
  •   Jake Miller    7 年前

    您需要为 .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 如果使用动态内容,则不接受静态高度。然后,从后端接收的内容将占用所需的空间,直到达到最大高度值,然后溢出。