How to align content of a div to the bottom
Solution 1:
Relative+absolute positioning is your best bet:
#header {
position: relative;
min-height: 150px;
}
#header-content {
position: absolute;
bottom: 0;
left: 0;
}
#header, #header * {
background: rgba(40, 40, 100, 0.25);
}
<div id="header">
<h1>Title</h1>
<div id="header-content">And in the last place, where this might not be the case, they would be of long standing, would have taken deep root, and would not easily be extirpated. The scheme of revising the constitution, in order to correct recent breaches of it, as well as for other purposes, has been actually tried in one of the States.</div>
</div>
But you may run into issues with that. When I tried it I had problems with dropdown menus appearing below the content. It's just not pretty.
Honestly, for vertical centering issues and, well, any vertical alignment issues with the items aren't fixed height, it's easier just to use tables.
Example: Can you do this HTML layout without using tables?
Solution 2:
If you're not worried about legacy browsers use a flexbox.
The parent element needs its display type set to flex
div.parent {
display: flex;
height: 100%;
}
Then you set the child element's align-self to flex-end.
span.child {
display: inline-block;
align-self: flex-end;
}
Here's the resource I used to learn: http://css-tricks.com/snippets/css/a-guide-to-flexbox/
Solution 3:
Use CSS positioning:
/* Creates a new stacking context on the header */
#header {
position: relative;
}
/* Positions header-content at the bottom of header's context */
#header-content {
position: absolute;
bottom: 0;
}
As cletus noted, you need identify the header-content to make this work.
<span id="header-content">some header content</span>
<div style="height:100%; position:relative;">
<div style="height:10%; position:absolute; bottom:0px;">bottom</div>
</div>
Solution 4:
I use these properties and it works!
#header {
display: table-cell;
vertical-align: bottom;
}
Solution 5:
After struggling with this same issue for some time, I finally figured out a solution that meets all of my requirements:
- Does not require that I know the container's height.
- Unlike relative+absolute solutions, the content doesn't float in its own layer (i.e., it embeds normally in the container div).
- Works across browsers (IE8+).
- Simple to implement.
The solution just takes one <div>
, which I call the "aligner":
CSS
.bottom_aligner {
display: inline-block;
height: 100%;
vertical-align: bottom;
width: 0px;
}
html
<div class="bottom_aligner"></div>
... Your content here ...
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:
.outer-container {
border: 2px solid black;
height: 175px;
width: 300px;
}
.top-section {
background: lightgreen;
height: 50%;
}
.bottom-section {
background: lightblue;
height: 50%;
margin: 8px;
}
.bottom-aligner {
display: inline-block;
height: 100%;
vertical-align: bottom;
width: 3px;
background: red;
}
.bottom-content {
display: inline-block;
}
.top-content {
padding: 8px;
}
HTML:
<body>
<div class="outer-container">
<div class="top-section">
This text
<br> is on top.
</div>
<div class="bottom-section">
<div class="bottom-aligner"></div>
<div class="bottom-content">
I like it here
<br> at the bottom.
</div>
</div>
</div>
</body>