Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make div stick to the bottom of parent div?

Tags:

html

css

Could anyone please help me making div#bottom stick to the bottom of the parent div, so when I click on the button inside it will allow me to show elements from div#list above itself?

Preferably I would want to avoid JS, jQuery, etc. or at least in the layout:

<div class="wrapper">    <div id="top">      <ul>       ...       ...      </ul>    </div>    <div class="bottom">       <div id="button">          <div id="list">             <ul>                <li>elem 1</li>                <li>elem 2</li>             </ul>          </div>       </div>    </div> </div> 
like image 222
m1k3y3 Avatar asked Feb 24 '11 14:02

m1k3y3


People also ask

How do you stick a div to the bottom of a parent div?

Set the position of div at the bottom of its container can be done using bottom, and position property. Set position value to absolute and bottom value to zero to placed a div at the bottom of container.

How do you make a div stick to a div?

To make an element sticky, do: make_sticky('#sticky-elem-id'); When the element becomes sticky, the code manages the position of the remaining content to keep it from jumping into the gap left by the sticky element.

How do I move a div to the bottom of the page?

Try position:fixed; bottom:0; . This will make your div to stay fixed at the bottom. Hope this helps.

How do I make DIVS always fit in my parent div?

Method 1: First method is to simply assign 100% width and 100% height to the child div so that it will take all available space of the parent div. Consider this HTML for demonstration: HTML.


2 Answers

.wrapper{position:relative;} .bottom{position:absolute; bottom:0;} 

Edited

Thanks to @centr0 and @T.J Crowder the key thing to understand here is that position:relative in .wrapper will "contain" any element that has position:absolute. It's like declaring boundaries for .bottom. Without position:relative in .wrapper, .bottom would break out of that div

like image 169
Val Avatar answered Sep 22 '22 06:09

Val


I run to a similiar problem. The solution from Val is good, but has one issue - the inner div will stick to bottom, but it will not affect the height of parrent div, thanks to its "position: absolute;" (it is out of the page layout flow).

So here is my expanded solution for anyone having similiar problem:

.wrapper {     position: relative;     padding-bottom: 50px; }  .wrapper .bottom {     position: absolute;     bottom: 0px;     height: 50px; } 

As you can see, I set height of the .bottom ang padding-bottom of the wrapper. Disadvantage is, that the height of the footer is on two places. But I think it is a good solution for example for product list, where there is fixed height in footer of the box (like for price etc).

Remember, that everytime you set "position: absolute;", the element will be absolutely positioned against first parent div, which has set position attribute. Or, eventualy, against the page itself.

like image 31
Martin Brabec Avatar answered Sep 23 '22 06:09

Martin Brabec