Issue when centering vertically with flexbox when heights are unknown

前端 未结 2 1098
长发绾君心
长发绾君心 2020-11-30 20:15

My layout has a container flex-container and a child.

HTML:

text
<
2条回答
  •  孤街浪徒
    2020-11-30 20:37

    I found the solution:

    .flex-container {
        display: flex; /* only */
        overflow-y: scroll;
    }
    
    .flex-container > div {
        margin: auto; /* horizontal and vertical align */
    }
    

    html, body {
        height: 100%;
        width: 100%;
        padding: 0;
        margin: 0;
    }
    
    .flex-container {
        display: flex;
        width: 100%;
        height: 100px; /* change height to 300px */
        overflow-y: scroll;
        background: #2a4;
    }
    
    .flex-container > div {
        padding: 1em 1.5em;
        margin: auto;
        background: #E77E23;
        width: 400px;
    }
    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iure fugit voluptas eius nemo similique aperiam quis ut! Ipsa aspernatur rem nesciunt est sed hic culpa nisi delectus error explicabo reprehenderit. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iure fugit voluptas eius nemo similique aperiam quis ut! Ipsa aspernatur rem nesciunt est sed hic culpa nisi delectus error explicabo reprehenderit.

    Codepen: http://codepen.io/ces/pen/Idklh

提交回复
热议问题