Align an element to bottom with flexbox

前端 未结 8 1985
陌清茗
陌清茗 2020-11-22 15:34

I have a div with some children:

heading 1

heading 2

Some

8条回答
  •  长发绾君心
    2020-11-22 16:18

    You can use auto margins

    Prior to alignment via justify-content and align-self, any positive free space is distributed to auto margins in that dimension.

    So you can use one of these (or both):

    p { margin-bottom: auto; } /* Push following elements to the bottom */
    a { margin-top: auto; } /* Push it and following elements to the bottom */
    

    .content {
      height: 200px;
      border: 1px solid;
      display: flex;
      flex-direction: column;
    }
    h1, h2 {
      margin: 0;
    }
    a {
      margin-top: auto;
    }

    heading 1

    heading 2

    Some text more or less

    Click me

    Alternatively, you can make the element before the a grow to fill the available space:

    p { flex-grow: 1; } /* Grow to fill available space */
    

    .content {
      height: 200px;
      border: 1px solid;
      display: flex;
      flex-direction: column;
    }
    h1, h2 {
      margin: 0;
    }
    p {
      flex-grow: 1;
    }

    heading 1

    heading 2

    Some text more or less

    Click me

提交回复
热议问题