使用 display:flex 使用 CSS 填滿剩餘的垂直空間
P粉794851975
P粉794851975 2023-08-23 22:23:14
0
2
476
<p>在 3 行佈局:</p> <ul> <li>上排的大小應依其內容而定</li> <li>底行應具有固定的高度(以像素為單位)</li> <li>中間行應展開以填滿容器</li> </ul> <p>問題在於,隨著主要內容的擴展,它會擠壓頁首和頁尾行:</p> <p><br /></p> <pre class="brush:css;toolbar:false;">section { display: flex; flex-flow: column; align-items: stretch; height: 300px; } header { flex: 0 1 auto; background: tomato; } div { flex: 1 1 auto; background: gold; overflow: auto; } footer { flex: 0 1 60px; background: lightgreen; /* fixes the footer: min-height: 60px; */ }</pre> <pre class="brush:html;toolbar:false;"><section> <header> header: sized to content <br>(but is it really?) </header> <div> main content: fills remaining space<br> x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br> <!-- uncomment to see it break - -> x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br> x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br> x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br> x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br> <!-- --> </div> <footer> footer: fixed height in px </footer> </section></pre> <p><br /></p> <p><strong>小提琴:</strong></p> <ul> <li>http://jsfiddle.net/7yLFL/1/(正在運行,內容較少)</li> <li>http://jsfiddle.net/7yLFL/(已損壞,內容較大)</li> </ul> <p>我很幸運,可以使用最新、最好的 CSS,而無需考慮舊版瀏覽器。我想我可以使用彈性佈局來最終擺脫舊的基於表格的佈局。由於某種原因,它沒有做我想做的事......</p> <p>根據記錄,SO上有很多關於「填充剩餘高度」的相關問題,但沒有任何問題可以解決我在使用flex時遇到的問題。參考資料:</p> <ul> <li>讓 div 填滿剩餘螢幕空間的高度</li> <li>填滿剩餘的垂直空間 - 僅 CSS</li> <li>與另一個 div 共用容器時是否有一個 div 來填充容器的剩餘高度/寬度? </li> <li>使嵌套 div 拉伸至剩餘容器 div 高度的 100%</li> <li>如何讓 Flexbox 佈局佔據 100% 垂直空間? </li> <li>等等</li> </ul><p><br /></p>
P粉794851975
P粉794851975

全部回覆(2)
P粉555696738

下面的範例包括當展開的中心元件的內容超出其邊界時的滾動行為。此外,中心組件佔用視窗中 100% 的剩餘空間。

jsfiddle 這裡

#
html, body, .r_flex_container{
    height: 100%;
    display: flex;
    flex-direction: column;
    background: red;
    margin: 0;
}
.r_flex_container {
    display:flex;
    flex-flow: column nowrap;
    background-color:blue;
}

.r_flex_fixed_child {
    flex:none;
    background-color:black;
    color:white;

}
.r_flex_expand_child {
    flex:auto;
    background-color:yellow;
    overflow-y:scroll;
}

可用於示範此行為的 html 範例

<html>
<body>
    <div class="r_flex_container">
      <div class="r_flex_fixed_child">
        <p> This is the fixed 'header' child of the flex container </p>
      </div>
      <div class="r_flex_expand_child">
            <article>this child container expands to use all of the space given to it -  but could be shared with other expanding childs in which case they would get equal space after the fixed container space is allocated. 
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,
            </article>
      </div>
      <div class="r_flex_fixed_child">
        this is the fixed footer child of the flex container
        asdfadsf
        <p> another line</p>
      </div>

    </div>
</body>
</html>
P粉018548441

簡單一點:DEMO

#

section {
  display: flex;
  flex-flow: column;
  height: 300px;
}

header {
  background: tomato;
  /* no flex rules, it will grow */
}

div {
  flex: 1;  /* 1 and it will fill whole space left if no flex value are set to other children*/
  background: gold;
  overflow: auto;
}

footer {
  background: lightgreen;
  min-height: 60px;  /* min-height has its purpose :) , unless you meant height*/
}
<section>
  <header>
    header: sized to content
    <br/>(but is it really?)
  </header>
  <div>
    main content: fills remaining space<br> x
    <br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    <!-- uncomment to see it break -->
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br> x
    <br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br> x
    <br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br> x
    <br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    <!-- -->
  </div>
  <footer>
    footer: fixed height in px
  </footer>
</section>
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!