When appending div
s to a div
with a fixed height, the child divs will appear from top to bottom, sticking at the top border.
┌─────────────────────────┐
│ Child Div 1 │
│ Child Div 2 │
│ │
│ │
│ │
└─────────────────────────┘
I'm now trying to display them from bottom to top like this (sticking to the bottom border):
┌─────────────────────────┐
│ │
│ │
│ │
│ Child Div 1 │
│ Child Div 2 │
└─────────────────────────┘
┌─────────────────────────┐
│ │
│ │
│ Child Div 1 │
│ Child Div 2 │
│ Child Div 3 │
└─────────────────────────┘
┌───────────────────────┬─┐
│ Child Div 2 │▲│
│ Child Div 3 │ │
│ Child Div 4 │ │
│ Child Div 5 │█│
│ Child Div 6 │▼│
└───────────────────────┴─┘
And so on... I hope you get what I mean.
Is this simply doable with CSS (something like vertical-align: bottom
)? Or do I have to hack something together with JavaScript?
A more modern answer to this would be to use
flexbox
.As with many other modern features, they won't work in legacy browsers, so unless you're ready to ditch support for browsers from the IE8-9 era you will need to look for another method.
Here's how it's done:
And that's all you need. For further reading on
flexbox
, see MDN.Here's an example of this with some basic styling: http://codepen.io/Mest/pen/Gnbfk
This is simple when you use
position: absolute
.http://jsfiddle.net/XHeZj/
All the answers miss the scrollbar point of your question. And it's a tough one. If you only need this to work for modern browsers and IE 8+ you can use table positioning,
vertical-align:bottom
andmax-height
. See MDN for specific browser compatibility.Demo (vertical-align)
html
Other than that, I think it's not possible with CSS only. You can make elements stick to the bottom of their container with
position:absolute
, but it'll take them out of the flow. As a result they won't stretch and make the container to be scrollable.Demo (position-absolute)
Keepin' it oldskool...
I wanted to do the same thing in a
#header
div so I created an empty div called#headspace
and placed it on the top the stack (inside of#header
):Then I used a percentage, for the height of the invisible
#headspace
div, to push the others down. It's easy to use the developer / inspector tools of the browser to get this just right.