Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertical align a child div to the bottom of a parent div, and set the child as a percentage of the parent's height

Tags:

html

css

I'm trying to vertical align a child div to the bottom of its parent div, and set it as a percentage of the parent's height.

However, the height: 20% property of the child seems to be getting ignored, and the div stretches to take up the entire 100%.

The end goal is to have a full-screen title page photo as an intro to an article, with a semi-opaque bar running along the bottom of the photo that will have the title of the article in it. I'm using percentages to make it a responsive design for any size screen.

JS Fiddle: http://jsfiddle.net/A52fw/1/

HTML:

<body>
   <div id="outerdiv">
      <div id="innerdiv">
      test
      </div>
   </div>
</body>

CSS:

html, body {
height: 100%;
width: 100%;
}

#outerdiv {
height: 100%;
width: 100%; 
display: table;
}       

#innerdiv {
width: 100%; 
height: 20%;
background-color: red;
display: table-cell;
vertical-align: bottom;
}
like image 646
Jason Davis Avatar asked Sep 19 '25 15:09

Jason Davis


1 Answers

Use positioning (relative on the parent, absolute on the child) instead of the display property:

#outerdiv {
    height: 100%;
    width: 100%;
    position:relative;
}
#innerdiv {
    width: 100%;
    height: 20%;
    background-color: red;
    position:absolute;
    bottom:0;
}

jsFiddle example

like image 139
j08691 Avatar answered Sep 22 '25 18:09

j08691