Align contents of div to the bottom

我的未来我决定 提交于 2019-12-05 05:42:25

HTML

<div class="box">
   <div class="content">
       <p>content 1</p>
       <p>content 2</p>
   </div>
</div>​​​​​​​​​​​​​​​​​​​​​

CSS

.box {
    border: 1px red solid;
    height: 100px;
    position: relative;
}

.content {
    position: absolute;
    bottom: 0;
    right: 0;
}

Will place the content in the bottom right hand corner of the div. You can see the result at http://jsfiddle.net/cbY6h/1/.

I was looking for something similar, and ended up using flexbox layout.

Given the following structure

<div>
   <p>content 1</p>
    <p>another</p>
   <p>content 2</p>
</div>

and this style

div {
    border: 1px red solid;
    height: 100px;

    display: flex;

    //align items from top to bottom 
    //[I've always thought the opposite, as rows are counted from top to bottom 
    //and columns are counted left to right. Any one have an explanation for this?]
    flex-direction: column;   

    //main axis align to the bottom, since we are using column for direction
    justify-content: flex-end; 

}

p { 
   //cross axis align towards the right. total outcome => bottom right
   align-self: flex-end; 
}

You will get the layout from the picture.

Edit: Won't work on older browsers (http://caniuse.com/flexbox). You can key of Modernizer

.flexbox .rest-of-your-selector { rule }

A key note to understand. If you were to change the height of the "box" to 100% instead of 100px, then it would not work. If height is not specified as a specific unit of measure it can't figure out how to place the absolute child item.

.box {
border: 1px red solid;
height: 100%;
position: relative;
}

Use relative instead of absolute for position of .content.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!