Heim  >  Fragen und Antworten  >  Hauptteil

Verwenden Sie display:flex, um den verbleibenden vertikalen Raum mithilfe von CSS zu füllen

<p>In einem 3-Zeilen-Layout: </p> <ul> <li>Die Größe der oberen Zeile sollte ihrem Inhalt entsprechen</li> <li>Die untere Zeile sollte eine feste Höhe in Pixeln haben</li> <li>Die mittlere Reihe sollte sich erweitern, um den Behälter zu füllen</li> </ul> <p>Das Problem besteht darin, dass bei der Erweiterung des Hauptinhalts die Kopf- und Fußzeilen zusammengedrückt werden: </p> <p><br /></p> <pre class="brush:css;toolbar:false;">section { Anzeige: Flex; Flex-Flow: Spalte; align-items: stretch; Höhe: 300px; } Header { Flex: 0 1 automatisch; Hintergrund: Tomate; } div { Flex: 1 1 Auto; Hintergrund: Gold; Überlauf: automatisch; } Fusszeile { Flex: 0 1 60px; Hintergrund: hellgrün; /* korrigiert die Fußzeile: min-height: 60px; }</pre> <pre class="brush:html;toolbar:false;"><section> <Kopfzeile> Kopfzeile: an den Inhalt angepasst <br>(aber ist es das wirklich?) </header> <div> Hauptinhalt: füllt verbleibenden Platz<br> x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br> <!-- Kommentar entfernen, um zu sehen, wie es funktioniert - -> 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> <Fußzeile> Fußzeile: feste Höhe in px </footer> </section></pre> <p><br /></p> <p><strong>Violine:</strong></p> <ul> <li>http://jsfiddle.net/7yLFL/1/ (läuft, weniger Inhalt)</li> <li>http://jsfiddle.net/7yLFL/ (beschädigt, großer Inhalt)</li> </ul> <p>Ich habe das Glück, unabhängig von älteren Browsern das neueste und beste CSS verwenden zu können. Ich denke, ich könnte das Flex-Layout verwenden, um das alte tabellenbasierte Layout endlich loszuwerden. Aus irgendeinem Grund macht es nicht das, was ich will...</p> <p>Fürs Protokoll: Auf SO gibt es viele verwandte Fragen zum Thema „Auffüllen der verbleibenden Höhe“, aber keine davon löst das Problem, das ich mit Flex habe. Referenz:</p> <ul> <li>Lassen Sie die Höhe des Div den verbleibenden Bildschirmraum ausfüllen</li> <li>Verbleibenden vertikalen Raum füllen – nur CSS</li> <li>Füllt ein Div die verbleibende Höhe/Breite des Containers aus, wenn der Container mit einem anderen Div geteilt wird? </li> <li>Das verschachtelte Div auf 100 % der Höhe des verbleibenden Container-Divs ausdehnen</li> <li>Wie kann ich dafür sorgen, dass ein Flexbox-Layout 100 % des vertikalen Raums einnimmt? </li> <li>Warten</li> </ul><p><br /></p>
P粉794851975P粉794851975395 Tage vor438

Antworte allen(2)Ich werde antworten

  • P粉555696738

    P粉5556967382023-08-24 19:14:15

    下面的示例包括当展开的中心组件的内容超出其边界时的滚动行为。此外,中心组件占用视口中 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>

    Antwort
    0
  • P粉018548441

    P粉0185484412023-08-24 16:03:05

    简单一点: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>

    Antwort
    0
  • StornierenAntwort