Limit flexbox height to browser window (currently it's overflowing causing vertical scroll)

后端 未结 2 657
梦毁少年i
梦毁少年i 2021-01-18 02:14

I\'m trying to have an app that fit the window size of the browser.

The menu should have a height that fit 100% of the parent and not have a height of 100% of the sc

2条回答
  •  滥情空心
    2021-01-18 02:27

    You need to adjust your height: 100% in two places. Currently, it's combining with additional px heights defined in your code, which is causing the overflow in the browser window.

    Instead of this:

    .layout-flex-container {
          height: 100%;
    }
    
    aside > nav {
          height: 100%;
    }
    

    Try this:

    .layout-flex-container {
          height: calc(100% - 128px);  /* subtract the defined height of the header element */
    }
    
    aside > nav {
           height: calc(100% - 64px);  /* subtract the defined line-height of the h2 element */
    }
    

    Revised Fiddle

    Learn more about the CSS calc function at W3C:

    8.1. Mathematical Expressions: calc()

    The calc() function allows mathematical expressions with addition (+), subtraction (-), multiplication (*), and division (/) to be used as component values. The calc() expression represents the result of the mathematical calculation it contains, using standard operator precedence rules. It can be used wherever , , , , , or values are allowed. (Read more.)

提交回复
热议问题