Fill remaining vertical space with CSS using display:flex

后端 未结 5 1316
天命终不由人
天命终不由人 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 11:57

    Use the flex-grow property to the main content div and give the dispaly: flex; to its parent;

    body {
        height: 100%;
        position: absolute;
        margin: 0;
    }
    section {
      height: 100%;
      display: flex;
      flex-direction : column;
    }
    header {
      background: tomato;
    }
    div {
      flex: 1; /* or flex-grow: 1  */;
      overflow-x: auto;
      background: gold;
    }
    footer {
      background: lightgreen;
      min-height: 60px;
    }
    header: sized to content
    (but is it really?)
    main content: fills remaining space
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    x
    footer: fixed height in px

提交回复
热议问题