Stacking Divs from Bottom to Top

前端 未结 7 2179
北荒
北荒 2020-12-04 09:46

When appending divs to a div with a fixed height, the child divs will appear from top to bottom, sticking at the top border.

┌─────         


        
7条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-04 10:44

    All the answers miss the scrollbar point of your question. And it's a tough one. If you only need this to work for modern browsers and IE 8+ you can use table positioning, vertical-align:bottom and max-height. See MDN for specific browser compatibility.

    Demo (vertical-align)

    .wrapper {
      display: table-cell;
      vertical-align: bottom;
      height: 200px;
    }
    .content {
      max-height: 200px;
      overflow: auto;
    }
    

    html

    row 1
    row 2
    row 3

    Other than that, I think it's not possible with CSS only. You can make elements stick to the bottom of their container with position:absolute, but it'll take them out of the flow. As a result they won't stretch and make the container to be scrollable.

    Demo (position-absolute)

    .wrapper {
      position: relative;
      height: 200px;
    }
    .content {
      position: absolute;
      bottom: 0;
      width: 100%;
    }
    

提交回复
热议问题