views:

37

answers:

3

This is kind of hard to explain, so I'll do my best:

I want to have text content clamped to the bottom of a div - so that no matter how long the content is, it is always set to the bottom.

So if I had:

HEADER
content 1
content 2

Content 2 would be lined up about 20px from the bottom of the parent div and if I had:

HEADER
content 1

Content 1 would be lined up about 20px from the bottom of the parent div, even though there is less content.

Can this be done dynamically without tables?

A: 
<div>
  <div id="header" style="position: absolute; top: 0px; left:0px; right: 0px; height:105px;">
  </div>
  <div id="content" style="position: absolute; top: 105px; left:0px; right: 0px; bottom: 20px; height:105px;"> 
  </div>
  <div id="footer" style="position: absolute; bottom: 0px; left: 0px; right: 0px; height: 20px;">
  </div>
</div>

Hope this gives you an idea...

gsblue
You misunderstand, I know layout rules for divs I want the actual TEXT inside the div to align from the bottom up. To give you some perspective, I'm creating a dropdown menu that opens from the bottom of the page and populated from a database. So I don't know how long the menu is going to be, or how many items it will have - so I want the last line of text strapped to the bottom. Absolute positioning aligns the the div to the bottom of the page, but doesn't adjust based on content as the "positioning" element of the div is in the top left.
Walker
+4  A: 

Yes, just wrap header and content in a single div like this:

<div id="parent">
  <div class="to_bottom">
    <h1>Header</h1>
    <p>Content 1</p>
    <p>Content 2</p>
  </div>
</div>

Then use this CSS:

#parent { position: relative }
.to_bottom { 
    position: absolute;
    left: 0;
    bottom: 20px;
    width: 100%;
} 

More info: Absolute positioning can be relative to any side: top, left, bottom, right. Or a combination.

So: bottom: 0; right: 0 anchors to the right and bottom, but not the top or left. It basically becomes: bottom: 0; right: 0; top: auto; left: auto.

Similarly, using bottom: 20px; left: 0 anchors it to the bottom left, 20px up from the bottom.

Doug Neiner
but doesnt this just map the absolute position of the top left corner of the div?!? If another line of content is added to that div, the top left corner will not move. It will still be 20px from the bottom.
Walker
Try it and see :) But I'll save you the trouble. It will work as you want it to work. The `.to_bottom`'s upper left corner will keep "growing" up from the bottom as more content is added. Think of it this way, a `div` by default tries to be as short as there is content in it. So I anchor this miniature div by the bottom, but as content is added, it grows. The content *in* the div thinks it is bound to the upper left, but the actual div is bound to the bottom. As its content grows, the upper left moves further away from the bottom.
Doug Neiner
A: 

I seem to have run into a similar issue needing to align something to the bottom of the div irrespective of the height of the content above it. Doing some searching I can across the jQuery UI position plugin and am just starting to take a look at it now... maybe of some help to you as well.

garyj