Fill remaining vertical space with CSS using display:flex

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

    A more modern approach would be to use the grid property.

    section {
      display: grid;
      align-items: stretch;
      height: 300px;
      grid-template-rows: min-content auto 60px;
    }
    header {
      background: tomato;
    }
    div {
      background: gold;
      overflow: auto;
    }
    footer {
      background: lightgreen;
    }
    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
    x
    x
    x
    x
    x
    x
    footer: fixed height in px

提交回复
热议问题