Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align content of a div to the bottom

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 the 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?

like image 228
kristof Avatar asked Feb 25 '09 13:02

kristof


People also ask

How do I align the content at the bottom of a div?

To align the div content to the bottom, use position: relative to the container class and position: absolute to the div element.

How do I align text to the bottom of a box?

In the Format Text Box dialog box, click the Text Box tab. In the Vertical alignment box, select Top, Middle, or Bottom.

How do I move text to the bottom in CSS?

If position: absolute; or position: fixed; - the bottom property sets the bottom edge of an element to a unit above/below the bottom edge of its nearest positioned ancestor. If position: relative; - the bottom property makes the element's bottom edge to move above/below its normal position.


1 Answers

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?

like image 53
cletus Avatar answered Oct 04 '22 08:10

cletus