Say I have the following CSS and HTML code:
#header {
height: 150px;
}
<div id="header">
<h1>Header title</h1>
Header content (one or multiple lines)
</div>
The header section is fixed height, but the header content may change. I would like to content of the header to be vertically aligned to the bottom of the header section, so the last line of text "sticks" to the bottom of the header section.
So if there is only one line of text it would be like:
-----------------------------
| Header title
|
|
|
| header content (resulting in one line)
-----------------------------
And if there were three lines:
-----------------------------
| Header title
|
| header content (which is so
| much stuff that it perfectly
| spans over three lines)
-----------------------------
How can this be done in CSS?
After struggling with this same issue for some time, I finally figured out a solution that meets all of my requirements:
The solution just takes one
<div>
, which I call the "aligner":CSS
html
This trick works by creating a tall, skinny div, which pushes the text baseline to the bottom of the container.
Here is a complete example that achieves what the OP was asking for. I've made the "bottom_aligner" thick and red for demonstration purposes only.
CSS:
HTML:
The modern way to do it would be using flexbox. See the example below. You don't even need to wrap
Some text...
into any HTML tag, since text directly contained in a flex container is wrapped in an anonymous flex item.If there is only some text and you want to align vertically to the bottom of the container.
try with:
try changing the % to fix it. Example: 120% or 90% ...etc.
I use these properties and it works!
Seems to be working:
HTML: I'm at the bottom
css:
A perfect cross-browser example is probably this one here:
http://www.csszengarden.com/?cssfile=/213/213.css&page=0
The idea is both to display the div at the bottom and also making it stick there. Often the simple approach will make the sticky div scroll up with the main content.
Following is a fully working minimal example. Note that there's no div embedding trickery required. The many BRs are just to force a scrollbar to appear:
If you are wondering your code might not be working on IE, remember to add the DOCTYPE tag at the top. It's crucial for this to work on IE. Also, this should be the first tag and nothing should appear above it.