Align text to the bottom of a div

前端 未结 2 1638
闹比i
闹比i 2020-12-02 14:11

I tried to align my text to the bottom of a div from other posts and answers in Stack Overflow I learned to handle this with different CSS properties. But I can\'t get it do

2条回答
  •  一个人的身影
    2020-12-02 14:56

    Flex Solution

    It is perfectly fine if you want to go with the display: table-cell solution. But instead of hacking it out, we have a better way to accomplish the same using display: flex;. flex is something which has a decent support.

    .wrap {
      height: 200px;
      width: 200px;
      border: 1px solid #aaa;
      margin: 10px;
      display: flex;
    }
    
    .wrap span {
      align-self: flex-end;
    }
    Align me to the bottom

    In the above example, we first set the parent element to display: flex; and later, we use align-self to flex-end. This helps you push the item to the end of the flex parent.


    Old Solution (Valid if you are not willing to use flex)

    If you want to align the text to the bottom, you don't have to write so many properties for that, using display: table-cell; with vertical-align: bottom; is enough

    div {
      display: table-cell;
      vertical-align: bottom;
      border: 1px solid #f00;
      height: 100px;
      width: 100px;
    }
    Hello

    (Or JSFiddle)

提交回复
热议问题