Fill remaining vertical space with CSS using display:flex

后端 未结 5 1284
天命终不由人
天命终不由人 2020-11-21 11:06

In a 3-row layout:

  • the top row should be sized according to its contents
  • the bottom row should have a fixed height in pixels
  • the middle row s
5条回答
  •  有刺的猬
    2020-11-21 12:07

    The example below includes scrolling behaviour if the content of the expanded centre component extends past its bounds. Also the centre component takes 100% of remaining space in the viewport.

    jsfiddle here

    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;
    }
    

    Example of html that can be used to demonstrate this behaviour

    
    
        

    This is the fixed 'header' child of the flex container

    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,
    this is the fixed footer child of the flex container asdfadsf

    another line

提交回复
热议问题