250

The div inside another div picture and code below. Because there will be text and images of the parent div. And red div will be the last element of the parent div.

alt text

<div style="width: 200px; height: 150px; border: 1px solid black;">
  <div style="width: 100%; height: 50px; border: 1px solid red;">
  </div>
</div>
j08691
  • 204,283
  • 31
  • 260
  • 272
uzay95
  • 16,052
  • 31
  • 116
  • 182
  • So is the code from the parent div logically placed above the child div? Are the images and text dynamically added? What's the specific question? – justkt Jan 27 '10 at 13:56

9 Answers9

291

This is one way

<div style="position: relative; 
                width: 200px; 
                height: 150px; 
                border: 1px solid black;">

  <div style="position: absolute; 
                    bottom: 0; 
                    width: 100%; 
                    height: 50px; 
                    border: 1px solid red;">
  </div>
</div>

But because the inner div is positioned absolutely, you'll always have to worry about other content in the outer div overlapping it (and you'll always have to set fixed heights).

If you can do it, it's better to make that inner div the last DOM object in your outer div and have it set to "clear: both".

j08691
  • 204,283
  • 31
  • 260
  • 272
Jon Smock
  • 9,451
  • 10
  • 46
  • 49
136

A flexbox way.

.parent {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

/*  not necessary, just to visualize it */
.parent {
  height: 500px;
  border: 1px solid black;
}

.parent div {
  border: 1px solid red;
}
<div class="parent">
  <div>Images, text, buttons oh my!</div>
  <div>Bottom</div>
</div>

Edit:

Source - Flexbox Guide

Browser support for flexbox - Caniuse

leonheess
  • 16,068
  • 14
  • 77
  • 112
Franklin Tarter
  • 1,419
  • 1
  • 9
  • 4
  • 7
    OMG, where have you been all my [programming] life! ;) I love that this is so simple and doesn't mess with positioning, and it seems to just about work in all browsers: http://caniuse.com/#feat=flexbox. This should be the solution and accepted answer for modern programming. – Sablefoste Nov 15 '16 at 20:45
  • 1
    Flexbox is usually the "easy way" these days and has (nearly) great browser support and documentation. Adding the caniuse link to the answer. – Franklin Tarter Mar 12 '17 at 20:36
95

Make the outer div position="relative" and the inner div position="absolute" and set it's bottom="0".

Kevin
  • 6,539
  • 5
  • 44
  • 54
Fermin
  • 34,961
  • 21
  • 83
  • 129
  • 12
    Yes, this works but absolute positioning breaks the "natural layout". Inner div's height will not get included as height of parent and as the outer div gets narrower, you might see overlap with other stuff in outer div. – Ayush Gupta Nov 24 '14 at 19:07
  • If parent div has set overflow, then absolution position is not putting element correctly. – Aakash Jul 25 '22 at 12:22
  • "0" doesn't work with me. Replaced it with 0 and it works! – J.K Jan 12 '23 at 20:46
42

Here is another pure CSS trick, which doesn't affect an elements flow.

#parent {
  min-height: 100vh; /* set height as you need */
  display: flex;
  flex-direction: column;
  background: grey;
}
.child {
  margin-top: auto;
  background: green;
}
<div id="parent">
  <h1>Positioning with margin</h1>
  <div class="child">
    Content to the bottom
  </div>
</div>
R3gi
  • 422
  • 5
  • 8
9

You may not want absolute positioning because it breaks the reflow: in some circumstances, a better solution is to make the grandparent element display:table; and the parent element display:table-cell;vertical-align:bottom;. After doing this, you should be able to give the the child elements display:inline-block; and they will automagically flow towards the bottom of the parent.

Ansikt
  • 1,442
  • 11
  • 13
8

Note : This is by no means the best possible way to do it!

Situation : I had to do the same thign only i was not able to add any extra divs, therefore i was stuck with what i had and rather than removing innerHTML and creating another via javascript almost like 2 renders i needed to have the content at the bottom (animated bar).

Solution: Given how tired I was at the time its seems normal to even think of such a method however I knew i had a parent DOM element which the bar's height was starting from.

Rather than messing with the javascript any further i used a (NOT ALWAYS GOOD IDEA) CSS answer! :)

-moz-transform:rotate(180deg);
-webkit-transform:rotate(180deg);
-ms-transform:rotate(180deg);

Yes thats correct, instead of positioning the DOM, i turned its parent upside down in css.

For my scenario it will work! Possibly for others too ! No Flame! :)

Pogrindis
  • 7,755
  • 5
  • 31
  • 44
  • 1
    This may not be the best possible way to do it but this is pretty awesome, very creative approach. I myself have been wrestling this issue for a while today, my main sticking points: no set heights, must be completely fluid (not enough chars to explain why display: table was not working). This handled everything beautifully; the images inside the div are also flipped when this happens so I just used the class on those elements to flip them back. Looks exactly as it should, with no set heights. A bit hacky but hey, sometimes there's no better alternative. Well done! – tganyan Mar 27 '15 at 21:17
4

Here is way to avoid absolute divs and tables if you know parent's height:

<div class="parent">
    <div class="child"> <a href="#">Home</a>
    </div>
</div>

CSS:

.parent {
    line-height:80px;
    border: 1px solid black;
}
.child {
    line-height:normal;
    display: inline-block;
    vertical-align:bottom;
    border: 1px solid red;
}

JsFiddle:

Example

formatc
  • 4,261
  • 7
  • 43
  • 81
0

You may not want absolute positioning because it breaks the reflow: in some circumstances, a better solution is to make the grandparent element display:table; and the parent element display:table-cell;vertical-align:bottom;. After doing this, you should be able to give the the child elements display:inline-block; and they will automagically flow towards the bottom of the parent.

<div style="position: relative; 
                width: 200px; 
                height: 150px; 
                border: 1px solid black;">

  <div style="position: absolute; 
                    bottom: 0; 
                    width: 100%; 
                    height: 50px; 
                    border: 1px solid red;">
  </div>
</div>
-1
<div style="width: 200px; height: 150px; border: 1px solid black;position:relative">
    <div style="width: 100%; height: 50px; border: 1px solid red;position:absolute;bottom:0">
    </div>
</div>
ЯegDwight
  • 24,821
  • 10
  • 45
  • 52