Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I send an inner <div> to the bottom of its parent <div>?

Tags:

html

css

The div inside another div picture and code below. Because there will be text and images of the parent div. And red div will be the last element of the parent div.

alt text

<div style="width: 200px; height: 150px; border: 1px solid black;">     <div style="width: 100%; height: 50px; border: 1px solid red;">     </div> </div> 
like image 939
uzay95 Avatar asked Jan 27 '10 13:01

uzay95


People also ask

How do I move 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 I move a div to the bottom of the screen?

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

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

Use the text-align property to align the inner content of the block element. Use the bottom and left properties. The bottom property specifies the bottom position of an element along with the position property. The left property specifies the left position of an element along with the position property.

How do you keep an element at the bottom of the page?

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.


2 Answers

This is one way

<div style="position: relative;              width: 200px;              height: 150px;              border: 1px solid black;">      <div style="position: absolute;                  bottom: 0;                  width: 100%;                  height: 50px;                  border: 1px solid red;">     </div> </div> 

But because the inner div is positioned absolutely, you'll always have to worry about other content in the outer div overlapping it (and you'll always have to set fixed heights).

If you can do it, it's better to make that inner div the last DOM object in your outer div and have it set to "clear: both".

like image 200
Jon Smock Avatar answered Sep 28 '22 00:09

Jon Smock


A flexbox way.

HTML:

<div class="parent">   <div>Images, text, buttons oh my!</div>   <div>Bottom</div> </div> 

CSS:

.parent {   display: flex;   flex-direction: column;   justify-content: space-between; }  /*  not necessary, just to visualize it */  .parent {   height: 500px;   border: 1px solid black; }   .parent div {   border: 1px solid red; } 

enter image description here

Edit:

Source - Flexbox Guide

Browser support for flexbox - Caniuse

like image 37
Franklin Tarter Avatar answered Sep 28 '22 00:09

Franklin Tarter