How can I position my div at the bottom of its container?

前端 未结 24 2424
广开言路
广开言路 2020-11-22 00:46

Given the following HTML:



        
24条回答
  •  无人共我
    2020-11-22 01:41

    The flexbox approach!

    In supported browsers, you can use the following:

    Example Here

    .parent {
      display: flex;
      flex-direction: column;
    }
    .child {
      margin-top: auto;
    }
    

    .parent {
      height: 100px;
      border: 5px solid #000;
      display: flex;
      flex-direction: column;
    }
    .child {
      height: 40px;
      width: 100%;
      background: #f00;
      margin-top: auto;
    }
    Align to the bottom


    The solution above is probably more flexible, however, here is an alternative solution:

    Example Here

    .parent {
      display: flex;
    }
    .child {
      align-self: flex-end;
    }
    

    .parent {
      height: 100px;
      border: 5px solid #000;
      display: flex;
    }
    .child {
      height: 40px;
      width: 100%;
      background: #f00;
      align-self: flex-end;
    }
    Align to the bottom


    As a side note, you may want to add vendor prefixes for additional support.

提交回复
热议问题