【问题标题】:Three-row modal with fixed with header, bottom row, and scrollable middle row三行模式,带有固定的标题、底行和可滚动的中间行
【发布时间】:2019-03-14 19:36:26
【问题描述】:

我正在尝试创建一个顶部、中间和底部的模式。顶部始终是固定高度。

底部必须“粘合”到底部,并且可能会有所不同。它里面的所有内容和元素都必须从底部构建。因此,如果有一行文本,底部将是该行的高度。如果有 3 或 4 行文字,底部会根据需要向上推。

中间需要填满所有剩下的,并且如果内容overflow-y的它必须显示滚动条并且不干扰顶行或底行。

我该怎么做?我在尝试overflow-y: auto 时发现它不起作用,大概是因为没有高度,而是将桌子推到了#wrap div 的范围之外。

这是一个 CodePen:https://codepen.io/sfullman/pen/XGZoJb

    body{
      font-family: Arial;
    }
    .table{
      display: table;
      height: 100%;
      width: 100%;
    }
    .row{
      display: table-row;
    }
    .cell{
      display: table-cell;
      width: 75%;
    }
    #top{
      background-color: burlywood;
      height: 41px;
    }
    #middle .cell{
      overflow-y: scroll;
    }
    #bottom{
      height: 15px; /* make this height less than expected height, table row/cell behavior will successfully exceed it an push content up */
      border-top: 1px solid darkred;
      background-color: rgba(0,0,0,.15);
    }
    #wrap{
      /* this div is designed to be a dialog/modal and will normally be abs. positioned */
      border: 1px solid darkred;
      width: 425px;
      height: 300px;
      position: absolute;
      top: 20px;
      left: 20px;
    }
    <div id="wrap">
      <div id="innerTable" class="table">
      <div id="top" class="row">
        <div class="cell">
        top
        </div>
      </div>
      <div id="middle" class="row">
        <div class="cell">
          middle<br>
          middle<br>
          middle<br>
          middle<br>
          middle<br>
          middle<br>
          middle<br>
          middle<br>
          middle<br>
          middle<br>
          middle<br>
          middle<br>
        </div>
      </div>
      <div id="bottom" class="row">
        <div class="cell">
        bottom<br>
        bottom<br>
        bottom<br>
        </div>
      </div>
    </div>
    </div>

【问题讨论】:

    标签: html css css-tables


    【解决方案1】:

    在网上搜索后,我在 JS fiddle 上找到了一些东西,然后我对其进行了修改以创建解决方案 here

    这是 HTML:

    <div id="body">
         <div id="head">
           <!-- if your have content larger than declared height here, it will simply roll under the bottom with no scrolling -->
            <p>Fixed size without scrollbar 1</p>
            <p>Fixed size without scrollbar 2</p>
            <p>Fixed size without scrollbar 3</p>
            <p>Fixed size without scrollbar 4</p>
        </div> 
        <div id="content">
            <!--
            add or remove these to see scrool/non-scroll behavior
            -->
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <!--
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            -->
        </div>
        <div id="foot">
          <!-- this content will build from the bottom, pushing the top wall up. #content's bottom will adjust up accordingly -->
            <p>Dynamic size without scrollbar 1</p>
            <p>Dynamic size without scrollbar 2</p>
            <p>Dynamic size without scrollbar 3</p>
        </div> 
    </div>
    

    还有 CSS:

    #body {
        position: absolute;
        top: 15px;
        left: 15px;
        height: 300px;
        width: 500px;
        outline: black dashed 1px;
        display: flex;
        flex-direction: column;
    }
    
    #head {
        /*border: blue solid 1px;*/
        background-color: rgba(0,0,255,0.25);
        height: 50px;
        flex-shrink: 0;
    }
    #content{
        /*border: red solid 1px;*/
        background-color: palegoldenrod;
        overflow-y: auto;
        height: 100%;
    }
    
    #foot {
        /*border: green solid 1px;*/
        background-color: whitesmoke;
        flex-shrink: 0;
    }
    

    【讨论】:

      【解决方案2】:

      我已经使用 flexbox 创建了一个示例:https://codepen.io/jgoncalves/pen/XGEqoj

      .container {
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        display: flex;
      }
      
      .main {
        flex: 1;
        display: flex;
        flex-direction: column;
      }
      
      .content {
        flex: 1;
        display: flex;
        overflow: auto;
      }
      
      body {
        font-family: sans-serif;
        font-size: 20px;
        line-height: 50px;
        text-transform: uppercase;
        font-weight: bold;
      }
      
      .header {
        text-align: center;
        color: #fff;
        background: #444;
      }
      
      .footer {
        padding: 6px 20px;
        background: #004141;
        color: #fff;
      }
      
      .content {
        background: #ddd;
      }
      <div class="container">
      	<div class="main">
            <div class="header">Main header Main headerMain headerMain headerMain headerMain headerMain headerMain header</div>
      		<div class="content">
      			  Scroll me Scroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll meScroll me
      		</div>
          <div class="footer">Footer end of page. Footer end of page. Footer end of page. Footer end of page. Footer end of page. Footer end of page. Footer end of page. Footer end of page. </div>
      	</div>
      </div>

      中心行滚动,同时具有不同高度的固定页眉和固定页脚。

      【讨论】:

        【解决方案3】:

        这个想法是有一个带有max-height 设置的column flexbox 设置填充它所在的容器 - 请参阅下面的演示以了解全视口设置(这是基于this flexbox-based question ):

        body {
          margin: 0;
        }
        *{
          box-sizing: border-box;
        }
        .row {
          display: flex;
          flex-direction: column;
          max-height: 100vh;
        }
        .flex {
          flex: 1;
          overflow-y: auto;
        }
        .row, .row > * {
          border: 1px solid;
        }
        <div class="row">
          <div>some content</div>
          <div class="flex">This fills the available space</div>
          <!-- fills/grows available space -->
          <div>another content</div>
        </div>

        请注意,headerfooter 的高度可以在此处动态变化 - 即使您没有固定的高度也可以。对于更复杂的布局,最好使用较新的 CSS Grid layouts,因为 flexbox 是一维布局,而 CSS Grid 是 2D 布局 解决方案 - 请参阅 one example here


        解决方案

        现在我将根据您的代码进行调整 - 看看 middle 部分中有大量内容时它是如何工作的:

        body {
          font-family: Arial;
        }
        
        .table {
          display: flex;
          flex-direction: column;
          max-height: 100%;
          width: 100%;
        }
        
        .cell {
          width: 75%;
        }
        
        #top {
          background-color: burlywood;
        }
        
        #middle {
          flex: 1;
          overflow-y: auto;
        }
        
        #bottom {
          border-top: 1px solid darkred;
          background-color: rgba(0, 0, 0, .15);
        }
        
        #wrap {
          border: 1px solid darkred;
          width: 425px;
          height: 300px;
          position: absolute;
          top: 20px;
          left: 20px;
        }
        <div id="wrap">
          <div id="innerTable" class="table">
            <div id="top" class="row">
              <div class="cell">
                top
              </div>
            </div>
            <div id="middle" class="row">
              <div class="cell">
                middle<br> middle
                <br> middle
                <br> middle
                <br> middle
                <br> middle
                <br> middle
                <br> middle
                <br> middle
                <br> middle
                <br> middle
                <br> middle
                <br> middle
                <br> middle
                <br> middle
                <br> middle
                <br> middle
                <br> middle
                <br> middle
                <br> middle
              </div>
            </div>
            <div id="bottom" class="row">
              <div class="cell">
                bottom
              </div>
            </div>
          </div>
        </div>

        现在看看当 middle 部分中的内容非常少时它是如何工作的 - 请注意 footer 部分是如何到底部的:

        body {
          font-family: Arial;
        }
        
        .table {
          display: flex;
          flex-direction: column;
          max-height: 100%;
          width: 100%;
        }
        
        .cell {
          width: 75%;
        }
        
        #top {
          background-color: burlywood;
        }
        
        #middle {
          flex: 1;
          overflow-y: auto;
        }
        
        #bottom {
          border-top: 1px solid darkred;
          background-color: rgba(0, 0, 0, .15);
        }
        
        #wrap {
          border: 1px solid darkred;
          width: 425px;
          height: 300px;
          position: absolute;
          top: 20px;
          left: 20px;
        }
        <div id="wrap">
          <div id="innerTable" class="table">
            <div id="top" class="row">
              <div class="cell">
                top
              </div>
            </div>
            <div id="middle" class="row">
              <div class="cell">
                middle<br> middle
                <br> middle
                <br> middle
                <br> middle
        
                <br> middle
                <br> middle
                <br> middle
                <br> middle
                <br> middle
                <br> middle
              </div>
            </div>
            <div id="bottom" class="row">
              <div class="cell">
                bottom
              </div>
            </div>
          </div>
        </div>

        【讨论】:

        • 感谢您为此付出的努力。我是 flexbox 的新手。底部没有“停留”的第二个实例仍然是一个问题。搜索后我在 JS Fiddle 上找到了一些东西,正是我需要的,在这里:jsfiddle.net/sfullman/2e8stnfg/20
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-11-22
        • 2012-07-14
        • 1970-01-01
        • 1970-01-01
        • 2011-02-07
        • 1970-01-01
        • 2013-01-14
        相关资源
        最近更新 更多