0

I'm curious is it possible to use the Grid Layout CSS to create such thing:

************************
*        row #1        *
************************
*                      *
*                      *
*        row #2        *
*                      *
*                      *
************************
*        row #3        *
************************

So the grid must fill the full body height. And there's also some restrictions for other elements:

  • The row #1 is aligned to the top of the grid and can change it's height (but has a max-height value)
  • The row #3 is aligned to the bottom and can change it's height (also has a max-height value)
  • So the row #2 must fill all remaining space in grid.
  • The grid container should not overflow the html body.

There's an example what I achieved: 3 row grid layout. I also can make everything with absolute position like this but there's no use because I can automatically calculate the row #2 margins without any imperative js code.

shadeglare
  • 7,006
  • 7
  • 47
  • 59
  • go for this http://getbootstrap.com/examples/sticky-footer/ – Ravimallya Dec 31 '13 at 08:47
  • row #3 is not fixed. it's not a question about a sticky footer. indeed it's about how to < row#2 = grid - (row#1 + row#3) >. where row#1 and row#2 can vary. – shadeglare Dec 31 '13 at 09:00

3 Answers3

11

I see that the original question is marked as answered, but as the original included an attempt to use the CSS Grid Layout module to solve the problem, I thought I'd complement the answers with some solutions using newer standards.

Using flexbox

First of all, this kind of layout is pretty easy using flexbox. The flex-grow property allows you to define elements that fill the remaining space in this very way. JSBin example using flexbox here

Note: Not using all prefixes (e.g. to target IE10 etc) in the quick demo, but if you use something like autoprefixer it's kind of trivial. Also, beware of bugs relating to things like vh units in iOS and min-height flexbox columns in IE.

Using grid layout

Note: This demo will only work in Chrome Canary at the time the answer was written!

Grid layout is picking up pace and the spec has stabilized a bit. Chrome Canary has an implementation that is pretty far along, as does the WebKit nightly builds.

Grid layout has the same type of flexible sizing as flexbox, and moves the layout mechanism to the container element instead. JSBin demo – remember, Chrome Canary only at the time of writing. (It would work in WebKit nightlies as well with the right prefixes.)

Basically, the whole thing boils down to these lines:

body {
  margin: 0;
  display: grid;
  grid-template-rows: auto 1fr auto;
  grid-template-columns: 100%;
  height: 100vh;
}

The above means "Use the body element as a grid container, place items in it in source order, in a single column that is 100% wide, and size the first and third row according to content, and the middle one takes up all the space that is left". We don't need to specifically place the items inside the grid: they will auto-place themselves – we could change order etc if we wanted though. Grid Layout can do many more advanced things!

Most browser vendors are working on finishing their first grid implementations, so it's fun & worthwhile to start playing with it. :-) Until then, the flexbox version gets you pretty good browser support.

Emil
  • 1,949
  • 2
  • 16
  • 25
  • It has very poor browser compatibility for now. – Michał Dąbrowski Jan 29 '17 at 03:45
  • 1
    Yes, I did note the browser support in several places. Funnily enough, your comment comes at a time when Firefox, Chrome and Safari all have announced that they will support it in their next release - Safari 10.1 will drop very soon, Firefox 52 on March 6th, and Chrome 57 sometime in middle March. So, within the space of a few weeks, support in the latest generation of browsers will go from 0% to like 80%. :-) – Emil Jan 29 '17 at 10:15
2

You can do this with display:table property See Spec and Compatibility

Working Demo

CSS

#container {
    display:table;
}

#head, #content, #foot {
    display:table-row;
}

Edit:

Updated Fiddle

Added div inside table-row to prevent overflow

Surjith S M
  • 6,642
  • 2
  • 31
  • 50
1

what about setting percentages heights like this:

.head{
   height:10%;
   max-height: /*max height allowed*/;
}

.content{
   height:80%;
   max-height: /*max height allowed*/;
}

.foot{
   height:10%;
   max-height: /*max height allowed*/;
}
charlie
  • 46
  • 2