Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align contents of div to the bottom

I have a div containing 2 paragraphs. I want the paragraphs to be aligned to the bottom right of the div. I was able to align the paragrams using text-align: right, but I am struggling with trying to get the paragrams to align to the bottom of the div.

The markup is quite simple:

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

CSS:

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

p{
    text-align: right;
}

I tried using position:absolute on the paragrams and setting bottom: 0, but this makes the paragraphs overlap each other due to the absolute positioning.

The div does not span the whole page, so the paragraph text should be contrained within the div.

Is there a way to achieve the effect such that the content "grows from the bottom"?

The effect I want is like this (photoshopped): enter image description here

And a fiddle of the above: http://jsfiddle.net/cbY6h/

like image 551
F21 Avatar asked Apr 29 '12 01:04

F21


People also ask

How do I align the content at the bottom of a div?

Use the text-align property to align the inner content of the block element. Use the bottom and left properties. The bottom property specifies the bottom position of an element along with the position property. The left property specifies the left position of an element along with the position property.

How do I align text to the bottom of a box?

In the Format Text Box dialog box, click the Text Box tab. In the Vertical alignment box, select Top, Middle, or Bottom.

How align to bottom of div in bootstrap?

align items to the left or right make sure that you include the entire d-flex align-items-end flex-column on the parent element and choose start or end depending if you want it to be aligned left or right. align item to the bottom Then, use mt-auto on the div that you want to stick to the bottom of the parent.


2 Answers

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/.

like image 68
Bill Avatar answered Nov 10 '22 01:11

Bill


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 }
like image 35
Frison Alexander Avatar answered Nov 10 '22 00:11

Frison Alexander